git 詹金斯:无法连接到存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21557998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:40:16  来源:igfitidea点击:

Jenkins: Failed to connect to repository

gitgithubsshjenkins

提问by adolfosrs

I'm trying to connect jenkins on a github repo.

我正在尝试在 github 存储库上连接 jenkins。

When I specify the Repo URL jenkins return the following error message:

当我指定 Repo URL 时,jenkins 返回以下错误消息:

Failed to connect to repository : Command "git ls-remote -h [email protected]:adolfosrs/jenkins-test.git HEAD" returned status code 128: stdout: stderr: Host key verification failed. fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.

无法连接到存储库:命令“git ls-remote -h [email protected]:adolfosrs/jenkins-test.git HEAD”返回状态代码 128:stdout:stderr:主机密钥验证失败。致命:无法从远程存储库读取。请确保您拥有正确的访问权限并且存储库存在。

When using the HTTPS:// Url jenkins returns:

当使用 HTTPS:// Url jenkins 返回:

Failed to connect to repository : Failed to connect to https://github.com/adolfosrs/jenkins-test.git(status = 407)

无法连接到存储库:无法连接到 https://github.com/adolfosrs/jenkins-test.git(状态 = 407)

I could successfully clone the repo at the same machine where jenkins is running and I also run the git ls-remote -h [email protected]:adolfosrs/jenkins-test.git HEADcommand. So I have the right SSH at github.

我可以在运行 jenkins 的同一台机器上成功克隆 repo,我也运行了git ls-remote -h [email protected]:adolfosrs/jenkins-test.git HEAD命令。所以我在 github 上有正确的 SSH。

采纳答案by adolfosrs

The problem was that somehow I created the ssh files with the root user. So the files owner was root.

问题是我以某种方式使用 root 用户创建了 ssh 文件。所以文件所有者是root。

The solution was just change the ownership to the jenkins user.

解决方案只是将所有权更改为 jenkins 用户。

chown jenkins id_rsa.pub 
chown jenkins id_rsa

回答by Juergen Klasen

This is a very tricky issue - even if you're familiar with how things are working in https with certificates (OTOH if you see my workaround, it seems very logical :)

这是一个非常棘手的问题 - 即使您熟悉 https 与证书的工作方式(OTOH,如果您看到我的解决方法,这似乎非常合乎逻辑:)

If you want to connect to a GIT repository via http(s) from shell, you would make sure to have the public certificate stored (as file) on your machine. Then you would add that certificate to your GIT configuration

如果您想从 shell 通过 http(s) 连接到 GIT 存储库,您将确保将公共证书(作为文件)存储在您的机器上。然后您将该证书添加到您的 GIT 配置中

git config [--global] http.sslCAInfo "certificate"

git config [--global] http.sslCAInfo“证书”

(replace "certificate" with the complete path/name of the PEM file :)

(用 PEM 文件的完整路径/名称替换“证书”:)

For shell usage you would as well e.g. supply a '.netrc' provding your credentials for the http-server login. Having done that, you shall be able to do a 'git clone https://...' without any interactive provisioning of credentials.

对于 shell 使用,您还可以提供一个“.netrc”来证明您的 http-server 登录凭据。完成后,您将能够执行“git clone https://...”而无需任何交互式凭据配置。

However, for the Jenkins-service it's a bit different ... Here, the jenkins process needs to be aware of the server certificate - and it doesn't use the shell settings (in the meaning of the global git configuration file '.gitconfig') :P

然而,对于 Jenkins-service 来说有点不同......这里,jenkins 进程需要知道服务器证书 - 它不使用 shell 设置(在全局 git 配置文件 '.gitconfig ') :P

What I needed to do is to add another parameter to the startup options of Jenkins.

我需要做的是在 Jenkins 的启动选项中添加另一个参数。

... -Djavax.net.ssl.trustStore="keystore" ...

... -Djavax.net.ssl.trustStore="keystore" ...

(replace "keystore" with the complete path/name like explained below :)

(用完整的路径/名称替换“keystore”,如下所述:)

Now copy the keystore file of your webserver holding the certificate to some path (I know this is a dirty hack and not exactlysecure :) and refer to it with the '-Djavax.net.ssl.trustStore=' parameter.

现在将持有证书的网络服务器的密钥库文件复制到某个路径(我知道这是一个肮脏的黑客,并不完全安全:) 并使用 '-Djavax.net.ssl.trustStore=' 参数引用它。

Now the Jenkins service will accept the certificate from the webserver providing the repository via https. Configure the GIT repository URL like

现在 Jenkins 服务将接受来自通过 https 提供存储库的网络服务器的证书。配置 GIT 存储库 URL,如

https://yourserver.com/your-repositorypath

https://yourserver.com/your-repositorypath

Note that you still require the '.netrc' under the jenkins-user home folder for the logon !!!Thus what I describe is to be seen as a workaround ... until a properly working credentials helper plugin is provided. IMHO this plugin (in its current version 1.9.4) is buggy.

