Cloudbees 2.289.1.2
I am changing the code as per the Jenkinsfile string interpolation guidelines. Note: I am aware that Groovy String interpolation expects double-quotes for variables to be included.
There are several sh in several global variables(under vars/) in a shared library.
Currently(without using String interpolation directives by Jenkins), the commands specified below give a warning(shown after each command).
First command: This command can't populate custom/user-defined variables but it seems that username and password are populated:
String resultingImage = "${repo}/${parameters.openshiftProject}/${appImageName}"
.toLowerCase()
// tag images
withCredentials([usernamePassword(
credentialsId: pushCredentialsId,
passwordVariable: 'password',
usernameVariable: 'username')]) {
parameters[IMAGE_TAGS].each { tagName ->
sh 'set +x skopeo copy docker://$resultingImage:latest docker://$resultingImage:$tagName --src-tls-verify=false --src-creds=$username:$password --dest-creds=$username:$password echo "done copying ${resultingImage}:${tagName}"'
}
}
Output:
[Pipeline] withCredentials
Masking supported pattern matches of $username or $password
[Pipeline] {
[Pipeline] sh
+ set +x skopeo copy docker://:latest docker://: --src-tls-verify=false '--src-creds=****:****' '--dest-creds=****:****' echo 'done copying :'
If double-quotes are used, the following warning is shown but the command is formed correctly:
[Pipeline] {
[Pipeline] sh
Warning: A secret was passed to "sh" using Groovy String interpolation, which is insecure.
Affected argument(s) used the following variable(s): [password, username]
See https://jenkins.io/redirect/groovy-string-interpolation for details.
+ set +x
Second command: This throws an error(seems that the credentials are retrieved correctly), no matter if I use "${chartTarballName}" or ${chartTarballName} or $chartTarballName:
sh 'curl -f -u $username:$password -T "${chartTarballName}" $chartPublishRepo/$chartTarballName'
The error:
[Pipeline] sh
+ curl -f -u ****:**** -T '' /
curl: (3) <url> malformed
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
ERROR: script returned exit code 3
If double-quotes are used, the following warning is shown but the command is formed correctly:
Warning: A secret was passed to "sh" using Groovy String interpolation, which is insecure.
Affected argument(s) used the following variable(s): [password, username]
See https://jenkins.io/redirect/groovy-string-interpolation for details.
+ curl -f -u ****:**** -T exp-calculator-0.14.11.tgz https://repository.net/artifactory/stable-local/exp-calculator-0.14.11.tgz
I guess that these interpolation rules work only within a declarative pipeline directive, and not for scripted pipelines, global variables(at least, the user-defined variables) in a shared library, and other Groovy DSL scripts.
Update-1:
The single-quote seems to work only for the standard, directive pipeline directives:
pipeline {
environment {
CUSTOM_ENV_VAR = 'This is an environment variable'
}
agent any
stages {
stage('build') {
steps {
withCredentials([string(credentialsId: 'sq_token', variable: 'SECRET')]) {
sh ("echo $SECRET")
sh ('echo $SECRET $CUSTOM_ENV_VAR')
script {
String scriptBlockVariable = 'This is a variable in a script block'
sh ('echo $scriptBlockVariable')
sh ('echo $SECRET $scriptBlockVariable')
}
}
}
}
}
}
Output:
[Pipeline] stage
[Pipeline] { (build)
[Pipeline] withCredentials
Masking supported pattern matches of $SECRET
[Pipeline] {
[Pipeline] sh
Warning: A secret was passed to "sh" using Groovy String interpolation, which is insecure.
Affected argument(s) used the following variable(s): [SECRET]
See https://jenkins.io/redirect/groovy-string-interpolation for details.
+ echo ****
****
[Pipeline] sh
+ echo **** This is an environment variable
**** This is an environment variable
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ echo
[Pipeline] sh
+ echo ****
****
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // stage