'jenkins trigger build if new tag is released
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?
Triggering:
Thanks for any help
Solution 1:[1]
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.
Solution 2:[2]
Set refspec to: +refs/tags/*:refs/remotes/origin/tags/*
branch specifier: **
Under build triggers check Build when a change is pushed to GitHub
Solution 3:[3]
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
Solution 4:[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:
create job with:
Refspec: '+refs/tags/*':'refs/remotes/origin/tags/*'
Branch Specifier: **/tags/**
Run build
Reconfigure the same job to parameters:
Refspec: '+refs/tags/*':'refs/remotes/origin/tags/*'
Branch Specifier: **
Run build
Reconfigure the same job to parameters:
Refspec: '+refs/tags/*':'refs/remotes/origin/tags/*' Branch Specifier: **/tags/**
Run the build
Only after this magic steps, when I tag the branch it automatically trigger Jenkins
Solution 5:[5]
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.
Solution 6:[6]
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...
Solution 7:[7]
@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.
Solution 8:[8]
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:
- Set the refspec to
'+refs/tags/*':'refs/remotes/origin/tags/*'
- Set the branch specifier to
refs/tags/{A SPECIFIC TAG}
- Make sure 'Delete workspace before build starts' is unticked
- Run the build to create initial workspace
- Set the branch specifier to
refs/tags/**
- Make sure Polling to your Git service is ticked
- Set up the webhook on the Git service (e.g. Github)
- 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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow