Jenkins 字符串插值凭据

Jenkins string interpolation with credentials

我正在 Jenkins 中编写这个函数来查询 Artifactory:

def curlDockerArtifact(URL, registryName, moduleName, tag, token) {
 def controlURI = "${URL}/artifactory/api/storage/${registryName}/${moduleName}/${tag}"
 def result = sh(script: """
                         curl -I -H \'Authorization: Bearer $token\' \
                         https://$controlURI -o /dev/null -w \'%{http_code}\' -s
                         """, returnStdout: true)
}

但是我收到了这个我想避免的警告。

Warning: A secret was passed to "sh" using Groovy string interpolation, which is insecure.

我尝试使用单引号,但无法从 Groovy 中正确解释变量。知道如何 fix/refactor 代码吗?

你必须像你一样保留双引号,但你需要转义令牌的 $ 符号。像这样:

curl -I -H \'Authorization: Bearer $token\'

groovy 不会插入变量,正确的值将在 shell 级别传递。

更多信息:https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#interpolation-of-sensitive-environment-variables