69

I want to configure jenkins so that it starts building if a new tag is released in any branch of an git repository. How do I configure this behaviour?

git jenkins config

Triggering:

build trigger

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Kingalione
  • 4,237
  • 6
  • 49
  • 84

9 Answers9

41

Set refspec to: +refs/tags/*:refs/remotes/origin/tags/*

branch specifier: **

Under build triggers check Build when a change is pushed to GitHub

Elnur Abdurrakhimov
  • 44,533
  • 10
  • 148
  • 133
albertski
  • 2,428
  • 1
  • 25
  • 44
  • 1
    You forgot the stars for the refspec: +refs/tags/*:refs/remotes/origin/tags/*. Also, the branch specifier (**) didn't help, but "refs/tags/*" did it. Now it's only being triggered for tagged commits. – Antebios Jan 05 '17 at 19:39
  • 2
    I stand corrected: branch specifier needs to be `**`. Let me explain why: If you did a commit, then Jenkins job polled the repo, then you tagged... Jenkins will not detect the change. If you left branch specifier as `**`, then did the same process, then your new tag will be detected even if no new commit was introduced. SO, user "albertski" almost had the correct answer, just add the ASTERISK after "tags/". – Antebios Jan 05 '17 at 23:38
  • 2
    This isn't a reliable answer, it will trigger the build each time any branch is pushed or a tag is created. But it will only check-out the most recent commit in the repository, tagged or not. Eg: If you push a commit to branch `main`, then you push a different commit to branch `release/0.1`, then you tag the first commit on `main` as `v1.0.1` and push that tag; the job will trigger and check out the commit on `release/0.1`. As far as I can tell the desired behaviour is impossible. – Bracken Aug 09 '22 at 15:26
31

What do you mean by new tag? Does it has some template name?

You can surely define it in Advanced --> Refspec -->refs/tags/{tagname} .

You can even do refs/tags/* for finding really ANY new tags.

enter image description here

E.Z.
  • 6,393
  • 11
  • 42
  • 69
Stan E
  • 3,396
  • 20
  • 31
  • 1
    yes i want that jenkins starts building if a new tag is released in the /tags folder. I tried it with adding /tags/* to the branch specifier but it didn't worked for me – Kingalione Apr 20 '15 at 08:51
  • have you tried it how I showed in my answer? You should define a tag group and use them as shown on my screenshot. – Stan E Apr 20 '15 at 09:08
  • 1
    yes i tried it and it works half. It works if I tag a version to the latest revision not if I tag an older revision. I want that jenkins starts building if a new tag is tagged on any revision. I used +refs/tags/*:refs/remotes/uw/tags/* similar to your example in the refspec. Am I missing something? – Kingalione Apr 20 '15 at 09:27
  • So, to conclude. My refspec is something like `+refs/tags/*:refs/remotes/origin/tags/*` , my branch is `*/tags/*` + sure you will add SCM polling every N minutes (I have 5). What are you getting after that? – Stan E Apr 20 '15 at 09:40
  • I have updated my post with my current configuration which allows me to trigger the build if I tag the latest revision not any revision. thanks for your help – Kingalione Apr 20 '15 at 09:53
  • 2
    Well I found out that if I do no changes in the source jenkins will not start building the tag even if its the latest revision. But in our case it is neccessary to trigger the building even if there are no changes in the source. If a new tag is created, jenkins should build it no matter what. – Kingalione Apr 20 '15 at 10:11
  • Is it clear what I mean? Excuse me for my bad english – Kingalione Apr 20 '15 at 13:29
  • 6
    Yes, got it. Git is triggered after polling only if new code persist. It's a major rule for that plugin, so there is no way to make a workaround with only git plugin. The thing that I can advice you is to make 2 tasks. 1st task is running every N minutes and just working with console. You are executing `git describe --tags` as a shell command and get the latest tag from git. Then you are writing the tag in the some property file (for saving that you have already run a task for that tag) and polling another stream where you are passing tag name and building it. – Stan E Apr 20 '15 at 14:00
  • As what albertski said below, this is what worked for me: Set refspec to: `+refs/tags/*:refs/remotes/origin/tags/*` and finally set branch specifier: `**` – Antebios Jan 05 '17 at 23:41
  • how can one achieve this with groovy job DSL script?https://stackoverflow.com/questions/54930947/groovy-job-dsl-for-triggering-jenkins-based-on-new-release-tags – uberrebu Feb 28 '19 at 23:10
10

Please note that the approach in the answer provided by stanjer doesn't make Jenkins trigger builds on new tags if they point to commits that were built before. For example, you tag release v1.0.0 (to make jenkins deploy this release), then on the future you have to rollback to v1.0.0, tagging its commit again, but with v1.0.0-rollback, Jenkins won't deploy your rollback because it will check the hash the tag points to, not the hash of the tag itself.

In summary, jenkins will only build new tags if they point to commits that are not tagged already, and this is currently not tweakable.

It would be awesome if one could use Jenkins as a CD tool working with tags for deploys and rollbacks.

More info here https://groups.google.com/forum/#!msg/jenkinsci-users/mYxtDNMz1ZI/xbX9-xM9BQAJ

Community
  • 1
  • 1
Samuel Henrique
  • 109
  • 1
  • 2
  • 2
    There's been progress on this: https://github.com/jenkinsci/github-branch-source-plugin/pull/158/ looks pretty close to merge. – Andrew Oct 26 '17 at 21:07
  • 2
    @Andrew hm, why is in github plugin, not a general git plugin? – Dan M. Jan 24 '19 at 12:58
4

Previous doesn't work for me. In my case works refspec in single quotes:

Refspec: '+refs/tags/*':'refs/remotes/origin/tags/*' Branch Specifier: **/tags/**

I have Jenkins 2.120. To make job work which is triggered by tag need to do the following steps:

  1. create job with:

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

    Branch Specifier: **/tags/**

  2. Run build

  3. Reconfigure the same job to parameters:

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

    Branch Specifier: **

  4. Run build

  5. Reconfigure the same job to parameters:

    Refspec: '+refs/tags/*':'refs/remotes/origin/tags/*' Branch Specifier: **/tags/**

  6. Run the build

Only after this magic steps, when I tag the branch it automatically trigger Jenkins

Neuron
  • 5,141
  • 5
  • 38
  • 59
Sergey
  • 41
  • 2
  • This also does not work for me. I added hook on github side and can see the hook is correctly triggered on github (enterprise version). But still cannot trigger the job run on Jenkins. The git plugin is installed and build triggers "GitHub hook trigger for GITScm polling" is also checked. Don't know why it's does not work. But when I change the event to "Pushes" on github, then the trigger will work! So the question is how to get jenkins job triggered when tag created? – TangHongWan Apr 13 '19 at 00:58
2

Combined @albertski and @Sergey answers works for me.

Path: Jenkins > {YourJob} > Configure > Pipeline > Definition(Pipeline script from SCM) > SCM(Git)

Options:

Repositories > Advanced... > Refspec +refs/tags/v*:refs/remotes/origin/tags/v*

Branches to build > Branch Specifier (blank for 'any') **/tags/v*

