带有 https 的 Jenkins Git 插件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19561570/
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:10:27  来源:igfitidea点击:

Jenkins Git plugin with https

gitjenkinsjenkins-plugins

提问by Christina

I am trying to configure a Git project in Jenkins using the Git plugin. In the project configuration page I enter the repository URL in the Git configuration, which is an https URL (https://git.mycompany.com/git/MyProject.git). When I'm building the project however I get the following error:

我正在尝试使用 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.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.invoke(GitSCM.java:1012)
    at hudson.plugins.git.GitSCM.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.access0(CliGitAPIImpl.java:47)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.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.invoke(GitSCM.java:1025)
    at hudson.plugins.git.GitSCM.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)

I can clone the repository from the command line, as well as by executing git cloneas a shell command in the Jenkins project pre-build steps, so I'm at a loss on why the plugin configuration doesn't work. Thinking it might be an authentication issue I tried specifying the credentials in a .netrc file as well as including them in the URL (ie. https://username:[email protected]/git/MyProject.git) however in all cases I'm still getting the same error. Any ideas?

我可以从命令行克隆存储库,也可以git clone在 Jenkins 项目预构建步骤中作为 shell 命令执行,所以我不知道为什么插件配置不起作用。认为这可能是一个身份验证问题,我尝试在 .netrc 文件中指定凭据并将它们包含在 URL 中(即https://username:[email protected]/git/MyProject.git)但是在在所有情况下,我仍然遇到相同的错误。有任何想法吗?

回答by jimpic

This is a bug in the Jenkins Git Plugin.

这是Jenkins Git Plugin中的一个错误。

You can workaround the credential issue by creating credentials using the credential plugin and then use these credentials in the SCM/Git section of your Job. However, this will expose your user/password in plaintext in the build log if the checkout fails.

您可以通过使用凭据插件创建凭据来解决凭据问题,然后在作业的 SCM/Git 部分使用这些凭据。但是,如果结帐失败,这将在构建日志中以纯文本形式公开您的用户/密码。

Also, this will not work if you are using a HTTP proxy with git. The best way (for now) is to use JGit (to be configured in the Jenkins configuration). However, JGit is experimental and very limited as well when it comes to proxies.

此外,如果您在 git 中使用 HTTP 代理,这将不起作用。最好的方法(目前)是使用 JGit(在 Jenkins 配置中进行配置)。然而,JGit 是实验性的,在代理方面也非常有限。

(answer posted due to popular request ;) )

(由于受欢迎的要求而发布了答案;))

回答by mgr32

In case you're using a self-signed certificate for your Git repository and Git works from command line but not from Jenkins Git Client plugin, you need to add the certificate to the Jenkins Java Keystore (as described by tijsin the comment above).

如果您使用的是自签名证书,以便Git仓库和Git的命令行工作,但不能从詹金斯Git的客户端插件,您需要添加证书到詹金斯Java密钥(如由tijs上述评论) .

This is because Git Client plugin tries to connect directly using Java's Apache HttpClient (bypassing git.exe), so all Git settings normally used to create the connection are ignored (including GIT_SSL_NO_VERIFYand certificates in curl-ca-bundle.crt). The HttpClient throwsSunCertPathBuilderException: unable to find valid certification path to requested targetwhich is unfortunately wrapped in a GitException without stack trace, so all we can see is 'Failed to connect'message.

这是因为Git的客户端插件尝试直接使用Java的Apache的HttpClient的(绕过git.exe)连接,因此所有的Git设置通常用于创建连接被忽略(包括GIT_SSL_NO_VERIFY和证书curl-ca-bundle.crt)。HttpClient的抛出SunCertPathBuilderException: unable to find valid certification path to requested target这是不幸的是包裹在一个GitException无堆栈跟踪,所以我们只能看到“无法连接”消息。

In order to fix it you can follow the link provided by tijs: http://www.mkyong.com/webservices/jax-ws/suncertpathbuilderexception-unable-to-find-valid-certification-path-to-requested-target/

为了修复它,您可以按照tijs提供的链接进行操作http: //www.mkyong.com/webservices/jax-ws/suncertpathbuilderexception-unable-to-find-valid-certification-path-to-requested-target/

You would need to copy the resulting jssecacertsfile to C:\Program Files (x86)\Jenkins\jre\lib\securityif you're using default Jenkins installation.

如果您使用默认的 Jenkins 安装jssecacertsC:\Program Files (x86)\Jenkins\jre\lib\security则需要将生成的文件复制到。

You can find a link to InstallCert.javain the original Andreas Sterbenz post(thanks to web.archive.org), or a slightly modified version at code.google.

您可以InstallCert.javaAndreas Sterbenz的原始帖子(感谢 web.archive.org)中找到链接,或者在code.google 中找到稍微修改的版本。

I checked the above approach works for Git Client plugin version 1.4.6.