请注意,您仍然需要 jenkins-user 主文件夹下的“.netrc”才能登录!!!因此,我所描述的将被视为一种解决方法......直到提供了一个正常工作的凭据帮助程序插件。恕我直言,这个插件(在其当前版本 1.9.4 中)有问题。

I could never get the credentials-helper to work from Jenkins no matter what I tried :( At best I got to see some errors about the not accessible temporary credential helper file, etc. You can see lots of bugs reported about it in the Jenkins JIRA, but no fix.

无论我尝试什么,我都无法让凭证助手从 Jenkins 工作:( 充其量我看到了一些关于不可访问的临时凭证助手文件等的错误。您可以在 Jenkins 中看到很多关于它的错误报告JIRA,但没有修复。

So if somebody got it to work okay, please share the knowledge ...

所以如果有人让它正常工作,请分享知识......



P.S.: Using the Jenkins plugins in the following versions:

PS:使用以下版本的Jenkins插件:

Credentials plugin 1.9.4, GIT client plugin 1.6.1, Jenkins GIT plugin 2.0.1

凭据插件 1.9.4、GIT 客户端插件 1.6.1、Jenkins GIT 插件 2.0.1

回答by Caleb Cheng

I had the exact same problem. The way I solved it on Mac is this:

我有同样的问题。我在 Mac 上解决它的方法是这样的:

  1. Switch to jenkins user (sudo -iu jenkins)
  2. Run: ssh-keygen (Note - You are creating ssh key pairs for jenkins user now. You should see something like this : Enter file in which to save the key (/Users/Shared/Jenkins/.ssh/id_rsa):
  3. Keep pressing Enter for default value till end
  4. Run the command showing in the Jenkins error message, on your teminal (eg : "git ls-remote -h [email protected]:adolfosrs/jenkins-test.git HEAD")
  5. You will be asked if you want to continue. Say yes
  6. The Github repo will be added to your known_hosts file in : /Users/Shared/Jenkins/.ssh/
  7. Go back to Jenkins portal and try your Github SSH url
  8. It should work. Good Luck
  1. 切换到 jenkins 用户 (sudo -iu jenkins)
  2. 运行:ssh-keygen(注意 - 您现在正在为 jenkins 用户创建 ssh 密钥对。您应该看到如下内容:输入保存密钥的文件(/Users/Shared/Jenkins/.ssh/id_rsa):
  3. 一直按 Enter 键获取默认值直到结束
  4. 在您的终端上运行 Jenkins 错误消息中显示的命令(例如:“git ls-remote -h [email protected]:adolfosrs/jenkins-test.git HEAD”)
  5. 系统会询问您是否要继续。说是
  6. Github 存储库将添加到您的 known_hosts 文件中:/Users/Shared/Jenkins/.ssh/
  7. 返回 Jenkins 门户并尝试您的 Github SSH url
  8. 它应该工作。祝你好运

回答by Rajaraman

I faced a similar issue when I tried to connect jenkins in my Windows server with my private GIT repo. Following is the error returned in the source code management section of Jenkins job

当我尝试将 Windows 服务器中的 jenkins 与我的私人 GIT 存储库连接时,我遇到了类似的问题。以下是Jenkins作业源码管理部分返回的错误

Failed to connect to repository : Command "git.exe ls-remote -h ssh://git@my_server/repo.git HEAD" returned status code 128: stdout: stderr: Load key "C:\Windows\TEMP\ssh4813927591749610777.key": invalid format git@my_server: Permission denied (publickey). fatal: Could not read from remote repository.

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

无法连接到存储库:命令“git.exe ls-remote -h ssh://git@my_server/repo.git HEAD”返回状态代码 128: stdout: stderr: Load key "C:\Windows\TEMP\ssh4813927591749610777.密钥”:无效格式 git@my_server:权限被拒绝(公钥)。致命:无法从远程存储库读取。

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

This error is thrown because jenkins is not able to pick the private ssh key from its user directory. I solved this in the following manner

抛出此错误是因为 jenkins 无法从其用户目录中选择私有 ssh 密钥。我通过以下方式解决了这个问题

Step 1

第1步

In the jenkins job, fill up the following info under Source Code Management

在詹金斯工作中,填写以下信息 Source Code Management

Repositories

Repository URL: ssh://git@my_server/repo.git
Credentials: -none-

存储库

存储库 URL:ssh://git@my_server/repo.git
凭据:-none-

Step 2

第2步

In my setup jenkins is running under local system account, so the user directory is C:\Windows\System32\config\systemprofile(This is the important thing in this setup that is not very obvious).

在我的设置中 jenkins 在本地系统帐户下运行,所以用户目录是C:\Windows\System32\config\systemprofile(这是这个设置中不是很明显的重要事情)。

Now create ssh private and public keys using ssh-keygen -t rsa -C "key label"via git bashshell. The ssh private and public keys go under .ssh directory of your logged in user directory. Just copy the .ssh folder and paste it under C:\Windows\System32\config\systemprofile

现在使用ssh-keygen -t rsa -C "key label"通过git bashshell创建 ssh 私钥和公钥。ssh 私钥和公钥位于您登录用户目录的 .ssh 目录下。只需复制 .ssh 文件夹并将其粘贴到C:\Windows\System32\config\systemprofile

Step 3

第 3 步

Add your public key to your GIT server account. Run the jenkins job and now you should be able to connect to the GIT account via ssh from jenkins.

将您的公钥添加到您的 GIT 服务器帐户。运行 jenkins 作业,现在您应该能够通过 jenkins 的 ssh 连接到 GIT 帐户。

回答by 030

In our case githad to be installed on the Jenkins server.

在我们的例子git中,必须安装在 Jenkins 服务器上。

回答by David Engdal

Jenkins runs as another user, not as your ordinary login. So, do as this to solve the ssh problem:

Jenkins 以另一个用户的身份运行,而不是您的普通登录名。所以,这样做来解决ssh问题:

  1. Log on as jenkins su jenkins(you may first have to do sudo passwd jenkinsto be able to set the password for jenkins. I couldn't find the default...)
  2. Generate ssh key pair: ssh-keygen
  3. Copy the public key (id_rsa.pub) to your github account (or wherever)
  4. Clone the repo as jenkinsin order to have the host added to jenkins known_hostswhich is neccessary to do. Now you can remove the cloned repo again if you wish.
  1. 以 jenkins 身份登录su jenkins(您可能首先必须sudo passwd jenkins为 jenkins 设置密码。我找不到默认值...)
  2. 生成 ssh 密钥对: ssh-keygen
  3. 将公钥 ( id_rsa.pub)复制到您的 github 帐户(或任何地方)
  4. 将 repo 克隆为 jenkins,以便将主机添加到 jenkins known_hosts,这是必要的。现在,您可以根据需要再次删除克隆的存储库。

回答by Harshal Vaidya

Check with below settings. That always work for me.

检查以下设置。那总是对我有用。

Jenkins Configuration :

詹金斯配置:

1) Check whether git executable is appropriately specified

1) 检查是否正确指定了 git 可执行文件

2) Provide SSH repository link git@blahblah

2)提供SSH仓库链接git@blahblah

3) Under credentials >> Select Username and Authentication key (go to your server, Generate SSH keys ssh-keygen... Copy keys to JENKINS_HOME/,ssh) You should be able to connect to your GIT repository from Jenkins

