在jenkins文件中使用jenkins商店中的凭证 [英] Using credentials from jenkins store in a jenkinsfile

查看:1022
本文介绍了在jenkins文件中使用jenkins商店中的凭证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在詹金斯制造了一个多分支管道项目。我需要使用两个存储库,并且都需要凭据。



我在repository1中创建了一个Jenkinsfile:

  node('label1 '){
''健全性检查'
sh'echo健全性检查'
阶段'结帐其他存储库'
结帐([
$ class:'GitSCM',分支:[[name:'* / master']],
userRemoteConfigs:[[url:'https://BRNTZN@bitbucket.org/BRNTZN/repository2.git'],[credentialsId:'23b2eed1-2863 -49d5-bc7b-bcccb9ad914d']]
])
阶段'记录结果'
sh'echo结果=确定'
}

当我将这个文件推送到repository1的一个分支并开始构建时,我在jenkins中得到以下错误:

 分支索引
将原点设置为https://BRNTZN@bitbucket.org/BRNTZN/repository1.git
获取原点...
> git rev-parse --is-inside-work-tree#timeout = 10
从远程Git仓库获取更改
> GIT中配置remote.origin.url HTTPS://BRNTZN@bitbucket.org/BRNTZN/repository1.git#超时= 10
撷取从https上游的变化://BRNTZN@bitbucket.org/BRNTZN/repository1.git
> git --version#timeout = 10
使用.gitcredentials设置凭据
> git config --local credential.username BRNTZN#timeout = 10
> git config --local credential.helper store --file = / tmp / git1367320661933193799.credentials#timeout = 10
> git -c core.askpass = true fetch --tags --progress https://BRNTZN@bitbucket.org/BRNTZN/repository1.git + refs / heads / *:refs / remotes / origin / *
> git config --local --remove-section credential#timeout = 10
检出版本d997a29e9d1f639d56eb425ec00e03309e099c7a(jenkinsfilebranch1)
> git config core.sparsecheckout#timeout = 10
> git checkout -f d997a29e9d1f639d56eb425ec00e03309e099c7a
> GIT中REV-列表f81d0d366fd751857a2640c587817f4d047a15af#超时= 10
[管道]节点
上运行上詹金斯剂(I-07353fc08cb42f10e)在/ var /詹金斯/工作区/多分支/ jenkinsfilebranch1
[管道] {
[Pipeline]阶段(完整性检查)
进入阶段完整性检查
正在执行
[管道] sh
[jenkinsfilebranch1]运行shell脚本
+ echo完整性检查
完整性检查
[Pipeline]阶段(检出其他存储库)
输入阶段结帐其他存储库
正在执行
[管道]检出
> git rev-parse --is-inside-work-tree#timeout = 10
从远程Git仓库获取更改
> GIT中配置remote.origin.url HTTPS://BRNTZN@bitbucket.org/BRNTZN/repository2.git#超时= 10
撷取从https上游的变化://BRNTZN@bitbucket.org/BRNTZN/repository2.git
> git --version#timeout = 10
> git -c core.askpass = true fetch --tags --progress https://BRNTZN@bitbucket.org/BRNTZN/repository2.git + refs / heads / *:refs / remotes / origin / *
错误:错误获取远程回购 '起源'
hudson.plugins.git.GitException:无法从https获取://BRNTZN@bitbucket.org/BRNTZN/repository2.git
在hudson.plugins.git.GitSCM .fetchFrom(GitSCM.java:799)
at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1055)
at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1086 )
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)
在org.jenkinsci.plugins.workflow.steps.scm.SCMStep $ StepExecutionImpl.run(SCMStep.java:73)
在org.jenkinsci.plugins.workflow.steps .AbstractSynchronousNonBlockingStepExecution $ 1 $ 1.call(AbstractSynchronousNonBlockingStepExecution.java:52)
at hudson.security.ACL.impersonate(ACL.java:21 3)
在org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution $ 1.run(AbstractSynchronousNonBlockingStepExecution.java:49)
在java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:471 )
在java.util.concurrent.FutureTask.run(FutureTask.java:262)
处的Java java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
。 util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)
在java.lang.Thread.run(Thread.java:745)
产生的原因:hudson.plugins.git.GitException:命令git -c core.askpass = true fetch --tags --progress https://BRNTZN@bitbucket.org/BRNTZN/repository2.git + refs / heads / *:refs / remotes / origin / *返回状态码128:
stdout:
stderr:remote:无效的用户名或密码。如果您通过第三方服务登录,则必须确保您的帐户配置文件中设置了帐户密码。
致命:https://BRNTZN@bitbucket.org/BRNTZN/repository2.git/'

