Joaquim Oliveira
Joaquim Oliveira

Reputation: 1218

Jenkins: how to trigger pipeline on git tag

We want to use Jenkins to generate releases/deployments on specific project milestones. Is it possible to trigger a Jenkins Pipeline (defined in a Jenkinsfile or Groovy script) when a tag is pushed to a Git repository?

We host a private Gitlab server, so Github solutions are not applicable to our case.

Upvotes: 6

Views: 7658

Answers (3)

Moses Liao GZ
Moses Liao GZ

Reputation: 1608

if you use multibranch pipeline, there is a discover tag. Use that plus Spencer solution

Upvotes: 0

Brad Albright
Brad Albright

Reputation: 756

I had the same desire and rolled my own, maybe not pretty but it worked...

In your pipeline job, mark that "This project is parameterized" and add a parameter for your tag. Then in the pipeline script checkout the tag if it is present.

Create a freestyle job that runs a script to:

  • Checkout
  • Run git describe --tags --abbrev=0 to get the latest tag.
  • Check that tag against a running list of builds (like in a file).
  • If the build hasn't occurred, trigger the pipeline job via a url passing your tag as a parameter (in your pipeline job under "Build Triggers" set "Trigger builds remotely (e.g. from scripts) and it will show the correct url.
  • Add the tag to your running list of builds so it doesn't get triggered again.
  • Have this job run frequently.

Upvotes: 0

Spencer Malone
Spencer Malone

Reputation: 1509

This is currently something that is sorely lacking in the pipeline / multibranch workflow. See a ticket around this here: https://issues.jenkins-ci.org/browse/JENKINS-34395

If you're not opposed to using release branches instead of tags, you might find that to be easier. For example, if you decided that all branches that start with release- are to be treated as "release branches", you can go...

if( env.BRANCH_NAME.startsWith("release-") ) {
 // groovy code on release goes here
}

And if you need to use the name that comes after release-, such as release-10.1 turning into 10.1, just create a variable like so...

if( env.BRANCH_NAME.startsWith("release-") ) {
 def releaseName = env.BRANCH_NAME.drop(8)
}

Both of these will probably require some method whitelisting in order to be functional.

Upvotes: 5

Related Questions