【问题标题】:Git push using jenkins credentials from declarative pipeline使用来自声明性管道的 jenkins 凭据进行 Git 推送
【发布时间】:2019-09-22 01:20:28
【问题描述】:

我正在使用 jenkins 管道(声明式合成器),我想将提交推送到我的远程存储库。

有没有办法使用 git 插件来完成这个? 这是我目前正在尝试的:

withCredentials([usernamePassword(credentialsId: "${GIT_CREDENTIAL_ID}", passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
                        sh "git add ${BRANCH_RENAME}.bundle"
                        sh "echo ${GIT_USERNAME}|||||||${GIT_PASSWORD}"
                        sh "git tag -a backup -m 'Backup branch ${BRANCH} from vega-salesforce to vega-salesforce-backup' "
                        sh('git push https://${GIT_USERNAME}:${GIT_PASSWORD}@${GIT_URL_WITHOUT_HTTPS} --tags')
                    }

但它不起作用。 我收到以下错误:`

fatal: unable to access 'https://****:****@myrepositoryurl/mygitgroup/salesforce-backup/': Could not resolve host: ****:clear_password_here; Name or service not known

有人可以帮忙吗? 我虽然问题出在我的密码中的特殊字符,但我不确定。

【问题讨论】:

  • 使用 Git Pipeline Plugin 可能会更容易。
  • jenkins 管道中没有用于 push 的 git 插件。有吗?
  • 我们通过 url 编码密码解决了这个问题。问题是 GIT_PASSWORD 包含应该编码的特殊字符

标签: git jenkins jenkins-pipeline git-push


【解决方案1】:

我们终于弄明白了。 问题很简单,就是我们的密码中有特殊字符会破坏 url。

这是工作代码:

withCredentials([usernamePassword(credentialsId: env.GIT_CREDENTIAL_ID, usernameVariable: 'USER', passwordVariable: 'PASS')]) {
                    script {
                        env.encodedPass=URLEncoder.encode(PASS, "UTF-8")
                    }
                    sh 'git clone https://${USER}:${encodedPass}@${GIT_URL} ${DIR} -b ${BRANCH}'
                    sh 'git add .'
                    sh 'git commit -m "foobar" '
                    sh 'git push'
                } 

【讨论】:

  • 我认为这个解决方案只有一个问题。对密码进行编码后,jenkins 将输出不带掩码 (****) 的值。此问题的一种可能解决方案是disable the command echoing。我看到的另一个解决方案是 use the MaskPasswords plugin,但我不知道它是否有效,因为我无法对其进行测试。
  • @PedroPinheiro ,感谢您指出这一点!
【解决方案2】:

您不能使用 username:password 在脚本中连接到 git repo。

您应该使用 ssh 密钥。更多信息请查看this answer

【讨论】:

  • 由于公司限制,我无法使用 ssh。
  • 我们通过 url 编码密码解决了这个问题。问题是 GIT_PASSWORD 包含应该编码的特殊字符
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-10
  • 2019-10-03
  • 1970-01-01
相关资源
最近更新 更多