pkaramol
pkaramol

Reputation: 19382

Jenkinsfile when condition that contains branch pattern does not apply

I want by pipeline to run on any branch that matches the pattern alerta/ (and anything beyond the slash (/).

So I have tried the following 2 approaches in terms of Jenkinsfile

          expression {
              env.BRANCH_NAME ==~ 'alerta\/.*'
          }
          expression {
              env.BRANCH_NAME == '*/alerta/*'
          }

both of them have the corresponding stages being skipped.

How should I format my when conditional to meet my purpose?

Upvotes: 1

Views: 1464

Answers (1)

Daniel Taub
Daniel Taub

Reputation: 5379

You forgot to return the state in the expression

expression

Execute the stage when the specified Groovy expression evaluates to true, for example: when { expression { return params.DEBUG_BUILD } } Note that when returning strings from your expressions they must be converted to booleans or return null to evaluate to false. Simply returning "0" or "false" will still evaluate to "true".

pipeline {
    agent any
    stages {
        stage('alerta-branch'){
            when{
                expression {
                    return env.BRANCH_NAME ==~ /alerta\/.*/
                }
            }
            steps {
                echo 'run this stage - ony if the branch contains alerta in branch name'
            }
        }
    }
}

This should work for you

Upvotes: 2

Related Questions