Git push using jenkins credentials from declarative pipeline

浪子不回头ぞ 提交于 2020-05-15 09:39:20

问题


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

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.


回答1:


We finally figure it out. The problem was simply that we have special characters in our password which break out the url.

Here is the working code:

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



回答2:


You can't use username:password for connecting to git repo in script.

You should use ssh key. Please see this answer for more information



来源:https://stackoverflow.com/questions/55970749/git-push-using-jenkins-credentials-from-declarative-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!