jenkins - 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
任何输入都会有所帮助。
谢谢 !
解决方案
这个问题在后面的问题中得到了回答。答案描述了如何使用 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 插件,但我不知道这是否有效,因为我无法对其进行测试。
推荐阅读
- azure-devops - 在 Azure DevOps 中成功发布之前,如何阻止 PR 完成?
- mysql - 与临时 docker 容器一起使用的命令
- css - SVG 蒙版或 CSS 样式可消除 svg 的中心,使其透明
- python - 对二维空间中的矩形进行编号以满足单调性
- python - xarray 计算来自多年 netcdf 的月平均值
- swift - EquatableView 不强制 SwiftUI 使用 == 函数的自定义实现
- c# - 控制台应用.net core 3.1配置问题看不到Appsettings.Development.json
- javascript - 如何使用 JavaScript 变量添加背景图片
- time-complexity - 如何解决给定代码块示例的 O-Notation?
- javascript - 为什么当我在本地存储中添加新产品时,他会被新产品替换