Execute stage only for branches with or without tags in Jenkins

202 Views Asked by At

I want Jenkins to build a certain stage only if the build runs for a branch without a tag. I am using the declarative syntax.

This is what I have so far:

stage ('Do stuff'){
    when {
        expression {
            TAG_NAME == null
        }
    }
    steps {
        sh'''#!/bin/bash
           ...
        '''
    }
}

When running the build, I get the following error:

groovy.lang.MissingPropertyException: No such property: TAG_NAME for class: groovy.lang.Binding

Thanks in advance for your help.

1

There are 1 best solutions below

0
schande On

I just fount about the keywords buildingTag() and not to accomplish my requirements. https://www.jenkins.io/doc/book/pipeline/syntax/

stage ('Do stuff'){
    when {
        not {
            buildingTag()
        }
    }
    steps {
        sh'''#!/bin/bash
           ...
        '''
    }
}

Here the script inside the step block will only be executed if the current build has no tag.