繁体   English   中英

在 Jenkins 管道 groovy 脚本中转义 git 密码的特殊字符

[英]Escape special character of git password in Jenkins pipeline groovy script

Jenkins 管道在我执行 git 拉“http:{username}:{password}@myrepo.github.com”的阶段失败。

根据密码策略,密码中有一个@。

pipelinescript.groovy 下面

def credId = '存储在 Jenkins 凭据中的凭据的 ID'

stage('Some Stage'){
   withCredentials([usernamePassword(credId: "${cred}", passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME']){
 sh """
  git checkout ${myBranch}
  git status
  
  git pull https://${GIT_USERNAME}:${GIT_PASSWORD}@github.myorg.com/myrepo.git
 """
  }

}

GIT_PASSWORD 是“@hello”

阶段失败,错误无法解析主机:4hello@github.myorg.com

我无法将 @ 编码为 %40 并将密码硬编码在上述 url 中。

我尝试了以下代替 GIT_PASSWORD

回声-n $GIT_PASSWORD | od -A n -t x1 | sed 's/ /%/g' 给出 %40%68%65%6c%6c%6f

git pull https://${GIT_USERNAME}:$(echo -n $GIT_PASSWORD | od -A n -t x1 | sed 's/ /%/g')@github.myorg.com/myrepo.git

不确定是否需要或已在我的组织中安装了用于凭据帮助程序的插件,但尝试了

sh "git config --global credential.helper \"!echo password=${GIT_PASSWORD}; echo\""

尝试了许多在线发布的东西,无法获得有效的解决方案。 请帮忙。

像下面这样:

git branch: 'master',
    credentialsId: 'your-credential-id',
    url: 'ssh://git@github.myorg.com/myrepo.git'

或者

仅使用结帐 scm 阶段怎么样。 这样您就不必担心密码中的特殊字符。

stage('Some Stage') {
  steps {
   checkout([$class: 'GitSCM', 
    branches: [[name: '*/master']], 
    doGenerateSubmoduleConfigurations: false, 
    extensions: [[$class: 'CleanCheckout']], 
    submoduleCfg: [], 
    userRemoteConfigs: [[credentialsId: 'your-credential-id', url: 'https://github.myorg.com/myrepo.git']]
  ])
 }
}

上述阶段将检查主分支。 如果您想结帐到其他分支,只需将文本替换为您要结帐的分支名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM