Jenkins Git Plugin: How to build specific tag?
I am having issues getting Jenkins to build a specified tag. The tag is part of a parametrized build, but I do not know how to pass this through to the git plugin to just build that tag. This has been taking 3 hours of my day and I have conceded defeat to the masters at Stack Overflow.
Solution 1:
I was able to do that by using the "branches to build" parameter:
Branch Specifier (blank for default): tags/[tag-name]
Replace [tag-name] by the name of your tag.
Solution 2:
None of these answers were sufficient for me, using Jenkins CI v.1.555, Git Client plugin v.1.6.4, and Git plugin 2.0.4.
I wanted a job to build for one Git repository for one specific, fixed (i.e., non-parameterized) tag. I had to cobble together a solution from the various answers plus the "build a Git tag" blog post cited by Thilo.
- Make sure you push your tag to the remote repository with
git push --tags
- In the "Git Repository" section of your job, under the "Source Code Management" heading, click "Advanced".
- In the field for Refspec, add the following text:
+refs/tags/*:refs/remotes/origin/tags/*
- Under "Branches to build", "Branch specifier", put
*/tags/<TAG_TO_BUILD>
(replacing<TAG_TO_BUILD>
with your actual tag name).
Adding the Refspec for me turned out to be critical. Although it seemed the git repositories were fetching all the remote information by default when I left it blank, the Git plugin would nevertheless completely fail to find my tag. Only when I explicitly specified "get the remote tags" in the Refspec field was the Git plugin able to identify and build from my tag.
Update 2014-5-7: Unfortunately, this solution does come with an undesirable side-effect for Jenkins CI (v.1.555) and the Git repository push notification mechanism à la Stash Webhook to Jenkins: any time any branch on the repository is updated in a push, the tag build jobs will also fire again. This leads to a lot of unnecessary re-builds of the same tag jobs over and over again. I have tried configuring the jobs both with and without the "Force polling using workspace" option, and it seemed to have no effect. The only way I could prevent Jenkins from making the unnecessary builds for the tag jobs is to clear the Refspec field (i.e., delete the +refs/tags/*:refs/remotes/origin/tags/*
).
If anyone finds a more elegant solution, please edit this answer with an update. I suspect, for example, that maybe this wouldn't happen if the refspec specifically was +refs/tags/<TAG TO BUILD>:refs/remotes/origin/tags/<TAG TO BUILD>
rather than the asterisk catch-all. For now, however, this solution is working for us, we just remove the extra Refspec after the job succeeds.
Solution 3:
Can't you tell Jenkins to build from a Ref name? If so then it's
refs/tags/tag-name
From all the questions I see about Jenkins and Hudson, I'd suggest switching to TeamCity. I haven't had to edit any configuration files to get TeamCity to work.
Solution 4:
If you are using Jenkins pipelines and want to checkout a specific tag (eg: a TAG
parameter of your build), here is what you can do:
stage('Checkout') {
steps {
checkout scm: [$class: 'GitSCM', userRemoteConfigs: [[url: 'YOUR_GIT_REPO_URL.git', credentialsId: 'YOUR_GIT_CREDENTIALS_ID' ]], branches: [[name: 'refs/tags/${TAG}']]], poll: false
}
}