Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 780
Joaquim Oliveira Avatar asked May 16 '17 14:05

Joaquim Oliveira


People also ask

How do I pass tag name in Jenkins pipeline?

just specify name of tag in a field 'Branches to build'. in a parametrized build you can use parameter as variable in a same field 'Branches to build' i.e. ${Branch_to_build}. you can install Git Parameter Plugin which will provide to you functionality by listing of all available branches and tags.


1 Answers

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.

like image 127
Spencer Malone Avatar answered Nov 02 '22 08:11

Spencer Malone