在org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl。的java:1723)
。在org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:1459)
。在org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access $ 300(CliGitAPIImpl.java:63 )
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl $ 1.execute(CliGitAPIImpl.java:314)
at org.jenkinsci.plugins.gitclient.RemoteGitImpl $ CommandInvocationHandler $ 1.call(RemoteGitImpl.java:152 )
at org.jenkinsci.plugins.gitclient.RemoteGitImpl $ CommandInvocationHandler $ 1.call(RemoteGitImpl.java:145)
at hudson.remoting.UserRequest.perform(UserRequest.java:153)
在hudson.remoting.UserRequest.perform(UserRequest.java:50)
at hudson.remoting.Request $ 2.run(Request.java:332)
at hudson.remoting.InterceptingExecutorService $ 1.call(Intercept $ java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)$ b $在java.lang.Thread.run(Thread.java:744)
at ..... .remote调用jenkins代理(i-07353fc08cb42f10e)(Native Method)
at hudson.remoting.Channel.attachCallSiteStackTrace(Channel.java:1416)
at hudson.remoting.UserResponse.retrieve(UserRequest.java :253)
at hudson.remoting.Channel.call(Channel.java:781)
at org.jenkinsci.plugins.gitclient.RemoteGitImpl $ CommandInvocationHandler.execute(RemoteGitImpl.java:145)
。在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43 )
at java.lang.reflect.Method.invoke(Method.java:606)
at org.jenkinsci.plugins.gitclient.RemoteGitImpl $ CommandInvocationHandler.invoke(RemoteGitImpl.java:131)
at com $ sudo.proxy $ Proxy75.execute(Unknown Source)
at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:797)
... 13 more
[Pipeline] }
[Pipeline] //节点
[Pipeline]流水线结束
错误:null
完成:FAILURE

证书应该正确:



在自由式项目中使用这些存储库的凭证不会产生错误:



更新
我使用s创建了一个自由式项目sh证书并将该公钥添加到我的bitbucket帐户,以测试我是否可以使ssh工作:


这有效:

 由用户admin开始
在工作区/ var中远程创建jenkins代理(i-039385e75b60d70f7)(label1) / jenkins / workspace / gitcredentials test
> git rev-parse --is-inside-work-tree#timeout = 10
从远程Git仓库获取更改
> git config remote.origin.url git@bitbucket.org:BRNTZN / repository2.git#timeout = 10
从git@bitbucket.org获取上游更改:BRNTZN / repository2.git
> git --version#timeout = 10
使用GIT_SSH设置凭证jenkinsmaster key
> git -c core.askpass = true fetch --tags --progress git@bitbucket.org:BRNTZN / repository2.git + refs / heads / *:refs / remotes / origin / *
> git rev-parse refs / remotes / origin / master ^ {commit}#timeout = 10
> git rev-parse refs / remotes / origin / origin / master ^ {commit}#timeout = 10
检出版本1d51064143e7337cbc0b1910918166facc9c2330(refs / remotes / origin / master)
> git config core.sparsecheckout#timeout = 10
> git checkout -f 1d51064143e7337cbc0b1910918166facc9c2330
首次构建。跳过更新日志。
完成:SUCCESS

然而,当以下列方式更新jenkins文件时:

  node('label1'){
'健全检查'
sh'echo健全检查'
'
checkout'[
$ class:'GitSCM',branches:[[name:'* / master']],
userRemoteConfigs:[[url:'git @ bitbucket.org:BRNTZN / repository2.git'],[credentialsId:'jenkinsmaster']]
])
阶段'记录结果'
sh'echo结果=确定'
}

我仍然遇到同样的错误:

 由用户admin启动
