129

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.

Jonik
  • 74,291
  • 66
  • 249
  • 356
sksamuel
  • 15,025
  • 8
  • 54
  • 97
  • You mean this is different from http://stackoverflow.com/questions/7157170/can-i-get-jenkins-to-build-a-git-tag-from-a-passed-in-parameter ? (third result of https://www.google.com/search?q=git+jenkins+tag&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a) – VonC Apr 17 '12 at 17:34
  • 1
    "This has been taking 3 hours of my day" - I'm not so lazy that 3 hours of my day didn't include every link I could find on google :) – sksamuel Apr 17 '12 at 17:42
  • @mpontillo - that link is now being squatted on. – Tydaeus Jan 15 '21 at 16:47
  • 2
    Thanks @Tydaeus - I couldn't edit it, so I've deleted the original comment with the reference, which noted that tagging in git doesn't scale and it might be advisable to use another option, such as an "execute shell" task to write a script to check out the required tag/revision. – mpontillo Jan 15 '21 at 17:13

11 Answers11

241

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.

Emmanuel Keller
  • 2,939
  • 1
  • 12
  • 15
  • 7
    I don't know why this doesn't have more +1s. That erics-notes blog entry is confusing as hell. This is simple and works great. Thanks! – Cody S Nov 25 '13 at 19:54
  • 4
    Worked great for me. Thanks. My parameter was named RELEASE_TAG so I used tags/${RELEASE_TAG} as the value for Branch Specifier. – Wesley Womack Jul 17 '14 at 19:32
  • 4
    Could not get this to work. For some reason can't checkout the tag. I get: 'ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.' I specify tags/3.0.1, I have also tried */tags/3.0.1 I verified the tag does exist. – lostintranslation Nov 25 '14 at 18:54
  • 1
    When I try doing what is suggested in this answer, every poll of the repository triggers a build. The git polling log will continuously show that the "Last Built Revision" is the revision of the tag but the "Latest remote head revision is" is the revision of the newest `HEAD`. The logic of the git plugin seems to compare these two revisions, which in my repository are *always* unequal and thus a new build is always triggered. – Louis Nov 26 '14 at 14:20
  • This must be the right answer surely, it worked for me and is so simple. I don't poll the repo though, so I guess there is still that issue. – Jeremy Jan 11 '16 at 13:28
  • Works great, easy to use git tags with jenkins – Mahtab Alam Mar 24 '17 at 17:19
