jenkins trigger build if new tag is released

GitJenkinsTags

Git Problem Overview


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

Git Solutions


Solution 1 - Git

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

branch specifier: **

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

Solution 2 - Git

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

Solution 3 - Git

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

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

Solution 5 - Git

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/

Solution 6 - Git

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

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

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.

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
QuestionKingalioneView Question on Stackoverflow
Solution 1 - GitalbertskiView Answer on Stackoverflow
Solution 2 - GitStan EView Answer on Stackoverflow
Solution 3 - GitSamuel HenriqueView Answer on Stackoverflow
Solution 4 - GitSergeyView Answer on Stackoverflow
Solution 5 - GitSaundersBView Answer on Stackoverflow
Solution 6 - GitGeorgeView Answer on Stackoverflow
Solution 7 - GitefrensterView Answer on Stackoverflow
Solution 8 - GitlindsaymacveanView Answer on Stackoverflow