Git push using jenkins credentials from declarative pipeline

后端 未结 2 1874
南方客
南方客 2020-12-20 17:26

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

相关标签:
2条回答
  • 2020-12-20 18:15

    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

    0 讨论(0)
  • 2020-12-20 18:16

    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'
                    } 
    
    0 讨论(0)
提交回复
热议问题