Can I check if Environment variable exist or not in Jenkinsfile
Solution 1
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.
Solution 2
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 } }
Solution 3
You can also use the changeRequest()
function in the when clause to check for PR:
when {
anyOf {
changeRequest() // if pull request
branch 'master'
branch 'release/*'
}
}
Related videos on Youtube
Comments
-
kishs1991 over 3 years
I am running Multibranch pipeline for my project.
The behaviour of Jenkinsfile should change according to the trigger. There are two events that triggeres the pipeline 1. Push event 2. Pull Request.
I am trying to check Environment variable 'CHANGE_ID' ('CHANGE_ID' will be available for Pull Request only).Reference .
So if pipeline is triggred by Push event and If check the 'CHANGE_ID' variable it throws exception (code works fine if pipeline gets triggered by Pull Request).
Code:
stage('groovyTest'){ node('mynode1') { if (CHANGE_ID!=NULL){ echo "This is Pull request" }else{ echo "This is Push request" } } }
Error:
groovy.lang.MissingPropertyException: No such property: CHANGE_ID for class: groovy.lang.Binding at groovy.lang.Binding.getVariable(Binding.java:63) at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:224) at org.kohsuke.groovy.sandbox.impl.Checker$4.call(Checker.java:241) at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:238) at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:221) at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:221) at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:28) at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20) at WorkflowScript.run(WorkflowScript:5) at ___cps.transform___(Native Method)
How can I check the 'CHANGE_ID' variable exist or not in Jenkinsfile?
-
aristotll over 6 years@kishs1991 Glad it helps, you can accept my answer (*≧∪≦)
-
Jorge Nunez Newton over 5 yearsIf not working the script tag can be used: "script { if(env.CHANGE_ID){"
-
anthony sottile over 5 yearsfor completeness, the value of
env.DOES_NOT_EXIST
isnull
-
frank over 3 yearsI have one additional question what in case when we have i.e. 10 or more variables to check and all of them are named starting from the same word (BUILD_ID, BUILD_NUMBER, BUILD_DATE,... etc). It is possible to use some kind of sentence as below?: if (env.BUILD_*) { ...
-
MikeyT about 3 yearsYes, thank you! Using
changeRequest
did not always do what I expected. Using the env vars is what I needed. -
Chris F almost 2 yearsThis does NOT check if the value exists, but rather a check if the string is not empty.
if (env.CHANGE_ID != null)
is the correct check.