Execute Jenkins Pipeline step only when building a tag
Asked Answered
T

3

9

I have certain build logic, such as publication, that I would like to have Jenkins perform only when it is building a Git tag. How can I accomplish this using Jenkin's Declarative Pipeline?

In other words, I am trying to build functionality equivalent to Travis CI's deploy on tags functionality:

deploy:
  [...]
  on: 
    tags: true

There is a built-in condition to check the branch, but I do not see one that indicates the tag.

Trudytrue answered 21/1, 2018 at 2:50 Comment(1)
T
22

Update: As of version 1.2.8 of the Pipeline Model Definition Plugin you can now use buldingTag():

stage('Deploy') {
  when {
    buildingTag()
  }
  steps {
    echo 'Replace this with your actual deployment steps'
  }
}

When using the Multibranch Pipeline configuration you can use the expression condition along with the TAG_NAME environment variable provided by the underlying Branch API Plugin. Unfortunately, you can't directly check if the environment variable is defined at the Groovy level (API restrictions) so you have to test in in the shell:

stage('Deploy') {
  when { expression { sh([returnStdout: true, script: 'echo $TAG_NAME | tr -d \'\n\'']) } }
  steps {
    echo 'Replace this with your actual deployment steps'
  }
}

The above takes advantage of non-empty strings being truthy in Groovy.

An easier way may be introduced in future versions. See jenkinsci/pipeline-model-definition-plugin#240.

Trudytrue answered 21/1, 2018 at 2:50 Comment(2)
Can somebody explain what does the expression in the 2nd example should do? Sadly buildingTag() doesn't work for me :(Pericynthion
@Pericynthion or whomever - it appears the expression opens a shell, executes a script to echo the TAG_NAME environment variable, removes the newline, and returns the result which it evaluates as either empty string (false) or non-empty string (true), so if TAG_NAME has a non-empty value it returns true.Hooky
P
3

Declarative Pipelines

Execute STAGE only when building for Tag

For a whole stage, @vossad01 already provided the answer to use buildingTag() within a when like

stage('Deploy') {
  when {
    buildingTag()
    beforeAgent true
  }
  steps {
    echo 'deploy something when triggered by tag
  }
}

the beforeAgent true is optional and ensures to check the condition before executing on an agent.

Execute STEP only when building for Tag

In case it's just about a step within a stage that should be conditional on a tag, you can use Groovy syntax (as used in Scripted Pipelines) inside a script block as follows.

stage('myStage') {                                              
  steps {                                                       
    echo 'some steps here that should always be executed'
    script {                                                    
      if (env.TAG_NAME) {                                       
        echo "triggered by the TAG:"                                 
        echo env.TAG_NAME                                       
      } else {                                                  
        echo "triggered by branch, PR or ..."                              
      }                                                         
    }                                                           
  }                                                             
}

Additional hint to build for Tags

You may also be interested in the basic-branch-build-strategies-plugin that enables to trigger builds when pushing tags (which is not enabled per default). See here for how to use it. You can install it via Manage Jenkins -> Manage Plugins -> Available tab and then use the Search bar.

Populace answered 19/10, 2021 at 7:57 Comment(0)
C
1

I had a similar situation which I handled by getting the branch name ,in case of tag it is like refs/tags/v101.0.0-beta8468 , so you have to extract / parse this to check if its a tag otherwise its just the branch name like pipeline . eg.

if(env.gitlabBranch.contains("tags"))
    {
        isTag = true
        echo "----------------true----------------"
        branch = env.gitlabBranch.split("/")[2]
        imageTag = branch

    }
    else
    {
        branch = "origin/$env.gitlabBranch"

    }

And in the chekout step mention the branch as

 branches: [[name: "${branch}"]

if you want to checkout from the same project. Based on the isTag variable you can choose to run a certain stage . Like:

if(isTag) {
stage('Deploy') {
   // your logic here
}

initialise your isTag as false :)

Clunk answered 21/1, 2018 at 18:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.