Regex in Jenkins pipeline Regex in Jenkins pipeline jenkins jenkins

Regex in Jenkins pipeline


You can use String.startsWith(str) that returns true if env.GERRIT_PROJECT starts with platform/zap.

stage ('zap') {    when {        expression {            env.GERRIT_PROJECT?.startsWith("platform/zap")        }    }    steps {        build job: 'Zap', parameters: [            string(name: 'ZAP_PROJECT', value: env.GERRIT_PROJECT)        ]    }}

To avoid NPE if env.GERRIT_PROJECT is null for some reason, you can use NPE-safe operator ?. to invoke startsWith method.

The alternative solution that uses Groovy's exact match operator with regex could look like this:

stage ('zap') {    when {        expression {            env.GERRIT_PROJECT ==~ /^platform\/zap(.*)$/        }    }    steps {        build job: 'Zap', parameters: [            string(name: 'ZAP_PROJECT', value: env.GERRIT_PROJECT)        ]    }}


https://jenkins.io/doc/book/pipeline/syntax/
Section environment:

environment
Execute the stage when the specified environment variable is set to the given value, for example: when { environment name: 'DEPLOY_TO', value: 'production' }

Maybe this can help?