使用来自声明性管道的 jenkins 凭据进行 Git 推送

标签 git jenkins jenkins-pipeline git-push

我正在使用 jenkins 管道(声明性 synthax)并且我想将提交推送到我的远程存储库。

有什么方法可以使用 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

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

最佳答案

我们终于想通了。
问题很简单,我们的密码中有特殊字符会破坏 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 凭据进行 Git 推送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55970749/

相关文章:

jenkins-cli 未使用提供的 ssh 私钥进行身份验证

junit - 如何在 Jenkins 中使用 Email-ext 获取 Junit 测试结果

api - Jenkins - 如何使用 curl、http 发送 boolean 参数?

ansible - Jenkins 管道与 terraform - 如何将输出变量馈送到以下 Ansible 阶段

git - 删除 git 历史记录中具有多个分支的特定提交?

git - 'git pull origin mybranch' 在 origin 之前留下本地 mybranch N 提交。为什么?

jenkins - 在 Jenkins 脚本管道中通过动态键获取 env 属性

jenkins - 如何在另一个jenkins管道B中调用jenkins管道A

git - 你如何将 “update” 藏起来?

git - 在代理模式下使用两个不同的凭证处理两个不同的Git Organization存储库