Not able to push to GIT repo using 'withcredentials'

318 views
Skip to first unread message

ishan jain

unread,
May 5, 2017, 6:02:36 AM5/5/17
to Jenkins Users
I am trying to push some tags back to repository and for obvious reasons, i would like to keep my usernam/pass hidden. I use withcredentials for this and the code looks like this:

checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'git']], submoduleCfg: [], userRemoteConfigs: [[url: 'http://my-GITBUCKET-gitURL.git']]])

        
dir ('git') {

withCredentials([usernamePassword(credentialsId: '11111111-1111-11111-11111-111111122222', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) {
sh "git credential approve"
sh 'git tag -a ' + env.BUILD_ID + ' -m "Creating tag"'
sh "git push origin " + env.BUILD_ID
}
}

This always gives me the following error:

09:45:12 [git] Running shell script
09:45:13 + git push origin 124
09:45:13 fatal: could not read Username for 'http://my-GITBUCKET-gitURL.git': No such device or address

I tried learning more about this error and seems like it should have been working after using GIT approve, but apparently it is not.

I tried something like below and it works fine, only that it is an ugly workaround and i do not with to use as a final solution.

checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'git']], submoduleCfg: [], userRemoteConfigs: [[url: 'http://my-GITBUCKET-gitURL.git']]])

        
dir ('git') {

withCredentials([usernamePassword(credentialsId: '11111111-1111-11111-11111-111111122222', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) {
sh "git credential approve"
sh 'git tag -a ' + env.BUILD_ID + ' -m "Creating tag"'
sh "git push http://${GIT_USERNAME}:${GIT_PASSWORD}@${repoURL.substring(7)} --tags"
}
}


Ramanathan Muthaiah

unread,
May 5, 2017, 7:56:09 PM5/5/17
to Jenkins Users
 
So, the URL with username and password is working (as expected).  If you are using withCredentials (meta-step ?) with credentials id, username and password are variables that help to store authentication info that can be used for git operations.

Have you looked at the console log, these values will be masked and displayed as asterisks ?

BTW, you should avoid having generic names, like, 'git' within dir() as seen in the code snippet shared by you.
It should reflect the purpose for which is meant for, like, it could be same as the repo name that you are trying to update with new commits.

/Ram

ishan jain

unread,
May 8, 2017, 7:29:38 AM5/8/17
to Jenkins Users
Hi,

I changed my code to post it here and during that i changed the dir name. I wouldn't worry about naming standards yet, the bigger problem for me is that it is not working.

I saw some code snippets claiming withcredentials to be working the way i have posted. Don't know what is the issue here. 
Reply all
Reply to author
Forward
0 new messages