将原点设置为git@bitbucket.org:BRNTZN / repository1.git
获取原点...
> git rev-parse --is-inside-work-tree#timeout = 10
从远程Git仓库获取更改
> git config remote.origin.url git@bitbucket.org:BRNTZN / repository1.git#timeout = 10
从git@bitbucket.org获取上游变更:BRNTZN / repository1.git
> git --version#timeout = 10
使用GIT_SSH设置凭证jenkinsmaster key
> git -c core.askpass = true fetch --tags --progress git@bitbucket.org:BRNTZN / repository1.git + refs / heads / *:refs / remotes / origin / *
检出版本29fc47911827d829f5abe9456bd8df78bc478fe7(jenkinsfilebranch1 )
> git config core.sparsecheckout#timeout = 10
> git checkout -f 29fc47911827d829f5abe9456bd8df78bc478fe7
> GIT中REV-列表29fc47911827d829f5abe9456bd8df78bc478fe7#超时= 10
[管道]节点
上运行上詹金斯剂(I-039385e75b60d70f7)在/ var /詹金斯/工作区/多分支/ jenkinsfilebranch1
[管道] {
[Pipeline]阶段(完整性检查)
进入阶段完整性检查
正在执行
[管道] sh
[jenkinsfilebranch1]运行shell脚本
+ echo完整性检查
完整性检查
[Pipeline]阶段(检出其他存储库)
输入阶段结帐其他存储库
正在执行
[管道]检出
> git rev-parse --is-inside-work-tree#timeout = 10
从远程Git仓库获取更改
> git config remote.origin.url git@bitbucket.org:BRNTZN / repository2.git#timeout = 10
从git@bitbucket.org获取上游更改:BRNTZN / repository2.git
> git --version#timeout = 10
> git -c core.askpass = true fetch --tags --progress git@bitbucket.org:BRNTZN / repository2.git + refs / heads / *:refs / remotes / origin / *
错误:获取远程repo出错'origin'
hudson.plugins.git.GitException:无法从git@bitbucket.org获取:BRNTZN / repository2.git
at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:799 )
at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1055)
at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1086)
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:52)
at hudson.security.ACL.impersonate(ACL.java:213)
at org.jen kinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution $ 1.run(AbstractSynchronousNonBlockingStepExecution.java:49)$ b $在java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:471)$ b $在java.util .concurrent.FutureTask.run(FutureTask.java:262)$ java.util.concurrent.ThreadPoolExecutor.runWorker
(ThreadPoolExecutor.java:1145)$ java.util.concurrent.ThreadPoolExecutor
$ Worker.run (ThreadPoolExecutor.java:615)
在java.lang.Thread.run(Thread.java:745)
引起:hudson.plugins.git.GitException:命令git -c core.askpass = true fetch --tags --progress git@bitbucket.org:BRNTZN / repository2.git + refs / heads / *:refs / remotes / origin / *返回状态码128:
stdout:
stderr :权限被拒绝(publickey)。
致命:无法从远程存储库读取。

请确保您拥有正确的访问权限
并存在存储库。

在org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:1723)
在org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:1459)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access $ 300(CliGitAPIImpl.java:63)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl $ 1.execute(CliGitAPIImpl.java:314)
at org.jenkinsci.plugins.gitclient.RemoteGitImpl $ CommandInvocationHandler $ 1.call(RemoteGitImpl.java:152)
at org.jenkinsci.plugins.gitclient.RemoteGitImpl $ CommandInvocationHandler $ 1.call(RemoteGitImpl.java:145)
at hudson.remoting.UserRequest.perform(UserRequest.java:153)
at hudson.remoting.UserRequest.perform(UserRequest.java:50)
at hudson.remoting.Request $ 2。运行(Request.java:332)
at hudson.remoting.InterceptingExecutorService $ 1.call(InterceptingExecutorService.java:68)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)$ b $在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)$ b $在java.lang。 Thread.run(Thread.java:744)
at ......远程调用jenkins代理(i-039385e75b60d70f7)(Native Method)
at hudson.remoting.Channel.attachCallSiteStackTrace(Channel。
at hudson.remoting.UserResponse.retrieve(UserRequest.java:253)
at hudson.remoting.Channel.call(Channel.java:781)
at org.jenkinsci .plugins.gitclient.RemoteGitImpl $ CommandInvocationHandler.execute(RemoteGitImpl.java:145)
在sun.reflect.GeneratedMethodAccessor1180.invoke(来源不明)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java: 43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.jenkinsci.plugins.gitclient.RemoteGitImpl $ CommandInvocationHandler.invoke(RemoteGitImpl.java:131)
at $ Proxy $ .exe(未知来源)
at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:797)
... 13 more
[Pipeline ]}
[Pipeline] //节点
[流水线]流水线结束
错误:空
完成:FAILURE


