Hello in my config I want to get the last tag created (the last tag is created on develop by a CI step), but I always have an empty value and the echo give me the plain “${GIT_TAG}”.
The part of my script that doing what I search to do:
I also have defined the GIT_TAG env in the config of Jenkins with a default value, and now I get this default value, don’t know if it’s a good idea to set an env in jenkins too:
'single quote' text always remains a plain old String, "double quote" text becomes a GString if it contains the ${interpolations}, and performs the values substitution. See The Apache Groovy programming language - Syntax
def x = 123
def s1 = 'string ${x}'
def s2 = "string"
def g = "gstring ${x}"
println(s1)
println(s1.getClass())
println(s2)
println(s2.getClass())
println(g)
println(g.getClass())
string ${x}
class java.lang.String
string
class java.lang.String
gstring 123
class org.codehaus.groovy.runtime.GStringImpl
upd: So just to be clear; from what I see, you already store the output of sh() properly, it’s just the echo() call that is slightly wrong
That’s a slightly different manifestation of the same problem you. Single quotes → ${} is not evaluated → string is passed to the script as is. So you literally get a script file like
that gets executed via sh -ex /jenkins/workspace/jobname@tmp/script.sh.
Groovy variables do not automatically translate into environment variables that a shell process would see (withEnv() and withCredentials() do that). So from shell’s perspective GIT_TAG does not exist – and a variable that does not exist when referenced in shell evaluates into an empty string.
Same fix as before: replace '' with "" for anything where you want the Groovy to do the substitution.