Parameter not recognized in a Jenkinsfile method

I am doing a method call :

notifyGerrit('Success')

and the method definition is

def notifyGerrit(String msg) { 
    sh ''' 
       ssh -i ${JENKINS_PUB_KEY} -o UserKnownHostsFile=/dev/null \ 
           -p 12345 gerrit.abc.com gerrit review \ 
              ${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER} \ 
           --label ${msg} \ -m "'Successful: ${BUILD_URL}'" 
      ''' 
}

When this gets executed, the --label gets empty value

+ ssh -i **** -o UserKnownHostsFile=/dev/null -p 12345 gerrit.abc.com gerrit review 4134,1 --label -m ''\'' Successful: https://abc.gerrit.com/'\''' fatal: Label vote missing '=': -m

I tried using {msg} msg $msg ${msg} but none of these work. How do I get the value of the parameter msg inside the block of code. Note that other variables like ${BUILD_URL} etc coming from the environment works.

Upvotes: 0

Views: 340

Answers (1)

Szymon Stepniak
Szymon Stepniak

Reputation: 42174

If you want to interpolate $msg variable with the string you need to use double quotes instead of single quotes. Something like this:

def notifyGerrit(String msg) { 
    sh """ 
       ssh -i ${JENKINS_PUB_KEY} -o UserKnownHostsFile=/dev/null \ 
           -p 12345 gerrit.abc.com gerrit review \ 
              ${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER} \ 
           --label ${msg} \ 
           -m \\"'Successful: ${BUILD_URL}'\\" 
      """ 
}

Upvotes: 2

Related Questions