我检查了上述方法适用于 Git 客户端插件版本 1.4.6。

回答by Jenish Patel

Upgrade your Git client to 2.10.0 or up.

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

execute below commands.

执行以下命令。

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

回答by eis

Main page of git pluginhas this to say:

git 插件的主页有这样的说法:

If you are seeing output indicating Git could not clone, something like the output below, go to to the Jenkins configuration settings (not the project settings, the global ones) and change the Git path to a fully qualified path (eg. not "git" but "/usr/bin/git" or wherever your Git binary is installed). You should also verify that the permissions are correct if you are doing a file system based clone.

如果您看到指示 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.invoke(GitSCM.java:400)
    at hudson.plugins.git.GitSCM.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)

This might be an issue for you too.

这对你来说也可能是一个问题。

The same page also suggests testing with JGit if there's an issue with default plugin behaviour(-Dorg.jenkinsci.plugins.gitclient.Git.useCLI=false).

如果默认插件行为有问题,同一页面还建议使用 JGit 进行测试(-Dorg.jenkinsci.plugins.gitclient.Git.useCLI=false)。

回答by Lucas Amorim Silva

You can register your git credentialsin "Credentials"with your loginand passwordto access the git repository by https protocol(below of "Manage Jenkins"), it will generate a credentialId(e.g. dd0d6733-cc2e-4464-bb7d-4b6af86fe40a) that helps Jenkins to use this credential in pipelines.

您可以使用您的登录名密码“凭据”中注册您的git 凭据以通过https 协议访问 git 存储库(“管理 Jenkins”下方),它将生成一个凭据ID(例如dd0d6733-cc2e-4464-bb7d-4b6af86fe40a)帮助 Jenkins 在管道中使用此凭证。

When you use Jenkins Pipeline, you can do:

当您使用Jenkins Pipeline 时,您可以执行以下操作:

node{
    git url: "https://tfs:8080/tfs/job.git", branch: "feature/migration", credentialsId:'dd0d6733-cc2e-4464-bb7d-4b6af86fe40a'
}

It will download your source codes to the job's workspace

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

回答by micfan

My solution did works well for Jenkins-Git-Plugin.

我的解决方案确实适用于 Jenkins-Git-Plugin。

# create my exec
su root
darngit=/usr/bin/darngit
touch $darngit
echo "#! /usr/bin/env bash" > $darngit
echo "git -c sslVerify=false $@" > $darngit

Goto system setting, set git executable = /usr/bin/darngit

转到系统设置,设置 git 可执行文件 = /usr/bin/darngit

回答by ashah

I tried all stuff to get rid of certificate issue but then finally it is issue with git plugin. I roll back plugins to git 1.5 (from 2.0.3) and git-client 1.0.7 (from 1.6.3) and it worked find. Note that I tried git-client 1.4.6 first so that I can use credential option but it didn't work. So I think correct combination of these 2 plugins are important. This solution was mentioned at JENKINS-20533

我尝试了所有方法来摆脱证书问题,但最后还是 git 插件的问题。我将插件回滚到 git 1.5(从 2.0.3)和 git-client 1.0.7(从 1.6.3)并且它找到了。请注意,我首先尝试了 git-client 1.4.6,以便我可以使用凭据选项,但它不起作用。所以我认为这两个插件的正确组合很重要。JENKINS-20533 中提到了此解决方案

回答by Mahi

In order to use Git in Jenkins, the following two plugins need to be installed:

为了在Jenkins中使用Git,需要安装以下两个插件:

  1. Git-Client-plugin
  2. Git-plugin
  1. Git 客户端插件
  2. Git插件

The first plugin (Git-Client-plugin) is a low-level API to execute git operations (git add, git commit, git push ,git clone,..etc).

第一个插件 (Git-Client-plugin) 是一个低级 API,用于执行 git 操作(git add、git commit、git push、git clone 等)。

The second plugin (Git-plugin) adds Git as an SCM option for Jenkins jobs.

第二个插件 (Git-plugin) 添加 Git 作为 Jenkins 作业的 SCM 选项。

The second plugin relies on functionality from the first plugin, and so both must be present for Git to work with Jenkins.

第二个插件依赖于第一个插件的功能,因此两个插件都必须存在,Git 才能与 Jenkins 一起工作。

回答by user5195035

In my case, one Jenkins slave worked normally, while another did not. When I changed the Git executable from default to JGit, they both worked.

就我而言,一个 Jenkins 奴隶正常工作,而另一个则没有。当我将 Git 可执行文件从默认更改为 JGit 时,它们都可以工作。

回答by kriss

This is now fixed and no more an issue with up to date plugin (as I'm writing this 1.19.0). It happened with git-plugin-client 1.6.x.

这现在已修复,并且不再是最新插件的问题(因为我正在编写此 1.19.0)。它发生在 git-plugin-client 1.6.x 上。