opti2k4
opti2k4

Reputation: 143

Can i checkout code with git tag as parameter in Jenkins?

I want to setup Jenkins pipeline that will ask me git tag every time I run the job. I am using declarative pipeline and couldn't find a way to do it. I found Jenkins plugin Git Parameter Plugin! which looks like it has what I need but not quite. In the example there is branch definition with parameter TAG but value is master which is git branch and not some string that would indicate it really is a git tag. If I setup a pipeline type of job, I need to pull repo to be able to get to the Jenkins file.

Alternative way would be to somehow avoid default git pull by Jenkins (that is required for Jenkins file since it's in SCM repo) an find tag remotely inside the branch and then pull the branch or maybe pull the branch, find the tag and do 2nd checkout. Not sure what is the best way to approach the problem so recommendations are welcomed.

Thanks!

Upvotes: 2

Views: 3834

Answers (2)

Unforgettable631
Unforgettable631

Reputation: 1020

We do checkout (once) based on a tag like this for GIT, see especially the section for branches as this can identify how it's setup if it's a tag:

                checkout([
                        $class                           : 'GitSCM',
                        branches                         : [[name: "refs/tags/${yourTagName}"]],
                        userRemoteConfigs                : [[credentialsId: 'user', url: 'url']],
                        doGenerateSubmoduleConfigurations: false
                ])

For Declarative you can use parameters (choice or string) to select or fill in the tag you want to checkout. Preferably use string as you have to maintain choices if you change / add / remove tags if you choose to use choice parameters.

parameters {
  choice choices: ['v51.0.0', 'v50.1.3'], description: 'Pick your Tag to be checked out!', name: 'yourGitTag'
  string defaultValue: '', description: 'FIll in your Tag to be checked out!', name: 'yourGitTag', trim: true
}

The defaultValue will be used if you don't provide any input. More options available to generate available scripts in your Jenkins instance can be found under <yourJenkinsUrl>/directive-generator/.

Upvotes: 1

Dillip Kumar Behera
Dillip Kumar Behera

Reputation: 289

To checkout a tag, provide the remote tag name in the place of the Branch parameter.

Upvotes: 0

Related Questions