Can I check if Environment variable exist or not in Jenkinsfile
This is how it would look like for a declarative pipeline:
pipeline {
// ...
stages {
// ...
stage('Build') {
when {
allOf {
expression { env.CHANGE_ID != null }
expression { env.CHANGE_TARGET != null }
}
}
steps {
echo "Building PR #${env.CHANGE_ID}"
}
}
}
}
To run a stage only when not building a PR:
when { expression { env.CHANGE_ID == null } }
You may check it before use it:
if (env.CHANGE_ID) {
...
From the doc
Environment variables accessible from Scripted Pipeline, for example:
env.PATH
orenv.BUILD_ID
. Consult the built-in Global Variable Reference for a complete, and up to date, list of environment variables available in Pipeline.