Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to concatenate strings in a Jenkinsfile?

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?

like image 430
cirpo Avatar asked Aug 06 '16 10:08

cirpo


People also ask

How do I concatenate in a groovy script?

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.

What is string concatenation example?

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".

What is AC Net string concatenation?

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.


1 Answers

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.

like image 178
mbanchero Avatar answered Sep 28 '22 00:09

mbanchero