Danny Vriens
Danny Vriens

Reputation: 45

Problem with escaping double quote Jenkinsfile

I start building a Jenkins job with a Jenkinsfile. In one of my steps, I create a variable. This variable echo's as expected, now I try to insert this variable into a new function and need to escape, everything looked fine until I see the last double quote on a new line and not on the end.

The line of code that create my variable is:

gitTag = sh(returnStdout: true, script: "git describe --tags--always")

The line code that use this variable:

sh "echo WEBSERVICE_VERSION=\"$gitTag\" > ${WORKSPACE}/webservice/src/webservice_version.py"

expected output WEBSERVICE_VERSION="$gitTag", but the output I see is WEBSERVICE_VERSION=$gitTag.

Did I make any mistake in escaping?

Upvotes: 1

Views: 5886

Answers (1)

Ram
Ram

Reputation: 1224

This should work for you

node('master'){
def gitTag="mytag"
sh "touch webservice_version.py"
sh "echo \\\"${gitTag}\\\"  > webservice_version.py"
sh "cat webservice_version.py"
}

Upvotes: 1

Related Questions