解决方案

您的GitSCM类实例化不正确。你所拥有的是创建两个UserRemoteConfig对象 - 一个具有git@bitbucket.org:BRNTZN / repository2.git的URL和一个具有'jenkinsmaster'的credentialsId的URL。

  checkout([
$ class:'GitSCM',branches: [[name:'* / master']],
userRemoteConfigs:[[url:'git@bitbucket.org:BRNTZN / repository2.git'],[credentialsId:'jenkinsmaster']]
] )

应该是

  checkout([
$ class:'GitSCM',branches:[[name:'* / master']],
userRemoteConfigs:[[url:'git@bitbucket.org :BRNTZN / repository2.git',credentialsId:'jenkinsmaster']]
])

我刚刚遇到同样的问题,并将Eclipse调试器连接到Jenkins以查找问题。



请参阅

I made a multibranch pipeline project in jenkins. I need to use two repositories and both need credentials.

I created a Jenkinsfile in repository1:

node ('label1'){
  stage 'sanity check'
  sh 'echo sanity check'
  stage 'checkout other repository'
  checkout([
    $class: 'GitSCM', branches: [[name: '*/master']],
    userRemoteConfigs: [[url: 'https://BRNTZN@bitbucket.org/BRNTZN/repository2.git'],[credentialsId:'23b2eed1-2863-49d5-bc7b-bcccb9ad914d']]
  ])
  stage 'log results'
  sh 'echo result = OK'
 }

When I push this file onto a branch of repository1 and start the build I get the following error in jenkins:

Branch indexing
Setting origin to https://BRNTZN@bitbucket.org/BRNTZN/repository1.git
Fetching origin...
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://BRNTZN@bitbucket.org/BRNTZN/repository1.git # timeout=10
Fetching upstream changes from https://BRNTZN@bitbucket.org/BRNTZN/repository1.git
 > git --version # timeout=10
using .gitcredentials to set credentials
 > git config --local credential.username BRNTZN # timeout=10
 > git config --local credential.helper store --file=/tmp/git1367320661933193799.credentials # timeout=10
 > git -c core.askpass=true fetch --tags --progress https://BRNTZN@bitbucket.org/BRNTZN/repository1.git +refs/heads/*:refs/remotes/origin/*
 > git config --local --remove-section credential # timeout=10
Checking out Revision d997a29e9d1f639d56eb425ec00e03309e099c7a (jenkinsfilebranch1)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f d997a29e9d1f639d56eb425ec00e03309e099c7a
 > git rev-list f81d0d366fd751857a2640c587817f4d047a15af # timeout=10
[Pipeline] node
Running on jenkins agent (i-07353fc08cb42f10e) in /var/jenkins/workspace/multiBranch/jenkinsfilebranch1
[Pipeline] {
[Pipeline] stage (sanity check)
Entering stage sanity check
Proceeding
[Pipeline] sh
[jenkinsfilebranch1] Running shell script
+ echo sanity check
sanity check
[Pipeline] stage (checkout other repository)
Entering stage checkout other repository
Proceeding
[Pipeline] checkout
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://BRNTZN@bitbucket.org/BRNTZN/repository2.git # timeout=10
Fetching upstream changes from https://BRNTZN@bitbucket.org/BRNTZN/repository2.git
 > git --version # timeout=10
 > git -c core.askpass=true fetch --tags --progress https://BRNTZN@bitbucket.org/BRNTZN/repository2.git +refs/heads/*:refs/remotes/origin/*
ERROR: Error fetching remote repo 'origin'
hudson.plugins.git.GitException: Failed to fetch from https://BRNTZN@bitbucket.org/BRNTZN/repository2.git
    at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:799)
    at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1055)
    at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1086)
    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:52)
    at hudson.security.ACL.impersonate(ACL.java:213)
    at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1.run(AbstractSynchronousNonBlockingStepExecution.java:49)
    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 "git -c core.askpass=true fetch --tags --progress https://BRNTZN@bitbucket.org/BRNTZN/repository2.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout: 