Set v* if you want build tags started with v, such as v0.1.0, v1.0.5...

George
  • 91
  • 6
1

They released a new "buildingTag" that can be used in a when block.

buildingTag - A simple condition that just checks if the Pipeline is running against a tag in SCM, rather than a branch or a specific commit reference.

https://jenkins.io/blog/2018/04/09/whats-in-declarative/

SaundersB
  • 607
  • 2
  • 12
  • 25
0

@albertski answer works but do not forget below additional settings: 1. Setup hook from Bitbucket to Jenkins 2. Polling SCM need to be checked

You can test the trigger by adding new git tag from a commit in your bitbucket repo.

0

I was really stuck on this because I had ticked 'delete workspace', however the build needs an existing workspace to compare against. So I did the following:

  1. Set the refspec to '+refs/tags/*':'refs/remotes/origin/tags/*'
  2. Set the branch specifier to refs/tags/{A SPECIFIC TAG}
  3. Make sure 'Delete workspace before build starts' is unticked
  4. Run the build to create initial workspace
  5. Set the branch specifier to refs/tags/**
  6. Make sure Polling to your Git service is ticked
  7. Set up the webhook on the Git service (e.g. Github)
  8. Create a new tag in the Git service to trigger webhook

This should now work. The message in the log that you need to look out for is Multiple candidate revisions this means that when Git fetched from the remote and then applied the branch specifier there were multiple matches so it just picks the first in the list.

lindsaymacvean
  • 4,419
  • 2
  • 19
  • 21
0

If anyone is using multibranch pipeline projects, then the answer is a bit simpler. Once you've setup the webhook on your version control service, you can enable tagged branch builds by adding a branch behavior within the project configuration:

BranchSources > Behaviors > Add > Discover Tags

Tagged branches should now build when the tag is pushed. As others have mentioned, you can create tag-specific logic within your Jenkinsfile using the buildingTag() function and TAG_NAME env var.

stage('Deploy') {
  when { buildingTag() } // only execute this stage on tags
  steps {
    // pass tag name to my fancy deployment script
    pwsh '.deploy.ps1 -release_name $env:TAG_NAME'
  }
}