为什么 Jenkins 中的 GIT 插件无法连接到 GIT 存储库?

Why GIT Plugin in Jenkins is not able to connect to the GIT Repository?

我正在尝试使用 GIT 插件 Jenkins 提取代码形式 GIT 并且工作是 运行 在从机上。

MASTER系统有http_proxy=mycom.domain.com:80

并且在 SLAVE 系统中没有 http_proxy 定义。

每当我在 SLAVE 机器上进行本地 git 克隆时,它都能完美运行,但是在 Jenkins 中我没有成功。

它抛出以下错误:

Building remotely on SLAVE in workspace /data/test
 > /usr/bin/git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > /usr/bin/git config remote.origin.url https://github.domain.com/Project-Digital/Project-eCommerce.git # timeout=10
Fetching upstream changes from https://github.domain.com/Project-Digital/Project-eCommerce.git
 > /usr/bin/git --version # timeout=10
using GIT_ASKPASS to set credentials 
Setting http proxy: mycom.domain.com:80
 > /usr/bin/git fetch --tags --progress https://github.domain.com/Project-Digital/Project-eCommerce.git +refs/heads/*:refs/remotes/origin/*
ERROR: Error fetching remote repo 'origin'
hudson.plugins.git.GitException: Failed to fetch from https://github.domain.com/Project-Digital/Project-eCommerce.git
    at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:803)
    at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1063)
    at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1094)
    at hudson.scm.SCM.checkout(SCM.java:495)
    at hudson.model.AbstractProject.checkout(AbstractProject.java:1278)
    at hudson.model.AbstractBuild$AbstractBuildExecution.defaultCheckout(AbstractBuild.java:604)
    at jenkins.scm.SCMCheckoutStrategy.checkout(SCMCheckoutStrategy.java:86)
    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:529)
    at hudson.model.Run.execute(Run.java:1728)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:98)
    at hudson.model.Executor.run(Executor.java:404)
Caused by: hudson.plugins.git.GitException: Command "/usr/bin/git fetch --tags --progress https://github.domain.com/Project-Digital/Project-eCommerce.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout: 
stderr: error: Failed connect to github.build.ge.com:80; Operation now in progress while accessing https://github.domain.com/Project-Digital/Project-eCommerce.git/info/refs

是否因为 MASTER 系统试图设置 SLAVE 系统中不存在的 http 代理?

如果是,如何预防?

或者,我还有什么遗漏的吗?

在拉取项目之前,尝试在全局配置中设置HTTP代理:

$> git config --global http.proxy http://mycom.example.com:80

如果您需要为您的代理提供用户名和密码,您可以使用:

$> git config --global http.proxy http://example.com\<yourUsername>:<yourPassword>@<yourProxyServer>:80

今天我还使用 git 存储库设置了 jenkins,对我有用,希望这对您有所帮助,

要将 git 与 jenkins 连接,请执行以下步骤:

第 1 步。成功安装插件后创建一个新的工作是这样的:

1.create一个职位名称

2.checked Build a maven software project 单选按钮

3.click 好的

第 2 步。现在选中 Git 的单选按钮,输入 git 存储库的 uri。

第 3 步。如果你会看到这样的错误

无法连接到存储库:命令 "git ls-remote -h git@example.git HEAD" 返回状态代码 128: 标准输出: stderr: fatal: 'git@example.git' 似乎不是 git 存储库 fatal: 远端意外挂断

您还需要做一些配置:

1.Go 到终端

2.Run 这个命令:sudo visudo

3.Add %jenkins ALL=NOPASSWD: ALL in this file where sudo previlage is defined.and 关闭文件。

4.login 作为 jenkins 用户通过命令:sudo su jenkins

5.Create jenkins 主目录中的一个 .ssh 目录。

6.create public 这样的私钥对。

正在生成 SSH 密钥:

1: 检查 SSH 密钥

首先,我们需要检查您计算机上现有的 ssh 密钥。打开终端和 运行:

cd ~/.ssh 查看你的用户目录下是否有名为“.ssh”的目录

如果它显示 "No such file or directory" 转到第 2 步。否则,您已经有一个现有的密钥对,您可以跳到第 3 步。

2: 生成新的 SSH 密钥

要生成新的 SSH 密钥,请输入以下代码。我们需要默认设置,所以当要求输入一个文件来保存密钥时,只需按回车键。

ssh-keygen -t rsa -C "your_email@example.com"

创建一个新的 ssh 密钥,使用提供的电子邮件作为标签生成 public/private rsa 密钥对。输入保存密钥的文件 (/home/you/.ssh/id_rsa): 现在您需要输入密码或 file.press 输入而无需输入任何内容。

哪个应该给你这样的东西:

您的身份证明已保存在/home/you/.ssh/id_rsa

您的public密钥已保存在/home/you/.ssh/id_rsa.pub.密钥指纹为:

01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com

3:将您的 SSH 密钥添加到 GitHub

转到您的帐户设置

4 : 在源代码管理标签下,

       Build Triggers- Build whenever a SNAPSHOT dependency is built
       Root POM-  /var/lib/jenkins/jobs/ProjectName/workspace/ProjectName/pom.xml

在执行 Shell 标签下 你可以让你的脚本执行。

最后点击立即构建创建构建,打开控制台检查状态。

原来是代理问题。

在使用 Jenkins 从 GIT 中提取代码时,它会将 http_proxy 设置为 mycom.domain.com:80MASTER 机器代理)并且 MASTER 不需要此代理 SLAVE机.

所以,我刚刚将 GIT URL 添加到 Jenkins 的无代理主机部分 (Manage Jenkins -> Manage Plugins -> Advanced -> HTTP Proxy Configuration -> Added GIT URL in No Proxy Host field),现在它工作正常。