Issue
I am trying to run a script to dictate whether a Jenkins step runs. This script needs credentials, which we access by calling withCredentials
.
Here's a slightly reduced example:
stage('Sample test stage') {
when {
expression {
withCredentials([usernamePassword(credentialsId: 'MY_CREDENTIALS_ID', usernameVariable: 'MY_USERNAME', passwordVariable: 'MY_TOKEN')]) {
return sh([returnStdout: true, script: 'node bin/my-script ${MY_TOKEN}']).trim().startsWith('success')
}
}
}
steps {
echo 'I should only run when the script passes'
}
}
However, this doesn't work. Jenkins returns the error: "syntax error: bad substitution."
I'm assuming it's down to the syntax used when trying to pass in the credentials. I've tried a couple of variations, such as returning withCredentials
and wrapping the whole expression in withCredentials
, but both of them had fairly dramatic errors.
I cannot find any documentation on this, which makes me wonder if it's illegal
Solution
I feel quite silly. There's a difference between "
and '
in Groovy. Within single quotes, only Bash substitutions will work. Within double quotes, both Groovy and Bash substitutions will work.
Answered By - Jodi Warren
Answer Checked By - Candace Johnson (JavaFixing Volunteer)