首页 > 解决方案 > Jenkins Credential 插件使用 Pipeline groovy 脚本正确解析用户名和密码

问题描述

我正在尝试使用 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

任何输入都会有所帮助。

谢谢 !

标签: jenkinsgroovyjenkins-pluginsjenkins-pipeline

解决方案


这个问题在后面的问题中得到了回答。答案描述了如何使用 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 将输出不带掩码 (****) 的值。此问题的一种可能解决方案是禁用命令 echoing。我看到的另一个解决方案是使用 MaskPasswords 插件,但我不知道这是否有效,因为我无法对其进行测试。


推荐阅读