Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to pass username and password of a bitbucket repository to a shell script triggered via jenkins

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 myuser@bitbucket.org/myuser/proj.git" target="_blank" rel="nofollow">https://myuser@bitbucket.org/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.
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:$pass@bitbucket.org/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.
by

1 Answer

vishaljlf39
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

Login / Signup to Answer the Question.