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 
> <https://****:****@github.com/afsadev/debt-agreement.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/ 
> <https://****@github.com/afsadev/debt-agreement.git>myuser 
> <https://****:****@github.com/afsadev/debt-agreement.git>/myrepo.git 
> <https://****@github.com/afsadev/debt-agreement.git> 
> +refs/heads/*:refs/remotes/origin/*
> ERROR: Error fetching remote repo 'origin'hudson.plugins.git.GitException 
> <http://stacktrace.jenkins-ci.org/search?query=hudson.plugins.git.GitException>:
>  Failed to fetch from https://****@github.com/ 
> <https://****@github.com/afsadev/debt-agreement.git>myuser 
> <https://****:****@github.com/afsadev/debt-agreement.git>/myrepo.git 
> <https://****@github.com/afsadev/debt-agreement.git>
>       at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:810) 
> <http://stacktrace.jenkins-ci.org/search/?query=hudson.plugins.git.GitSCM.fetchFrom&entity=method>
>       at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1066) 
> <http://stacktrace.jenkins-ci.org/search/?query=hudson.plugins.git.GitSCM.retrieveChanges&entity=method>
>       at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1097) 
> <http://stacktrace.jenkins-ci.org/search/?query=hudson.plugins.git.GitSCM.checkout&entity=method>
>       at 
> org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:109) 
> <http://stacktrace.jenkins-ci.org/search/?query=org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout&entity=method>
>       at 
> org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:83)
>  
> <http://stacktrace.jenkins-ci.org/search/?query=org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run&entity=method>
>       at 
> org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:73)
>  
> <http://stacktrace.jenkins-ci.org/search/?query=org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run&entity=method>
>       at 
> org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1$1.call(AbstractSynchronousNonBlockingStepExecution.java:49)
>  
> <http://stacktrace.jenkins-ci.org/search/?query=org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1$1.call&entity=method>
>       at hudson.security.ACL.impersonate(ACL.java:213) 
> <http://stacktrace.jenkins-ci.org/search/?query=hudson.security.ACL.impersonate&entity=method>
>       at 
> org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1.run(AbstractSynchronousNonBlockingStepExecution.java:47)
>  
> <http://stacktrace.jenkins-ci.org/search/?query=org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1.run&entity=method>
>       at 
> java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) 
> <http://stacktrace.jenkins-ci.org/search/?query=java.util.concurrent.Executors$RunnableAdapter.call&entity=method>
>       at java.util.concurrent.FutureTask.run(FutureTask.java:262) 
> <http://stacktrace.jenkins-ci.org/search/?query=java.util.concurrent.FutureTask.run&entity=method>
>       at 
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
>  
> <http://stacktrace.jenkins-ci.org/search/?query=java.util.concurrent.ThreadPoolExecutor.runWorker&entity=method>
>       at 
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
>  
> <http://stacktrace.jenkins-ci.org/search/?query=java.util.concurrent.ThreadPoolExecutor$Worker.run&entity=method>
>       at java.lang.Thread.run(Thread.java:745) 
> <http://stacktrace.jenkins-ci.org/search/?query=java.lang.Thread.run&entity=method>
> Caused by: hudson.plugins.git.GitException 
> <http://stacktrace.jenkins-ci.org/search?query=hudson.plugins.git.GitException>:
>  Command "/opt/rh/devtoolset-2/root/usr/bin/git -c core.askpass=true fetch 
> --tags --progress https://****@github.com/ 
> <https://****@github.com/afsadev/debt-agreement.git>myuser 
> <https://****:****@github.com/afsadev/debt-agreement.git>/myrepo.git 
> <https://****@github.com/afsadev/debt-agreement.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.

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/2a57fd66-6412-4a03-9bb3-5279c3cf4a35%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to