Reputation: 2328
Please help. env
is null.
Jenkinsfile:
node {
println "Your ENV:"
println env.dump
println "Your params:"
println params.dump
}
Jenkins output:
[Pipeline] properties
[Pipeline] node
Running on foobarquux in c:\workspace\123abc
[Pipeline] {
[Pipeline] echo
Your ENV:
[Pipeline] echo
null
[Pipeline] echo
Your params:
[Pipeline] echo
null
I expect that my environment variables will not be null. I expect env.dump
not to be null and to see something beyond Your ENV:
when println env.dump
executes.
After reading very helpful comments from @mkobit, I realized I needed parentheses for dump
, and even with them Jenkins throws a security exception.
Upvotes: 1
Views: 7191
Reputation: 5742
Thanks to StephenKing for pointing out, i check again with a new fresh Jenkins instance. see comments inside
Assuming the job has 2 parameters [str1=val1, bool1=true] :
node {
// Print the value of a job parameter named "str1"
// output: val1
println "${params.str1}"
// Calling the dump() function to print all job parameters (keys/vals)
// NOTE: calling this method should be approved by Jenkins admin
// output: .... m=[str1:val1, bool1:true] ...
println params.dump()
// Same as the above.
// output: .... m=[str1:val1, bool1:true] ...
println "${params.dump()}"
// SYNTAX ERROR, the '$' is not expected here by the parser
//println ${params.dump()};
// This appears in the question, but it seems like this is not
// what the author meant. It tries to find a param named "dump"
// which is not available
// output: null
println params.dump
}
Upvotes: -2
Reputation: 833
${WORKSPACE}
only works if it is used in an agent (node)! Otherwise it comes out as null
.
I have agent none
at the top of my pipeline because I have a few input
steps that i don't want use heavyweight executors for. And I was setting an environment variable in the top-level environment {}
block that used ${WORKSPACE}
. For the life of me I couldn't figure out why it was being set to null. Some other thread mentioned the workspace on an agent, so i moved that definition into a step on an agent, and lo and behold, when you set a var with WORKSPACE while running on an agent, it all works as expected.
The sidebar here is that if you are using a top-level agent none
, the environment
and presumably other pre-stages blocks are not running in an agent. So anything that relies on an agent will behave unexpectedly.
Upvotes: 7
Reputation: 47319
Groovy's optional parenthesis requires at least one parameter, which is different than Ruby.
Method calls can omit the parentheses if there is at least one parameter and there is no ambiguity:
So, to call the dump()
method you would do env.dump()
or params.dump()
. However, this method will not be whitelisted and you will get a security exception (if you are running in the sandbox or using any sort of Jenkins security) because this would print out all fields of the object.
Upvotes: 0