3) 在凭据下 >> 选择用户名和身份验证密钥(转到您的服务器,生成 SSH 密钥 ssh-keygen... 将密钥复制到 JENKINS_HOME/,ssh)您应该能够从 Jenkins 连接到您的 GIT 存储库

回答by Joseph Lust

On Ubuntu, placed your id_rsaand id_rsa.pubfiles in /var/lib/jenkins/.ssh

在 Ubuntu 上,将您的id_rsaid_rsa.pub文件放在/var/lib/jenkins/.ssh

Make Jenkins own them sudo chown -R jenkins /var/lib/jenkins/.ssh/

让詹金斯拥有它们 sudo chown -R jenkins /var/lib/jenkins/.ssh/

Make sure that Jenkins key is added as deploy key with RW access in GitHub (or similar) - use the id_rsa.pubkey for this.

确保将 Jenkins 密钥添加为在 GitHub(或类似)中具有 RW 访问权限的部署密钥 -id_rsa.pub为此使用密钥。

Now everything should jive with the SCM Sync Plugin.

现在一切都应该与 SCM 同步插件配合使用。

回答by Jean-Michel Bernard

Not mentionned here so far, but this can come also from stash. We encountered the same issue, the root cause for our problem was that the stash instance we use for jenkins did crash. Restarting stash solved it in our case.

到目前为止这里没有提到,但这也可以来自 stash。我们遇到了同样的问题,我们问题的根本原因是我们用于 jenkins 的 stash 实例确实崩溃了。在我们的案例中,重新启动 stash 解决了这个问题。

回答by Roman

In my case I resolved this issue by

就我而言,我解决了这个问题

  • clicking button Addwhich is next to the "Credentials" text
  • adding credentials (loginand password)
  • selecting these credentials on the popup menu, which is on the left of the Addbutton
  • waiting for a couple of seconds
  • 单击Add“凭据”文本旁边的按钮
  • 添加凭据(loginpassword
  • Add按钮左侧的弹出菜单上选择这些凭据
  • 等待几秒钟

My environment was Jenkins installed in the Windows. The UI question was why the warning was placed before the tool to resolve it.

我的环境是 Jenkins 安装在 Windows 中。UI 问题是为什么将警告放在解决它的工具之前。