Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git push using jenkins credentials from declarative pipeline

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.

like image 479
Géraud Willing B-S Avatar asked May 03 '19 13:05

Géraud Willing B-S


People also ask

What is the Jenkins Git plugin?

The Jenkins git plugin uses Jenkins credentials to fetch a repository and checkout a branch for freestyle, pipeline, and multibranch pipeline jobs. It is also able to use Jenkins credentials to push tags and commits back to the repository from a freestyle job.

How to allow Jenkins Pipeline users to run authenticated Git commands?

Allow Jenkins Pipeline users to run authenticated git commands in sh, bat, and powershell. This project idea proposes to implement two new credential bindings that contribute files and environment variables to sh, bat, and powershell steps so that they can use command line git to perform authenticated operations.

How do I access a secured Git repository in Jenkins?

Git steps to access a secured repository should provide a Jenkins credential with the credentialsId argument rather than embedding credentials in the URL. Credentials embedded in a repository URL may be visible in console logs or in other log files.

What is Git credentials binding in pipeline?

With the git credentials binding, Pipeline users will be able to push merge results, commits, and tags from a Pipeline job. They will be able to create and delete remote branches.


2 Answers

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'
                } 
like image 71
Géraud Willing B-S Avatar answered Sep 22 '22 20:09

Géraud Willing B-S


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

like image 28
ozlevka Avatar answered Sep 23 '22 20:09

ozlevka