一起使用 withEnv 和 withCredentials

时间:2021-03-10 18:23:49

标签: jenkins jenkins-pipeline jenkins-groovy

假设您有一个场景,您有一个用户名和密码凭证,您想将其用作填充(事实上的)标准 http_proxy 环境变量的一部分。第一次尝试是这样的:

withCredentials([usernameColonPassword(credentialsId:'proxy-credentials', variable:'PROXY_CREDENTIALS')]) {
  def proxyUrl = "https://${env.PROXY_CREDENTIALS}@proxy-endpoint.com:3128"
  withEnv(["http_proxy=${proxyUrl}"]) {
    // Do stuff
  }
}

然而,虽然这样做有效,但 Jenkins 将其标记为不安全。这也不起作用:

withCredentials([usernameColonPassword(credentialsId:'proxy-credentials', variable:'PROXY_CREDENTIALS')]) {
  withEnv(['http_proxy=https://${PROXY_CREDENTIALS}@proxy-endpoint.com:3128']) {
    // Do stuff
  }
}

这是因为在 withEnv 中没有“shell 扩展”,因此 http_proxy 被设置为在其中包含文字 ${PROXY_CREDENTIALS},这不是我们想要的。< /p>

有关如何安全设置 http_proxy 的任何想法?

1 个答案:

答案 0 :(得分:0)

试试这个:

withCredentials([usernameColonPassword(credentialsId:'proxy-credentials', variable:'PROXY_CREDENTIALS')]) {
  withEnv(["http_proxy=https://$PROXY_CREDENTIALS@proxy-endpoint.com:3128"]) {
    // Do stuff
  }
}