jenkins - Can I check if Environment variable exist or not in Jenkinsfile -


i running multibranch pipeline project.

the behaviour of jenkinsfile should change according trigger. there 2 events triggeres pipeline 1. push event 2. pull request.

i trying check environment variable 'change_id' ('change_id' available pull request only).reference .

so if pipeline triggred push event , if check 'change_id' variable throws exception (code works fine if pipeline gets triggered pull request).

code:

stage('groovytest'){     node('mynode1') {         if (change_id!=null){             echo "this pull request"         }else{             echo "this push request"         }     } } 

error:

groovy.lang.missingpropertyexception: no such property: change_id class: groovy.lang.binding     @ groovy.lang.binding.getvariable(binding.java:63)     @ org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.sandboxinterceptor.ongetproperty(sandboxinterceptor.java:224)     @ org.kohsuke.groovy.sandbox.impl.checker$4.call(checker.java:241)     @ org.kohsuke.groovy.sandbox.impl.checker.checkedgetproperty(checker.java:238)     @ org.kohsuke.groovy.sandbox.impl.checker.checkedgetproperty(checker.java:221)     @ org.kohsuke.groovy.sandbox.impl.checker.checkedgetproperty(checker.java:221)     @ com.cloudbees.groovy.cps.sandbox.sandboxinvoker.getproperty(sandboxinvoker.java:28)     @ com.cloudbees.groovy.cps.impl.propertyaccessblock.rawget(propertyaccessblock.java:20)     @ workflowscript.run(workflowscript:5)     @ ___cps.transform___(native method) 

how can check 'change_id' variable exist or not in jenkinsfile?

you may check before use it:

 if (env.change_id) {  ... 

from doc

environment variables accessible scripted pipeline, example: env.path or env.build_id. consult built-in global variable reference complete, , date, list of environment variables available in pipeline.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -