使用https的Jenkins Git插件

16

我正在尝试使用Git插件在Jenkins中配置一个Git项目。在项目配置页面中,我在Git配置中输入了存储库的URL,这是一个https URL (https://git.mycompany.com/git/MyProject.git)。然而,在构建项目时,我遇到了以下错误:

Started by user Hudson Administrator
[EnvInject] - Loading node environment variables.
Building in workspace /home/hudson/.hudson/jobs/MyProject/workspace
Checkout:workspace / /home/hudson/.hudson/jobs/MyProject/workspace - hudson.remoting.LocalChannel@3699cfcc
Using strategy: Default
Cloning the remote Git repository
Cloning repository https://git.mycompany.com/git/MyProject.git
git --version
git version 1.8.2.1
ERROR: Error cloning remote repo 'origin' : Could not clone https://git.mycompany.com/git/MyProject.git
hudson.plugins.git.GitException: Could not clone https://git.mycompany.com/git/MyProject.git
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:286)
    at org.jenkinsci.plugins.gitclient.AbstractGitAPIImpl.clone(AbstractGitAPIImpl.java:59)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.clone(CliGitAPIImpl.java:47)
    at hudson.plugins.git.GitSCM$2.invoke(GitSCM.java:1012)
    at hudson.plugins.git.GitSCM$2.invoke(GitSCM.java:948)
    at hudson.FilePath.act(FilePath.java:912)
    at hudson.FilePath.act(FilePath.java:885)
    at hudson.plugins.git.GitSCM.determineRevisionToBuild(GitSCM.java:948)
    at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1114)
    at hudson.model.AbstractProject.checkout(AbstractProject.java:1411)
    at hudson.model.AbstractBuild$AbstractBuildExecution.defaultCheckout(AbstractBuild.java:652)
    at jenkins.scm.SCMCheckoutStrategy.checkout(SCMCheckoutStrategy.java:88)
    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:557)
    at hudson.model.Run.execute(Run.java:1665)
    at hudson.maven.MavenModuleSetBuild.run(MavenModuleSetBuild.java:507)
    at hudson.model.ResourceController.execute(ResourceController.java:88)
    at hudson.model.Executor.run(Executor.java:230)
Caused by: hudson.plugins.git.GitException: Failed to connect to https://git.mycompany.com/git/MyProject.git
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.getURLWithCrendentials(CliGitAPIImpl.java:1374)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.getURLWithCrendentials(CliGitAPIImpl.java:1326)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$300(CliGitAPIImpl.java:47)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:280)
    ... 16 more
Trying next repository
ERROR: Could not clone repository
java.io.IOException: Could not clone
    at hudson.plugins.git.GitSCM$2.invoke(GitSCM.java:1025)
    at hudson.plugins.git.GitSCM$2.invoke(GitSCM.java:948)
    at hudson.FilePath.act(FilePath.java:912)
    at hudson.FilePath.act(FilePath.java:885)
    at hudson.plugins.git.GitSCM.determineRevisionToBuild(GitSCM.java:948)
    at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1114)
    at hudson.model.AbstractProject.checkout(AbstractProject.java:1411)
    at hudson.model.AbstractBuild$AbstractBuildExecution.defaultCheckout(AbstractBuild.java:652)
    at jenkins.scm.SCMCheckoutStrategy.checkout(SCMCheckoutStrategy.java:88)
    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:557)
    at hudson.model.Run.execute(Run.java:1665)
    at hudson.maven.MavenModuleSetBuild.run(MavenModuleSetBuild.java:507)
    at hudson.model.ResourceController.execute(ResourceController.java:88)
    at hudson.model.Executor.run(Executor.java:230)