stderr: remote: Invalid username or password. If you log in via a third party service you must ensure you have an account password set in your account profile.
fatal: Authentication failed for 'https://BRNTZN@bitbucket.org/BRNTZN/repository2.git/'

    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:1723)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:1459)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$300(CliGitAPIImpl.java:63)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:314)
    at org.jenkinsci.plugins.gitclient.RemoteGitImpl$CommandInvocationHandler$1.call(RemoteGitImpl.java:152)
    at org.jenkinsci.plugins.gitclient.RemoteGitImpl$CommandInvocationHandler$1.call(RemoteGitImpl.java:145)
    at hudson.remoting.UserRequest.perform(UserRequest.java:153)
    at hudson.remoting.UserRequest.perform(UserRequest.java:50)
    at hudson.remoting.Request$2.run(Request.java:332)
    at hudson.remoting.InterceptingExecutorService$1.call(InterceptingExecutorService.java:68)
    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:744)
    at ......remote call to jenkins agent (i-07353fc08cb42f10e)(Native Method)
    at hudson.remoting.Channel.attachCallSiteStackTrace(Channel.java:1416)
    at hudson.remoting.UserResponse.retrieve(UserRequest.java:253)
    at hudson.remoting.Channel.call(Channel.java:781)
    at org.jenkinsci.plugins.gitclient.RemoteGitImpl$CommandInvocationHandler.execute(RemoteGitImpl.java:145)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.jenkinsci.plugins.gitclient.RemoteGitImpl$CommandInvocationHandler.invoke(RemoteGitImpl.java:131)
    at com.sun.proxy.$Proxy75.execute(Unknown Source)
    at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:797)
    ... 13 more
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: null
Finished: FAILURE

The credentials should be correct:

And using those credentials for that repository in a freestyle project gives no error:

Update I created a freestyle project using ssh credentials and added that public key to my bitbucket account to test if I can make ssh work:

This worked:

    Started by user admin
Building remotely on jenkins agent (i-039385e75b60d70f7) (label1) in workspace /var/jenkins/workspace/gitcredentials test
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url git@bitbucket.org:BRNTZN/repository2.git # timeout=10
Fetching upstream changes from git@bitbucket.org:BRNTZN/repository2.git
 > git --version # timeout=10
using GIT_SSH to set credentials jenkinsmaster key
 > git -c core.askpass=true fetch --tags --progress git@bitbucket.org:BRNTZN/repository2.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 1d51064143e7337cbc0b1910918166facc9c2330 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 1d51064143e7337cbc0b1910918166facc9c2330
First time build. Skipping changelog.
Finished: SUCCESS

However when updating the jenkinsfile in the following way:

node ('label1'){
  stage 'sanity check'
  sh 'echo sanity check'
  stage 'checkout other repository'
  checkout([
    $class: 'GitSCM', branches: [[name: '*/master']],
    userRemoteConfigs: [[url: 'git@bitbucket.org:BRNTZN/repository2.git'],[credentialsId:'jenkinsmaster']]
  ])
  stage 'log results'
  sh 'echo result = OK'
 }

I still get the same error:

Started by user admin
Setting origin to git@bitbucket.org:BRNTZN/repository1.git
Fetching origin...
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url git@bitbucket.org:BRNTZN/repository1.git # timeout=10
Fetching upstream changes from git@bitbucket.org:BRNTZN/repository1.git
 > git --version # timeout=10
