Jenkins从GitHub私有仓库拉取源代码

4

我正在尝试在我的服务器上为我的项目设置Jenkins。

我已经在GitHub私有仓库中上传了我的构建脚本。我已经建立了SSH密钥,并通过GitHub部署了它。

但是当我尝试构建时,Jenkins无法成功地从我的私有仓库拉取代码。

以下是控制台输出:

Started by user anonymous
Building in workspace /Users/Shared/Jenkins/Home/jobs/moai-test/workspace
Checkout:workspace / /Users/Shared/Jenkins/Home/jobs/moai-test/workspace - hudson.remoting.LocalChannel@15e6e48b
Using strategy: Default
Fetching changes from 1 remote Git repository
Fetching upstream changes from https://github.com/****/****.git
ERROR: Problem fetching from origin / origin - could be unavailable. Continuing anyway
hudson.plugins.git.GitException: Error performing command: git fetch -t https://github.com/****/****.git +refs/heads/*:refs/remotes/origin/*
    at hudson.plugins.git.GitAPI.launchCommandIn(GitAPI.java:862)
    at hudson.plugins.git.GitAPI.launchCommand(GitAPI.java:817)
    at hudson.plugins.git.GitAPI.fetch(GitAPI.java:197)
    at hudson.plugins.git.GitAPI.fetch(GitAPI.java:1063)
    at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:812)
    at hudson.plugins.git.GitSCM.access$100(GitSCM.java:90)
    at hudson.plugins.git.GitSCM$2.invoke(GitSCM.java:1096)
    at hudson.plugins.git.GitSCM$2.invoke(GitSCM.java:1064)
    at hudson.FilePath.act(FilePath.java:842)
    at hudson.FilePath.act(FilePath.java:824)
    at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1064)
    at hudson.model.AbstractProject.checkout(AbstractProject.java:1256)
    at hudson.model.AbstractBuild$AbstractBuildExecution.defaultCheckout(AbstractBuild.java:589)
    at jenkins.scm.SCMCheckoutStrategy.checkout(SCMCheckoutStrategy.java:88)
    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:494)
    at hudson.model.Run.execute(Run.java:1502)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)
    at hudson.model.ResourceController.execute(ResourceController.java:88)
    at hudson.model.Executor.run(Executor.java:236)
Caused by: java.lang.InterruptedException
    at java.lang.Object.wait(Native Method)
    at java.lang.Object.wait(Object.java:485)
    at java.lang.UNIXProcess.waitFor(UNIXProcess.java:115)
    at hudson.Proc$LocalProc.join(Proc.java:319)
    at hudson.Launcher$ProcStarter.join(Launcher.java:345)
    at hudson.plugins.git.GitAPI.launchCommandIn(GitAPI.java:843)
    ... 18 more
ERROR: Could not fetch from any repository
FATAL: Could not fetch from any repository
hudson.plugins.git.GitException: Could not fetch from any repository
    at hudson.plugins.git.GitSCM$2.invoke(GitSCM.java:1103)
    at hudson.plugins.git.GitSCM$2.invoke(GitSCM.java:1064)
    at hudson.FilePath.act(FilePath.java:842)
    at hudson.FilePath.act(FilePath.java:824)
    at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1064)
    at hudson.model.AbstractProject.checkout(AbstractProject.java:1256)
    at hudson.model.AbstractBuild$AbstractBuildExecution.defaultCheckout(AbstractBuild.java:589)
    at jenkins.scm.SCMCheckoutStrategy.checkout(SCMCheckoutStrategy.java:88)
    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:494)
    at hudson.model.Run.execute(Run.java:1502)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)
    at hudson.model.ResourceController.execute(ResourceController.java:88)
    at hudson.model.Executor.run(Executor.java:236)

你确定 Jenkins 进程是在实际拥有 ~user/.ssh 目录下 ssh 公钥和私钥的用户下运行的吗? - VonC
我很确定Jenkins正在以拥有私钥的用户身份运行。 我已经更改了Jenkins配置文件,以使用拥有SSH公钥的用户来运行Jenkins。 - Tinolover
好的,这里只是检查一下。再检查一下:远程GitHub地址正确吗(我的意思是,里面没有错别字吗?http://stackoverflow.com/questions/11485775/why-is-my-git-push-returning-with-fatal-error/11486595#11486595 或者 https://dev59.com/-2ox5IYBdhLWcg3w5IRv#8906460) - VonC
是的,我刚刚用****的地址替换了GitHub的地址。 我正在使用正确的地址。 - Tinolover
ok(因为一个小写字母而不是大写字母足以使整个查询失败) - VonC
你正在使用https网址而不是ssh网址,因此未使用.ssh密钥。 - Stephen Connolly
1个回答

3

查看你的构建日志

Using strategy: Default
Fetching changes from 1 remote Git repository
Fetching upstream changes from https://github.com/****/****.git
ERROR: Problem fetching from origin / origin - could be unavailable. Continuing anyway

您正在使用 GitHub 仓库的 HTTPS URI。

HTTPS URI 使用用户名和密码进行身份验证,而不是 SSH 密钥。

要使用 SSH 密钥,您需要检出 SSH URI,例如:

git@github.com:****/****.git

这应该能解决你的问题。

或者,您可以将用户名/密码提供给Jenkins(尽管这是更大的安全风险)。


我已将HTTPS URI更改为SSH URI,但遇到了另一个问题。 控制台输出显示主机密钥验证失败。 我确定我已生成id_rsa密钥并在github上部署了它。 有什么想法来解决这个问题吗? - Tinolover
要解决这个问题,您需要执行ssh http://github.com命令,然后在提示时回答yes,将GitHub添加到受信任主机列表中。您只需要在任何给定的计算机上执行此操作一次,它就会被永久添加。 - CIGuy
你需要确保Jenkins使用的密钥没有设置密码。如果我没记错,Jenkins无法处理带有密码的密钥(因为要将密码输入程序变得复杂 - 程序试图确保只有真正的键盘用于输入密码!) - Stephen Connolly
@Stephen Connolly:你应该能够通过预先授权使用一个受密码保护的密钥来使用ssh-agent。虽然这很复杂,但你需要在每次重新启动(或类似情况)时重新进行身份验证。 - umläute
@umlaeute,理论上你应该能够做到这一点,但是有些插件使用Java客户端,而其他插件使用CLI客户端,因此最终结果是一些插件会捕捉到代理,而其他插件则会失败。最好的办法是为Jenkins生成一个专门的密钥,并确保该密钥没有密码保护。 - Stephen Connolly

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接