HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

In Jenkins, how can parameters that contain spaces be referenced?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
canreferencedjenkinscontainspacesthathowparameters

Problem

Google has failed to even lead me to a proper answer as to why spaces are permitted in names of Jenkins parameters, let alone how to reference them. Assuming it's not just a major oversight, how does one access these parameters?

For example, I have a string parameter named "IP Address". I could rename it "IP_Address" but I'm trying to avoid the underscores_as_spaces if I can.

I've tried to refer to this parameter using "${IP Address}", "IP Address" and even "IP\ Address" but nothing seems to work. Is there any way to actually reference these?

Solution

You can use params['IP Address'].

Think of params as a Map containing a key 'IP Address'. If the key didn't have a space, then you could use params.IPAddress or params['IPAddress'], but when there's a space, you can only use the latter syntax.

That syntax is also useful when you're computing the key name to look up, and have stored it in a variable:

final String myParam = "param${i}"
// can't say `params.myParam`
echo "Value is ${params[myParam]}"

Code Snippets

final String myParam = "param${i}"
// can't say `params.myParam`
echo "Value is ${params[myParam]}"

Context

StackExchange DevOps Q#4711, answer score: 5

Revisions (0)

No revisions yet.