在Jenkins中使用Git Plugin訪問Https代碼庫失敗的問題


  最近需要在Jenkins上配置一個Job,SCM源是http://git.opendaylight.org/gerrit/p/integration.git
  於是使用Jenkins的Git Plugin做這件事情。
  結果報錯如下:

hudson.plugins.git.GitException: Failed to fetch from https://git.opendaylight.org/gerrit/p/integration.git
        at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:627)
        at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:865)
        at hudson.plugins.git.GitSCM.checkout(GitSCM.java:890)
        at hudson.model.AbstractProject.checkout(AbstractProject.java:1415)
        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:561)
        at hudson.model.Run.execute(Run.java:1678)
        at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)
        at hudson.model.ResourceController.execute(ResourceController.java:88)
        at hudson.model.Executor.run(Executor.java:231)
Caused by: hudson.plugins.git.GitException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
        at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.checkCredentials(CliGitAPIImpl.java:2198)
        at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:1152)
        at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$200(CliGitAPIImpl.java:87)
        at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:266)
        at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:625)
        ... 10 more

  但是直接在命令行中運行git clone是好的,那么差別在哪里呢?在網上查了一下,得到了一些思路,即Jenkins整體是構建在Java之上的,在進入git接管的范圍之前,是Java在做一些事情,既然git本身是可以工作的,那么應該就是Java這邊出的問題。
  查了下具體的錯誤,發現跟SSL/TSL證書有關。看到StackOverflow上有同志說是git plugin本身有bug就不能支持https,我覺得應該不至於吧,查看了git plugin的主頁(https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin)也沒有發現什么端倪。於是又隨便嘗試了一下github上的某個https的git庫,是可以clone下來的,這就說明支持https是沒問題的。那么問題出在哪里呢?重新復習了一下SSL/TSL的原理之后,認為問題應該出在頒發證書的CA上,於是在瀏覽器中去查看二者的CA有何不同,如下:

  github使用的是DigiCert的證書,而opendaylight用的是StartCom的。上網一查,這個StartCom頒發的整數居然是免費的。這樣看來,原因大概就是Java不信任這個CA了。那么解決方法應該就是把這個CA添加到Java的信任列表。具體做法如下:
1. 首先要得到StartCom的證書文件(xxx.cer)。從哪里獲得呢?既然瀏覽器能夠正常訪問,說明系統中是信任StartCom的證書的,所以先打開系統的證書管理界面。在Mac中是Keychain,打開之后找到StartCom的證書,然后右鍵導出成一個.cer文件,命名為startcom.cer。

2. 然后使用下面的命令把上面那個cer文件導入到Java的運行時系統中:

keytool -import -alias startcom  -keystore %JRE_HOME/lib/security/cacerts  -file startcom.cer

  運行該命令時會提示輸入密碼,如果你沒有改過的話密碼是’changeit’
  然后再運行下面的命令,就可以看到StartCom已經被加進去了。

keytool -list -keystore  %JRE_HOME/lib/security/cacerts  | less

  然后在在Jenkins中配置git路徑,git clone成功!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM