jenkins trigger build if new tag is released

70,437

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

enter image description here

Solution 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

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

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

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

Share:
70,437
Kingalione
Author by

Kingalione

Full stack developer, loves swift and node.js

Updated on March 08, 2021

Comments

  • Kingalione
    Kingalione about 3 years

    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

  • Kingalione
    Kingalione about 9 years
    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
  • Stan E
    Stan E about 9 years
    have you tried it how I showed in my answer? You should define a tag group and use them as shown on my screenshot.
  • Kingalione
    Kingalione about 9 years
    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?
  • Stan E
    Stan E about 9 years
    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?
  • Kingalione
    Kingalione about 9 years
    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
    Kingalione about 9 years
    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
    Kingalione about 9 years
    Is it clear what I mean? Excuse me for my bad english
  • Stan E
    Stan E about 9 years
    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.
  • Antebios
    Antebios over 7 years
    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
    Antebios over 7 years
    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
    Antebios over 7 years
    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: **
  • Andrew
    Andrew over 6 years
    There's been progress on this: github.com/jenkinsci/github-branch-source-plugin/pull/158 looks pretty close to merge.
  • Dan M.
    Dan M. over 5 years
    @Andrew hm, why is in github plugin, not a general git plugin?
  • uberrebu
    uberrebu about 5 years
    how can one achieve this with groovy job DSL script?stackoverflow.com/questions/54930947/…
  • TangHongWan
    TangHongWan about 5 years
    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?