(Note: I have looked at similar questions (such as this where the problem is failure to trim output from shell commands) but I think this case is distinct.)
I have a pipeline script in Groovy that uses parameters (via properties([parameters([...
). When I interpolate the value of the parameter into a double quoted string, it fails a .equals()
check against both captured (by which I mean "captured and .trim()d!") stdout (which is my use case) and even a simple string literal.
I can work around the problem using .trim()
, (even though you can see, both through my echoing them and checking the .length()
, that there is nothing to .trim()
), but I suspect that only "works" because it does an implicit .toString()
--which is also a successful workaround.
This looks like a bug to me, but it's literally my first week working with Groovy, so perhaps I'm missing something--can anyone explain what?
Even a simple literal "foo"
fails (i.e. "foo".equals("${params.foo_the_parameter}"
). Is the interpolated parameter some other kind of object or something?
[EDIT After getting the answer from @Matias Bjarland, I modified the code below to use println instead of shelled echo because it makes the output more concise. The solution he suggested is reflected in a commented block.]
My groovy code:
node() {
properties([
parameters([
string(
defaultValue: 'foo',
description: 'This is foo',
name: 'foo_the_parameter'
)
])
])
/* this is what I learned from the accepted answer
bob="${params.foo_the_parameter}"
println("class of interpolated param is ${bob.class}")
simple_foo="foo"
println("class of \"foo\" is ${simple_foo.class}")
*/
echoed_foo = sh(script:"echo 'foo'", returnStdout: true).trim()
println "echoed foo is [$echoed_foo], params foo is [${params.foo_the_parameter}]";
echo_foo_length = echoed_foo.length()
dqs_foo_length = "${params.foo_the_parameter}".length()
println "their lengths are: echo: [$echo_foo_length] and dqs: [$dqs_foo_length]";
if (echoed_foo.equals("${params.foo_the_parameter}")) {
println "SUCCESS they are equals()"
}
else {
println "FAIL they are not equals()" //this one fires
}
if (echoed_foo.equals("${params.foo_the_parameter}".trim())) {
println "SUCCESS they are equals() after the dqs gets a trim()" //this one fires
}
else {
println "FAIL they are not equals()after the dqs gets a trim()"
}
if (echoed_foo.equals("${params.foo_the_parameter}".toString())) {
println "SUCCESS they are equals() after the dqs gets a toString()" //this one fires
}
else {
println "FAIL they are not equals()after the dqs gets a toString()"
}
if ("foo".equals("${params.foo_the_parameter}")) {
println "SUCCESS at least a simple literal \"foo\" works"
}
else {
println "FAIL even a simple literal \"foo\" fails to be .equals() with the interpolated parameter" //this one fires
}
}
Jenkins output:
Started by user Michael South
[Office365connector] No webhooks to notify
Obtained jenkins.groovy from git git@github.redacted.com:msouth/test_groovy_equals.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on subnet_mon_02 in /opt/jenkins/m1/workspace/field-analytics-org/test_string_equals
[Pipeline] {
[Pipeline] properties
[Pipeline] sh
[test_string_equals] Running shell script
+ echo foo
[Pipeline] echo
echoed foo is [foo], params foo is [foo]
[Pipeline] echo
their lengths are: echo: [3] and dqs: [3]
[Pipeline] echo
FAIL they are not equals()
[Pipeline] echo
SUCCESS they are equals() after the dqs gets a trim()
[Pipeline] echo
SUCCESS they are equals() after the dqs gets a toString()
[Pipeline] echo
FAIL even a simple literal "foo" fails to be .equals() with the interpolated parameter
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
[Office365connector] No webhooks to notify
Finished: SUCCESS