gitcredentials in Pipeline

60 views
Skip to first unread message

Lionel Orellana

unread,
Apr 29, 2016, 12:16:29 AM4/29/16
to Jenkins Users
Hi 

I have a personal access token in my github account which I have added as a Credential in Jenkins. 

In freestyle jobs I can select the credential when configuring the git repository. This results in the token being used to configure gitcredentials when the job runs.

using .gitcredentials to set credentials
 > /opt/rh/devtoolset-2/root/usr/bin/git config --local credential.username ********* # timeout=10
 > /opt/rh/devtoolset-2/root/usr/bin/git config --local credential.helper store --file=/tmp/git8500404327159234501.credentials # timeout=10


How can I do this with the Pipeline git step? 

I tried the credentials-binding-plugin but didn't work.


node {  
   withCredentials
([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'cec6ad4c-a6c4-49ba-b7ba-95aace163002', passwordVariable: 'TOKEN', usernameVariable: 'USR']]) {
   git url
: "https://${env.USR}:${env.TOKEN}@github.com/myuser/myrepo.git"
   
}
}

[Pipeline] git
 > /opt/rh/devtoolset-2/root/usr/bin/git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > /opt/rh/devtoolset-2/root/usr/bin/git config remote.origin.url https://****:****@github.com/myuser/myrepo.git # timeout=10
Fetching upstream changes from https://****@github.com/afsadev/debt-agreement.git
 > /opt/rh/devtoolset-2/root/usr/bin/git --version # timeout=10
Setting http proxy: serverproxy:8080
 > /opt/rh/devtoolset-2/root/usr/bin/git -c core.askpass=true fetch --tags --progress https://****@github.com/myuser/myrepo.git +refs/heads/*:refs/remotes/origin/*
ERROR: Error fetching remote repo 'origin' hudson.plugins.git.GitException: Failed to fetch from https://****@github.com/myuser/myrepo.git
at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:810) at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1066) at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1097) at org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:109) at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:83) at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:73) at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1$1.call(AbstractSynchronousNonBlockingStepExecution.java:49) at hudson.security.ACL.impersonate(ACL.java:213) at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1.run(AbstractSynchronousNonBlockingStepExecution.java:47) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) at java.util.concurrent.FutureTask.run(FutureTask.java:262) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745) Caused by: hudson.plugins.git.GitException: Command "/opt/rh/devtoolset-2/root/usr/bin/git -c core.askpass=true fetch --tags --progress https://****@github.com/myuser/myrepo.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout: stderr: remote: Invalid username or password.

The same credential still works in my freestyle project.

 Thanks.

Lionel Orellana

unread,
Apr 29, 2016, 12:21:19 AM4/29/16
to Jenkins Users

Sorry just realised there's a credentialsId param in the git step.
Reply all
Reply to author
Forward
0 new messages