8

In my multibranch pipeline job, I can successfully access environment variables like this:

echo "$env.BRANCH_NAME" 

But it throws and exception if I try to compare against that same environment variable:

if($env.BRANCH_NAME == 'master') { echo "This is the master branch" } 

Here's the top of the error stack I'm given:

groovy.lang.MissingPropertyException: No such property: $env 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 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:4) 

Do I need to do some sort of script approval here? I checked in Manage Jenkins -> In-Process Script Approval, but nothing is there.

3 Answers 3

8

Try to remove the dollar sign or use "${env.BRANCH_NAME}"

Sign up to request clarification or add additional context in comments.

1 Comment

it worked when I changed it to if("$env.BRANCH_NAME" == 'release')
4

The earlier answer is correct, but for more information, the "$var" syntax is what is used within a string to interpolate the variable value into the string. Outside of a string, just reference the variable.

Comments

0

Use the $ to interpolate variables, any of this options will work:

if(env.BRANCH_NAME == 'master') { echo "This is the master branch" } 

or

if("$env.BRANCH_NAME" == 'master') { echo "This is the master branch" } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.