I'm trying to concatenate some strings in a Jenkinsfile but it's not working:
def versionFromFile = readFile("./version")
def version = versionFromFile + env.BUILD_NUMBER
I tried other solutions as well:
sh "echo version: ${version}-${env.BUILD_NUMBER}"
sh "git tag ${version}-${env.BUILD_NUMBER}"
but ${env.BUILD_NUMBER} is not evaluated/printed
if I do
sh "git tag 1.0.1-${env.BUILD_NUMBER}"
${env.BUILD_NUMBER} is evaluated/printed
I still don't get how the scripting language works inside the Jenkinsfile, the documentation is all about the DSL, does that means that you can't do common scripting operations?
The concatenation of strings can be done by the simple '+' operator. Parameters − The parameters will be 2 strings as the left and right operand for the + operator.
In formal language theory and computer programming, string concatenation is the operation of joining character strings end-to-end. For example, the concatenation of "snow" and "ball" is "snowball".
Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.
Jenkinsfiles follows the same syntax as Groovy language (with some exceptions). See Jenkins syntax
The way to concatenate strings in a Jenkinsfile is using the plus character ("+"). For example:
VAR1 = "THIS IS"
VAR2 = 4
RESULT = VAR1 + " " + VAR2 + " " + PARAM
echo "$RESULT"
Then if PARAM
is an input parameter with the value "YOU" then the printed output is:
"THIS IS 4 YOU"
Then regarding your problem with environment variable ${env.BUILD_NUMBER}
try to simply use BUILD_NUMBER
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With