Kingalione
Kingalione

Reputation: 4265

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?

git jenkins config

Triggering:

build trigger

Upvotes: 69

Views: 100193

Answers (9)

Joshua-Douglas
Joshua-Douglas

Reputation: 1

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'
  }
}

Upvotes: 0

lindsaymacvean
lindsaymacvean

Reputation: 4537

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.

Upvotes: 0

Sergey
Sergey

Reputation: 61

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

Upvotes: 6

George
George

Reputation: 101

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

Upvotes: 2

SaundersB
SaundersB

Reputation: 647

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/

Upvotes: 1

efrenster
efrenster

Reputation: 1

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

Upvotes: 0

Samuel Henrique
Samuel Henrique

Reputation: 119

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

Upvotes: 11

albertski
albertski

Reputation: 2622

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

branch specifier: **

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

Upvotes: 42

Stan
Stan

Reputation: 3461

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

Upvotes: 31

Related Questions