Jenkins: how to trigger pipeline on git tag
M

3

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.

Mansard answered 16/5, 2017 at 14:14 Comment(0)
S
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.

Steere answered 18/5, 2017 at 21:0 Comment(0)
M
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.
Malagasy answered 19/5, 2017 at 16:15 Comment(4)
Brad, do you have these jobs in a public repo, so I can see how you implemented this freestyle job?Mansard
@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.Malagasy
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.Devote
@Devote 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 :)Malagasy
C
0

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

Circumstantiate answered 26/3, 2021 at 5:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.