我可以通过命令行克隆存储库,也可以在Jenkins项目预构建步骤中执行git clone作为Shell命令来进行克隆,所以我不知道插件配置为什么不起作用。考虑可能是身份验证问题,我尝试在.netrc文件中指定凭据以及将它们包含在URL中(例如https://username:password@git.mycompany.com/git/MyProject.git),但是在所有情况下,我仍然得到相同的错误信息。有什么想法吗?


可能的一个原因是Jenkins无法接受您的SSL证书(也许它不是您使用的域名)。 - tijs
1
由于Jenkins是用Java编写的,它使用Java的可接受证书列表,因此您可能只需要将其添加到该存储中。以下是一些解决方法:http://www.mkyong.com/webservices/jax-ws/suncertpathbuilderexception-unable-to-find-valid-certification-path-to-requested-target/ - tijs
2
我也遇到了同样的问题。在我看来,这是Jenkins Git插件中的一个bug。当你在Jenkins配置中设置使用JGit时,它可以正常工作。或者使用SSH。在以前的版本中(几个月前),它是可以正常工作的。 - jimpic
1
@jimpic 看来终究是这样。我还测试了对公共 Git 存储库的访问(以确保不是我传递凭据的方式出现问题),但仍然无法工作,所以我猜唯一剩下的选择就是插件的 bug。如果您将此作为答案发布(可能还有一些关于可能解决方法的详细信息),我将很乐意接受它。 - Christina
感谢您提出这个问题并发表评论。我刚花了一周的时间试图弄清楚到底发生了什么事情,最后发现这是一个插件问题。该死的 Git 插件! (说的是那个不知好歹的免费插件的用户)。我也会点赞 @jimpic 在这里发布的任何答案。另外,有没有已经提交 bug 呢?如果没有,我会提交一个 bug。 - Classified
显示剩余2条评论
11个回答

17
这是Jenkins Git插件中的一个bug。
你可以通过使用凭据插件创建凭据,并在作业的SCM/Git部分中使用这些凭据来解决凭据问题。但是,如果检出失败,这将在构建日志中以明文形式公开您的用户名/密码。
此外,如果您正在使用HTTP代理与git,则此方法将无法正常工作。目前最好的方法是使用JGit(在Jenkins配置中配置)。然而,JGit是实验性的,当涉及到代理时也非常有限。
(由于众多请求而发布的答案;))

你知道这个问题是否已经被提出了吗? - eis
+1 确认这是插件的一个 bug。至少我不是疯了 :) 你有 Jenkins 的 bug ID 和/或修复进度吗?我发现了几个看起来像我正在经历的 bug(如果需要,我可以发布一些),但它看起来并不完全像我的,所以我不确定进展情况。甚至有一个说“不是缺陷”。 - Classified
我不知道是否有未解决的工单,因为我没有在那里注册账号,所以我也没有创建过。如果有人能够帮忙创建一下,那就太好了 :) - jimpic
1
那么,我们现在处于什么状态?这是一个错误吗?如果是错误,那么它是否已经解决了呢? - Kundan Ray

7
如果您正在使用自签名证书来管理Git仓库,且从命令行中可以成功连接,但是从Jenkins Git Client插件无法连接,则需要将该证书添加到Jenkins Java Keystore中(如上述评论所述)。
这是因为Git Client插件试图直接使用Java的Apache HttpClient进行连接(绕过git.exe),所以通常用于创建连接的所有Git设置都会被忽略(包括GIT_SSL_NO_VERIFYcurl-ca-bundle.crt中的证书)。HttpClient抛出了SunCertPathBuilderException: unable to find valid certification path to requested target异常,不幸的是它被GitException包装而没有堆栈跟踪,因此我们只能看到“连接失败”的消息。
为了解决这个问题,您可以按照tijs提供的链接操作:http://www.mkyong.com/webservices/jax-ws/suncertpathbuilderexception-unable-to-find-valid-certification-path-to-requested-target/ 如果您使用的是默认的Jenkins安装程序,请将生成的jssecacerts文件复制到C:\Program Files (x86)\Jenkins\jre\lib\security目录下。
您可以在原始的Andreas Sterbenz post中找到InstallCert.java的链接(感谢web.archive.org),或者在code.google上找到稍微修改过的版本。
我已经验证了以上方法适用于Git Client插件版本1.4.6。

尚未在Jenkins中进行验证,但对于Teamcity,还有一种更简单的方法也可以运行 - 只需在Jenkins的jre目录中运行以下命令:bin\keytool -Import -Noprompt -Keystore lib\security\cacerts -Deststorepass changeit -Alias <your_alias> -File <your_cer_file> - mgr32

2

将您的Git客户端升级到2.10.0或更高版本。

执行以下命令。

git config --system http.sslVerify false    
git config --global http.sslVerify false    

我可以知道为什么你撤销了我的(绝对正确的)格式吗? - Uwe Allner
我刚开始回答问题,所以不理解你的回答的目的。但现在我明白了,你可以更改它。感谢您的支持。 - Jenish Patel

