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.
11 Answers
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.
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
}
}
I did something like this and it worked :
Source Code Management
Git
Repositories
Advance
Name: ref
Refspec : +refs/tags/*:refs/remotes/origin/tags/*
Branches to build
Branch Specifier (blank for 'any') : v0.9.5.2
Jenkins log confirmed that it was getting the source from the tag
Checking out Revision 0b4d6e810546663e931cccb45640583b596c24b9
(v0.9.5.2)
In a latest Jenkins (1.639 and above) you can:
- just specify name of tag in a field 'Branches to build'.
- in a parametrized build you can use parameter as variable in a same field 'Branches to build' i.e. ${Branch_to_build}.
- you can install Git Parameter Plugin which will provide to you functionality by listing of all available branches and tags.
I set the Advanced->Refspec field to refs/tags/[your tag name]
. This seems simpler than the various other suggestions for Refspec, but it worked just fine for me.
UPDATE 23/7/2014 - Actually, after further testing, it turns out this didn't work as expected. It appears that the HEAD version was still being checked out. Please undo this as the accepted answer. I ended up getting a working solution by following the post from gotgenes in this thread (30th March). The issue mentioned in that post of unnecessary triggering of builds was not an issue for me, as my job is triggered from an upstream job, not from polling SCM.
UPDATE APR-2018 - Note in the comments that this does work for one person, and agrees with Jenkins documentation.
I was able to get Jenkins to build a tag by setting the Refspec and Branch Specifier as detailed in this blog post.
I also had to set the Repository Name (to "origin" in my case) so that I could reference it in the Refspec (otherwise it would apparently use a randomly generated name).
What I did in the end was:
- created a new branch
jenkins-target
, and got jenkins to track that - merge from whichever branch or tag I want to build onto the
jenkins-target
- once the build was working, tests passing etc, just simply create a tag from the
jenkins-target
branch
I'm not sure if this will work for everyone, my project was quite small, not too many tags and stuff, but it's dead easy to do, dont have to mess around with refspecs and parameters and stuff :-)
Adding my two cents here since I have not seen an answer that uses the option "Build with parameters" in Jenkins.
Here I am using Jenkins CI browser console for project starwars_api and I was able to build directly with "Build with parameters" with value refs/tags/tag-name
- choose the "build with parameters" option.
- add value in the box as "refs/tags/tag_142" (tag_name = tag_142 for my example)