3

I have a jenkins declarative pipeline which I am interested to be able to perform a stage only if a specific environment variable contains a specific substring(not fully equals to it, just contains it). Does anyone got any idea on how can I implement it(maybe using the when condition if possible).

Thanks in advance, Alon

1 Answer 1

5

As you mentioned, in declarative pipeline you can use the when directive to establish a condition in which the stage will be executed.
Among the built in condition options like triggeredBy,branch and tag there is the generic expression option, which allows you to run any groovy code and calculate the relevant Boolean value according to your needs.
So for your case for example you can just use the groovy contains methods to achieve what you want, something like:

pipeline {
    agent any
    stages {
        stage('Conditional Stage') {
            when {
                expression { return env.MyParamter.contains('MySubstring') }
            }
            steps {
                echo "Running the conditional stage"
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.