13

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?

cirpo
  • 902
  • 1
  • 9
  • 11

2 Answers2

6

Does this work?

versionFromFile = readFile("./version")
sh "git tag ${versionFromFile}-${env.BUILD_NUMBER}"

assuming that versionFromFile is read from the file correctly of course. To test that isn't your problem, you could just define it like this

versionFromFile = "99"
sh "git tag ${versionFromFile}-${env.BUILD_NUMBER}"
Mark Chorley
  • 2,087
  • 2
  • 22
  • 29
6

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.

mbanchero
  • 134
  • 1
  • 10