【问题标题】:Jenkins Credential plugin to resolve username and password with @ correctly using Pipeline groovy scriptJenkins Credential 插件使用 Pipeline groovy 脚本正确解析用户名和密码
【发布时间】:2019-03-02 18:55:24
【问题描述】:

我正在尝试使用 Jenkins Credential 插件连接到 github

withCredentials([usernamePassword(credentialsId: gitCredential, passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
        sh("git push https://${GIT_USERNAME}:${GIT_PASSWORD}@stash.abc.com:656/rad/abl/optical.git --tags")
    }

我正在尝试推入 Git,但由于密码包含@而失败并出现以下错误。由于我们无法对从 Jenkins 凭据插件获得的密码进行 urlEncode。我正在寻找一种正确解析用户名和密码的方法。

[Tagging] Running shell script
+ git push 'https://****:****@stash.abc.com:656/rad/abl/optical.git' --tags
fatal: unable to access 'https://****:ZxmP*K@v6iO/?w4ms@stash.abc.com:656/rad/abl/optical.git': Couldn't resolve host 'v6iO'
[Pipeline] }
[Pipeline] // withCredentials

任何意见都会有所帮助。

谢谢!

【问题讨论】:

  • 哦,凭证密码中的特殊字符似乎是一个常见的烦人问题。如果您将源设置为您的推送 URL 并使用 git 凭证助手,这会给您带来任何好处吗?我还没有尝试过,所以觉得我不能把它作为答案。这里有一些细节值得一看:alanedwardes.com/blog/posts/…
  • 谢谢@macg33zr,这确实有帮助:)

标签: jenkins groovy jenkins-plugins jenkins-pipeline


【解决方案1】:

later question 回答了这个问题。 answer 描述了如何使用 URLEncoder 命令对密码中的特殊字符进行编码。

在你的情况下,它会是这样的:

withCredentials([usernamePassword(credentialsId: gitCredential, passwordVariable: "GIT_PASSWORD", usernameVariable: "GIT_USERNAME")]) {
    script {
        env.URL_ENCODED_GIT_PASSWORD=URLEncoder.encode(GIT_PASSWORD, "UTF-8")
    }
        sh "git push https://${GIT_USERNAME}:${URL_ENCODED_GIT_PASSWORD}@stash.abc.com:656/rad/abl/optical.git --tags"
}

我认为这个解决方案只有一个问题。对密码进行编码后,jenkins 将输出不带掩码 (****) 的值。此问题的一种可能解决方案是disable the command echoing。我看到的另一个解决方案是use the MaskPasswords plugin,但我不知道这是否有效,因为我无法对其进行测试。

【讨论】:

    猜你喜欢
    • 2022-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多