76

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.

  1. Make sure you push your tag to the remote repository with git push --tags
  2. In the "Git Repository" section of your job, under the "Source Code Management" heading, click "Advanced".
  3. In the field for Refspec, add the following text: +refs/tags/*:refs/remotes/origin/tags/*
  4. 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.

Community
  • 1
  • 1
gotgenes
  • 34,337
  • 27
  • 93
  • 119
  • 5
    To "add the following text" to the refspec ... if your refspec was previously `+refs/heads/*:refs/remotes/origin/*` it will now be `+refs/heads/*:refs/remotes/origin/* +refs/tags/*:refs/remotes/origin/tags/*`. (I haven't worked with refspecs much, so it took some experimentation to learn that this field is space-delimited.) – driftcatcher May 01 '14 at 17:00
  • 1
    An extra +1 for this solution. The prior solutions did not work for me either. – whitespy9 May 31 '15 at 19:18
16

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.

Andrew Prock
  • 6,273
  • 5
  • 36
  • 58
Andrew T Finnell
  • 12,827
  • 2
  • 30
  • 47
  • You're not the first person to suggest team city actually. Is it really that much better? I might check it out. – sksamuel Apr 17 '12 at 17:42
  • 1
    @monkjack I tried the same syntax on one of my repo's and it worked. Can you list your current tags? Are you sure you've specifically pushed that tag to the remote repo with `git push --tags` – Andrew T Finnell Apr 17 '12 at 17:52
  • @monkjack I am not sure how Jenkins or Hudson are ever picked over TeamCity except for their restrictions on the number of Agents and Projects. They are really different in terms of quality. I'll pay for TeamCity when/if the time ever comes. – Andrew T Finnell Apr 17 '12 at 17:53
  • If you're not in a Windows environment, Pulse (zutubi.com) and Bamboo (Atlassian) are also good alternatives to Hudson/Jenkins. – ebneter Apr 17 '12 at 22:01
  • 4
    Getting closer. I wasn't pushing the tags up to remote, but now I am. I can get jenkins to build now by using refs/tags/harpercollins-1.0.16 however it always insists on building head regardless of what I put in there. I've confirmed that the remote has the tag (can see it in gitweb) and doing a snapshot of that tag confirms everything is in there properly. – sksamuel Apr 18 '12 at 09:56
  • @monkjack "it always insists on building head regardless..." Yep, that's what [I found](http://stackoverflow.com/questions/10195900/jenkins-git-plugin-how-to-build-specific-tag#comment42797466_19632917) too. – Louis Nov 26 '14 at 14:30
  • 6
    TeamCity is ‎proprietary, making it pretty much useless. – slang Nov 20 '15 at 20:16
  • 2
    Oh yes, switching from free tool to commercial is right choice! When jetbrains reinvent the wheel and create new bug tracker will you propose to others switching from bugzilla to that? – m1ld Dec 02 '15 at 08:12
11

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
  }
}
Vincent de Lagabbe
  • 4,354
  • 3
  • 28
  • 35
10

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

enter image description here

Jenkins log confirmed that it was getting the source from the tag

Checking out Revision 0b4d6e810546663e931cccb45640583b596c24b9 (v0.9.5.2)

MD XF
  • 7,062
  • 7
  • 34
  • 64
Hasan Shaik
  • 109
  • 1
  • 2
  • This is great for building all tags, thanks! Adding the `refspec` was the trick by clicking the Advanced button. – styfle Nov 17 '16 at 21:48
9

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.

Community
  • 1
  • 1
Nebu
  • 1,294
  • 1
  • 14
  • 18
  • Just wanted to note that—four years after this answer was posted—using `refs/tags/` is what the Jenkins documentation *says* should be used, and it's working fine for me. Perhaps the plugin was buggy at the time of the original post, but... as of April 2018, this *is* the correct answer. – evadeflow Apr 10 '18 at 17:01
  • Updating my previous comment: Actually, I've found that I can omit the `refs/tags` prefix and just use ``. YMMV, but... it's working fine for my purposes. – evadeflow Apr 10 '18 at 18:31
9

In a latest Jenkins (1.639 and above) you can:

  1. just specify name of tag in a field 'Branches to build'.
  2. in a parametrized build you can use parameter as variable in a same field 'Branches to build' i.e. ${Branch_to_build}.
  3. you can install Git Parameter Plugin which will provide to you functionality by listing of all available branches and tags.
m1ld
  • 1,133
  • 8
  • 22
  • 1
    Indeed just entering a tag name worked for me too. Although the documentation for this in the git plugin still say specifically that doing that should not work: _": This does not work since the tag will not be recognized as tag. Use refs/tags/ instead."_ – Zitrax Apr 21 '16 at 09:58
  • This worked for me in Jenkins 1.532.3, I just specified the tag version (e.g. `1.0.1`) in the branches to build field. – andre Jun 05 '18 at 15:34
3

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).

Thilo
  • 241,635
  • 91
  • 474
  • 626
2

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 :-)

Hugh Perkins
  • 6,646
  • 6
  • 50
  • 63
2

You can build even a tag type, for example 1.2.3-alpha43, using wildcards:

Refspec: +refs/tags/*:refs/remotes/origin/tags/*

Branch specifier: origin/tags/1.2.3-alpha*

You can also tick "Build when a change is pushed to GitHub" to trigger the push, but you have to add "create" action to the webhook

riverfall
  • 762
  • 5
  • 21
1

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

  1. choose the "build with parameters" option.
  2. add value in the box as "refs/tags/tag_142" (tag_name = tag_142 for my example)

build with ref tag name

anayagam
  • 81
  • 4