6

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.

Joaquim Oliveira
  • 1,208
  • 2
  • 16
  • 29

3 Answers3

5

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.

Spencer Malone
  • 1,449
  • 12
  • 12
0

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.
Brad Albright
  • 686
  • 7
  • 12
  • Brad, do you have these jobs in a public repo, so I can see how you implemented this freestyle job? – Joaquim Oliveira May 19 '17 at 17:45
  • @JoaquimOliveira I don't have it in a repo and I can't share it verbatim (sorry). The freestyle job really just has one main thing, in the Build section I have a build step to run a powershell script (I am in Windows, and have the powershell plugin). The powershell script just implements the behavior I noted above, run the git commands and check the tag against a text file, then trigger the build via a url. – Brad Albright May 19 '17 at 18:08
  • It is fine, but the issue here is, you have to put some validation for the already run build and not to trigger again for the same tag. But whenever there is being a push it triggers a build and it can not be controlled. I want build to trigger only with a push of a tag. – hi.nitish Dec 10 '17 at 06:44
  • @hi.nitish yeah I hear you, I wanted something more automatic too, but I didn't find it so I came up with my own solution because that's what programmers do :) – Brad Albright Jan 05 '18 at 16:34
0

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

Moses Liao GZ
  • 1,556
  • 5
  • 20
  • 45