using GIT_SSH to set credentials jenkinsmaster key
 > git -c core.askpass=true fetch --tags --progress git@bitbucket.org:BRNTZN/repository1.git +refs/heads/*:refs/remotes/origin/*
Checking out Revision 29fc47911827d829f5abe9456bd8df78bc478fe7 (jenkinsfilebranch1)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 29fc47911827d829f5abe9456bd8df78bc478fe7
 > git rev-list 29fc47911827d829f5abe9456bd8df78bc478fe7 # timeout=10
[Pipeline] node
Running on jenkins agent (i-039385e75b60d70f7) in /var/jenkins/workspace/multiBranch/jenkinsfilebranch1
[Pipeline] {
[Pipeline] stage (sanity check)
Entering stage sanity check
Proceeding
[Pipeline] sh
[jenkinsfilebranch1] Running shell script
+ echo sanity check
sanity check
[Pipeline] stage (checkout other repository)
Entering stage checkout other repository
Proceeding
[Pipeline] checkout
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url git@bitbucket.org:BRNTZN/repository2.git # timeout=10
Fetching upstream changes from git@bitbucket.org:BRNTZN/repository2.git
 > git --version # timeout=10
 > git -c core.askpass=true fetch --tags --progress git@bitbucket.org:BRNTZN/repository2.git +refs/heads/*:refs/remotes/origin/*
ERROR: Error fetching remote repo 'origin'
hudson.plugins.git.GitException: Failed to fetch from git@bitbucket.org:BRNTZN/repository2.git
    at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:799)
    at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1055)
    at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1086)
    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:52)
    at hudson.security.ACL.impersonate(ACL.java:213)
    at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1.run(AbstractSynchronousNonBlockingStepExecution.java:49)
    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 "git -c core.askpass=true fetch --tags --progress git@bitbucket.org:BRNTZN/repository2.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout: 
stderr: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:1723)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:1459)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$300(CliGitAPIImpl.java:63)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:314)
    at org.jenkinsci.plugins.gitclient.RemoteGitImpl$CommandInvocationHandler$1.call(RemoteGitImpl.java:152)
    at org.jenkinsci.plugins.gitclient.RemoteGitImpl$CommandInvocationHandler$1.call(RemoteGitImpl.java:145)
    at hudson.remoting.UserRequest.perform(UserRequest.java:153)
    at hudson.remoting.UserRequest.perform(UserRequest.java:50)
    at hudson.remoting.Request$2.run(Request.java:332)
    at hudson.remoting.InterceptingExecutorService$1.call(InterceptingExecutorService.java:68)
    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:744)
    at ......remote call to jenkins agent (i-039385e75b60d70f7)(Native Method)
    at hudson.remoting.Channel.attachCallSiteStackTrace(Channel.java:1416)
    at hudson.remoting.UserResponse.retrieve(UserRequest.java:253)
    at hudson.remoting.Channel.call(Channel.java:781)
    at org.jenkinsci.plugins.gitclient.RemoteGitImpl$CommandInvocationHandler.execute(RemoteGitImpl.java:145)
    at sun.reflect.GeneratedMethodAccessor1180.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.jenkinsci.plugins.gitclient.RemoteGitImpl$CommandInvocationHandler.invoke(RemoteGitImpl.java:131)
    at com.sun.proxy.$Proxy75.execute(Unknown Source)
    at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:797)
    ... 13 more
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: null
Finished: FAILURE

解决方案

Your GitSCM class instantiation is incorrect. What you have is creating two UserRemoteConfig objects - one with a url of 'git@bitbucket.org:BRNTZN/repository2.git' and one with a credentialsId of 'jenkinsmaster'. Instead you want one object with both properties set.

checkout([
  $class: 'GitSCM', branches: [[name: '*/master']],
  userRemoteConfigs: [[url: 'git@bitbucket.org:BRNTZN/repository2.git'],[credentialsId:'jenkinsmaster']]
])

Should be

checkout([
  $class: 'GitSCM', branches: [[name: '*/master']],
  userRemoteConfigs: [[url: 'git@bitbucket.org:BRNTZN/repository2.git',credentialsId:'jenkinsmaster']]
])

I had just ran into the same issue and connected up an eclipse debugger to Jenkins to find the issue.

See https://issues.jenkins-ci.org/browse/JENKINS-45007

这篇关于在jenkins文件中使用jenkins商店中的凭证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