2

I am trying to create a Jenkins job that will trigger a shell script via the option Send files or execute commands over SSH in Jenkins. The following is the main part of my shell script:

#!/bin/bash
BRANCH=$1
cd /vm/deployment
git clone https://[email protected]/myuser/proj.git
#updating the common property files
cd /vm/deployment/swcm-properties
git reset --hard HEAD
#git pull ${BRANCH}
git fetch && git checkout ${BRANCH}
git pull

My problem here is that the execution fails since I am unable to pass the password and username for the repository for the clone to work.

I found an option to set the username and password as global credentials and made the following configurations:

Configuration

When I try to execute the following shell script, which is saved on the server, I get the following error:

#!/bin/bash
git clone https://$uname:[email protected]/mysuer/myrepo.git

remote: Invalid username or password
fatal: Authentication failed for 'https://:@bitbucket.org/****/myrepo.git/'

What is the best approach to pass the username and password and trigger a git clone from a Bitbucket repository using Jenkins.

1
  • Hi Thomas, I have the similar issue. Did you resolved this? If so, please post the answer.
    – Vinodh Ram
    Commented Aug 20, 2020 at 16:35

1 Answer 1

1

Here is how I did it with Jenkins declarative.

The key here is to use URLEncoder.encode.

pipeline {
    environment {
        // bitbucketcredentials is stored at Jenkins Credentials Stored
        BITBUCKET_CREDENTIALS = credentials('bitbucketcredentials')
    }

    stages {
        stage('packaging git push para branch beta') {
            steps {  
                    script {
                        env.encodedPass=URLEncoder.encode(BITBUCKET_CREDENTIALS_PSW, "UTF-8")
                    }     
                    git push https://${BITBUCKET_CREDENTIALS_USR}:${encodedPass}@bitbucket.com/yourrepo.git branch

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .