Jenkins Git Plugin: How to build specific tag?

GitJenkins

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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 - Git

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.

Solution 3 - Git

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 - Git

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
  }
}

Solution 5 - Git

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)

Solution 6 - Git

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.

Solution 7 - Git

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][1] which will provide to you functionality by listing of all available branches and tags.

[1]: https://wiki.jenkins-ci.org/display/JENKINS/Git+Parameter+Plugin "Git Parameter Plugin"

Solution 8 - Git

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

Solution 9 - Git

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

Solution 10 - Git

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

Solution 11 - Git

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

Solution 12 - Git

I found the best solution for this issue, You should use the GitSCM class instead of git class:

stages {
        stage('SCM') {
            steps {
                script {
                    wrap([$class: 'BuildUser']) {
                        currentBuild.displayName = "#${BUILD_NUMBER} - ${env.BUILD_USER} --> Build from branch ${BRANCH}"
                        slackSend channel: "#jenkins-build", color: "#02fe21", message: "${JOB_NAME} started by ${env.BUILD_USER} : #${BUILD_NUMBER} --> Build from ${BRANCH} branch with ${TAG} TAG"
                    }
                }
                checkout(
                        changelog: false, poll: false, scm: [
                        $class           : 'GitSCM',
                        branches         : [
                                [name: "$BRANCH"],
                        ],
                        userRemoteConfigs: [
                                [
                                        url: "<YOUR-GIT-URL>/${REPOSITORY}.git", credentialsId: '<YOUR-CREDENTIAL>'
                                ],
                        ],
                ])
            }
        }

Solution 13 - Git

For git-tag "v1.5.4" I used the follow regex

:(.v[\d.]{1,15}.)

Jenkis config

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsksamuelView Question on Stackoverflow
Solution 1 - GitEmmanuel KellerView Answer on Stackoverflow
Solution 2 - GitgotgenesView Answer on Stackoverflow
Solution 3 - GitAndrew T FinnellView Answer on Stackoverflow
Solution 4 - GitVincent de LagabbeView Answer on Stackoverflow
Solution 5 - GitHasan ShaikView Answer on Stackoverflow
Solution 6 - GitNebuView Answer on Stackoverflow
Solution 7 - Gitm1ldView Answer on Stackoverflow
Solution 8 - GitThiloView Answer on Stackoverflow
Solution 9 - GitHugh PerkinsView Answer on Stackoverflow
Solution 10 - GitriverfallView Answer on Stackoverflow
Solution 11 - GitanayagamView Answer on Stackoverflow
Solution 12 - GitMohammad DhnaviView Answer on Stackoverflow
Solution 13 - GitDmitry IvanovView Answer on Stackoverflow