65
votes

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

Thanks for any help

7

7 Answers

38
votes

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

branch specifier: **

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

30
votes

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

8
votes

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

3
votes

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

1
votes

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/

1
votes

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

0
votes

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