Using withEnv and withCredentials together

Viewed 977

Suppose you have a scenario where you have a username and password credential that you would like to use as part of populating the (de-facto) standard http_proxy environment variable. The first attempt would be something like this:

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

However, while that works, Jenkins flags it as unsafe. This doesn't work either:

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

This is because there is no "shell expansion" in a withEnv, and thus http_proxy is set to have a literal ${PROXY_CREDENTIALS} in it, which is not what we want.

Any ideas on how to set up http_proxy safely?

1 Answers

Try this :

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