使用jenkins Pipeline作为代码从bitbucket私有存储库中克隆

时间:2017-01-04 03:44:18

标签: jenkins jenkins-pipeline

我使用jenikins管道作为代码来克隆私有bitbucket存储库(存储库)中的git项目。我使用此代码块在我的管道脚本中克隆项目。

node {  
//checkout from master  
stage 'checkout'  
   withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'MyID', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]) {  

      git url: 'https://paulrda@devMyCompany.org/stash/scm/test_automation.git' , branch: 'development'   
   }  
}  

'MyID'是凭证ID,我的用户名和密码是正确的。我将凭据保存在jenkins的全局凭据功能中。但是当我构建jenkins任务时,我得到了这个错误。

ERROR: Error fetching remote repo 'origin'
hudson.plugins.git.GitException: Failed to fetch from https://paulrda@devMyCompany.org/stash/scm/test_automation.git
    at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:803)
    at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1063)
    at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1094)
    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:47)
    at hudson.security.ACL.impersonate(ACL.java:221)
    at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1.run(AbstractSynchronousNonBlockingStepExecution.java:44)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: hudson.plugins.git.GitException: Command "git fetch --tags --progress https://paulrda@devMyCompany.org/stash/scm/test_automation.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout: 
stderr: fatal: Authentication failed for 'https://paulrda@devMyCompany.org/stash/scm/test_automation.git/'

    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:1745)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:1489)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$300(CliGitAPIImpl.java:64)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:315)
    at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:801)

在我的paulrda帐户下的mac机器中,我可以使用jenkins管道脚本成功克隆我的项目,但当我更改为另一个帐户并运行jenkins时,我收到此错误。我仍然无法理解为什么我得到这个错误。请提供此问题的解决方案。

我的配置。
詹金斯版本:2.19.2
凭据插件:2.1.8
Git插件:3.0.0
Git客户端插件:2.1.0

1 个答案:

答案 0 :(得分:4)

由于您没有正确地将凭据传递给git,因此无法进行身份验证。

由于您使用的是Git插件而不是shell命令,因此根本不需要使用withCredentials。您可以将credentialsId直接传递给git来电,就像那样:

stage('checkout') {
    git credentialsId: 'MyID', url: 'https://devMyCompany.org/stash/scm/test_automation.git', branch: 'development'
}