use*_*723 15 jenkins jenkins-workflow
我目前正在尝试使用Jenkins Workflow脚本标记repo.我尝试过使用一个sh步骤,但由于没有设置凭据,这会遇到问题.
fatal: could not read Username for 'https://<repo>': Device not configured
Run Code Online (Sandbox Code Playgroud)
是否有现有步骤可用于标记回购或解决凭据问题?
use*_*723 19
我已经设法通过使用withCredentials凭证绑定插件提供的步骤来实现这一点.
它不是很好,因为它涉及在URL中指定它,但这些值在控制台输出中被屏蔽.
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'MyID', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]) {
sh("git tag -a some_tag -m 'Jenkins'")
sh("git push https://${env.GIT_USERNAME}:${env.GIT_PASSWORD}@<REPO> --tags")
}
Run Code Online (Sandbox Code Playgroud)
Mag*_*tel 13
这是一个不需要知道远程URL的替代方法:
try {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'MyID', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]) {
sh("${git} config credential.username ${env.GIT_USERNAME}")
sh("${git} config credential.helper '!echo password=\$GIT_PASSWORD; echo'")
sh("GIT_ASKPASS=true ${git} push origin --tags")
}
} finally {
sh("${git} config --unset credential.username")
sh("${git} config --unset credential.helper")
}
Run Code Online (Sandbox Code Playgroud)
这可以让git从配置中读取用户名,并让凭证助手只提供密码.最后的额外echo用于使git作为参数传递给helper的命令不会与密码在同一行上.
小智 7
如果您的git密码包含特殊字符,例如"%",":","@"或"/",则${env.GIT_PASSWORD}作为git url的一部分传递即https://${env.GIT_USERNAME}:${env.GIT_PASSWORD}@<REPO>不进行任何编码可能会导致Invalid username or password错误.
使用内联凭证保存任何麻烦都是一种更好的方法,但是建议!echo password=\$GIT_PASSWORD; echo'会在构建日志中产生警告,warning: invalid credential line: get因为credential.helper会传递一个参数来指示所需的操作(获取,存储,擦除) .在这种情况下,凭证助手正在尝试将get操作解释为凭证输入.有效输入是协议,主机,路径,用户名,密码,URL.请参阅https://git-scm.com/docs/git-credential#IOFMT
更好的内联凭证.helper将是!f() { echo password=\$GIT_PASSWORD; }; f这种方式,将get忽略credential.helper操作.
完整示例:
try {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'MyID', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]) {
sh("${git} config credential.username ${env.GIT_USERNAME}")
sh("${git} config credential.helper '!f() { echo password=\$GIT_PASSWORD; }; f'")
sh("GIT_ASKPASS=true ${git} push origin --tags")
}
} finally {
sh("${git} config --unset credential.username")
sh("${git} config --unset credential.helper")
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15945 次 |
| 最近记录: |