在詹金斯管道中为git转义@字符

时间:2019-08-15 08:22:45

标签: git jenkins

在我们的詹金斯构建过程中,我们试图将其推向git。我们的密码最近已更改,现在包含一个@符号,它会显示以下错误:

建议似乎是对密码进行编码,但是我无法弄清楚如何在我们的jenkins管道中执行此操作。我该怎么做呢? (我也尝试过使用replace方法将@符号替换为%40,但这没用。)

def GIT_PASSWORD_R = GIT_PASSWORD.replace('@', '%40') 

Escape @ character in git proxy password

def GIT_PASSWORD_R = GIT_PASSWORD.toURL()       
git push -f https://${GIT_USERNAME}:${GIT_PASSWORD_R}@github.company.com/Product/subProd.git ${VERSION}-SNAPSHOT

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。除了编码之外,对我有用的另一种选择是使用git凭据帮助器。查看此答案:https://stackoverflow.com/a/40038869/9463800

它将设置一个git凭证帮助器,执行git操作,并在finally块中取消设置密码。

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")
}