使用声明性管道中的jenkins凭证进行Git推送 [英] Git push using jenkins credentials from declarative pipeline

查看:441
本文介绍了使用声明性管道中的jenkins凭证进行Git推送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jenkins管道(声明式synthax),并且想将提交推送到远程存储库中.

I'm using jenkins pipeline (declarative synthax) and I want to push a commit to my remote repository.

有什么办法可以使用git插件来做到这一点吗? 这是我目前正在尝试的方法:

Is there any way to accomplish this using the git plugin? Here is what I'm currently trying:

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

但是它不起作用. 我收到以下错误:

But it does'nt works. I got the following error:`

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

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

Could anyone help please? I though the issue comes from the special characters present in my password but I'm not sure.

推荐答案

我们终于弄清楚了. 问题很简单,我们在密码中包含特殊字符,这些特殊字符会破坏网址.

We finally figure it out. The problem was simply that we have special characters in our password which break out the 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推送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