1

Git插件的主页面中提到:

如果您看到输出指示Git无法克隆,例如下面的输出,请转到Jenkins配置设置(不是项目设置,而是全局设置),并将Git路径更改为完全限定路径(例如,不是“git”,而是“/usr/bin/git”或任何您的Git二进制文件安装的位置)。如果您正在进行基于文件系统的克隆,请还应验证权限是否正确。

Started by user anonymous
Checkout:workspace / C:\Documents and Settings\Administrator\.hudson\jobs\watir\workspace - hudson.remoting.LocalChannel@1a1f370
Last Build : #4
Checkout:workspace / C:\Documents and Settings\Administrator\.hudson\jobs\watir\workspace - hudson.remoting.LocalChannel@1a1f370
Cloning the remote Git repository
Cloning repository origin
$ git clone -o origin git://github.com/bret/watir.git "C:\Documents and Settings\Administrator\.hudson\jobs\watir\workspace"
Trying next repository
ERROR: Could not clone from a repository
FATAL: Could not clone
hudson.plugins.git.GitException: Could not clone
    at hudson.plugins.git.GitSCM$2.invoke(GitSCM.java:400)
    at hudson.plugins.git.GitSCM$2.invoke(GitSCM.java:358)
    at hudson.FilePath.act(FilePath.java:676)
    at hudson.FilePath.act(FilePath.java:660)
    at hudson.plugins.git.GitSCM.checkout(GitSCM.java:358)
    at hudson.model.AbstractProject.checkout(AbstractProject.java:833)
    at hudson.model.AbstractBuild$AbstractRunner.checkout(AbstractBuild.java:314)
    at hudson.model.AbstractBuild$AbstractRunner.run(AbstractBuild.java:266)
    at hudson.model.Run.run(Run.java:948)
    at hudson.model.Build.run(Build.java:112)
    at hudson.model.ResourceController.execute(ResourceController.java:93)
    at hudson.model.Executor.run(Executor.java:118)

这对你来说可能也是个问题。
同一页上还建议使用JGit进行测试,如果默认插件行为存在问题(-Dorg.jenkinsci.plugins.gitclient.Git.useCLI=false)。

我看到过这个,但至少在我的情况下,这不是问题所在。 - Christina

1
为了在Jenkins中使用Git,需要安装以下两个插件:
  1. Git-Client-plugin
  2. Git-plugin
第一个插件(Git-Client-plugin)是执行git操作(如git add、git commit、git push、git clone等)的低级API。
第二个插件(Git-plugin)将Git添加为Jenkins作业的SCM选项。
第二个插件依赖于第一个插件的功能,因此Git要与Jenkins一起工作,两个插件都必须存在。

1
你可以在"凭据"中使用你的登录名和密码,注册你的git凭据以通过https协议("Jenkins管理"下方)访问git存储库,这将生成一个凭据ID(例如dd0d6733-cc2e-4464-bb7d-4b6af86fe40a),帮助Jenkins在流水线中使用此凭据。当你使用Jenkins Pipeline时,你可以进行以下操作:
node{
    git url: "https://tfs:8080/tfs/job.git", branch: "feature/migration", credentialsId:'dd0d6733-cc2e-4464-bb7d-4b6af86fe40a'
}

它将下载您的源代码到作业的工作区。

0

现在已经修复了,使用最新的插件就不会再出现这个问题(我写这篇文章时是1.19.0版本)。这个问题发生在git-plugin-client 1.6.x版本。


0
在我的情况下,一个 Jenkins slave 正常工作,而另一个则不正常。当我将 Git 可执行文件从默认更改为 JGit 时,它们都可以正常工作。

0

我尝试了所有的方法来解决证书问题,但最终发现是git插件的问题。我将插件回滚到git 1.5(从2.0.3)和git-client 1.0.7(从1.6.3),然后它就可以正常工作了。请注意,我首先尝试了git-client 1.4.6,以便使用凭据选项,但它没有起作用。因此,我认为这两个插件的正确组合非常重要。这个解决方案在JENKINS-20533中提到。


0

在 Jenkins 1.644 和 Git 客户端 1.19.6 上,使用 git:// 而不是 https:// 对我有效。


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