首页 > 解决方案 > Jenkins 代理和主服务器上的 Git 路径不同

问题描述

我有以下问题:我在 GCE(谷歌计算引擎)上运行 Jenkins,它被配置为在需要时动态启动代理。主服务器和代理服务器都安装了 git,但路径不同。在主服务器上,Git 位于/opt/bitnami/git/bin/git和代理上/usr/bin/git

我配置了两个 Git 工具,Global tool configuration如下所示: Git全局工具配置

我的管道作业设置如下: Jenkins 流水线作业配置

Jenkinsfile 的相关部分在这里:

pipeline {

    agent {
        label 'ubuntu-1604'
    }

    triggers {
        pollSCM('H/5 * * * *')
    }

    tools {
        jdk 'JDK11'
        git 'agent-git'
    }

    stages {

        stage ('Checkout Source') {
            steps {
                sh "echo HOSTNAME is: `$hostname`"
                checkout scm
            }
        }

当我运行这个作业时,它失败了,因为它为 master 和 agent 使用了相同的 Git 路径。我收到以下错误:

Checking out git git@bitbucket.org:my-repo.git into /opt/bitnami/apps/jenkins/jenkins_home/workspace/test-pipeline@script to read Jenkinsfile
using credential XXXXX
Cloning the remote Git repository
Cloning repository git@bitbucket.org:my-repo.git
 > /opt/bitnami/git/bin/git init /opt/bitnami/apps/jenkins/jenkins_home/workspace/test-pipeline@script # timeout=10
Fetching upstream changes from git@bitbucket.org:my-repo.git
 > /opt/bitnami/git/bin/git --version # timeout=10
using GIT_SSH to set credentials Temporary git credentials using XXXXX's credentials
 .....
Fetching upstream changes from git@bitbucket.org:my-repo.git
using GIT_SSH to set credentials Temporary git credentials using XXXXXX's credentials
Checking out Revision b139fca90bb7b66ebb7432fc791bf0b77ef73fbb (refs/remotes/origin/some_branch)
Commit message: "Blah blah blah"
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on ubuntu-1604-agent-d1h0ha in /tmp/workspace/test-pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
using credential XXXXXX
Cloning the remote Git repository
ERROR: Error cloning remote repo 'origin'
hudson.plugins.git.GitException: Could not init /tmp/workspace/test-pipeline
    .....
    Suppressed: hudson.remoting.Channel$CallSiteStackTrace: Remote call to ubuntu-1604-agent-d1h0ha
        .....
Caused by: hudson.plugins.git.GitException: Error performing git command: /opt/bitnami/git/bin/git init /tmp/workspace/test-pipeline
    .....
Caused by: java.io.IOException: Cannot run program "/opt/bitnami/git/bin/git" (in directory "/tmp/workspace/test-pipeline"): error=2, No such file or directory
    .....
Caused by: java.io.IOException: error=2, No such file or directory
    .....

我尝试了几个修复:

  1. Git executable作业中的 设置为git-agent。这失败了,因为 master 上不存在 git 路径。
  2. 我能想到的所有组合都可以从 Jenkinsfile中选中/取消选中Lightweight checkout和删除/添加。checkout scm
  3. 我不能做这样的事情,因为我的节点是动态的。

有没有办法为主/代理定义不同的 git 路径?或者有没有办法让其中一个执行任何 git 操作?我认为取消选中Lightweight checkout会强制 master 执行所有 Git 操作,相反我认为调用checkout scmJenkinsfile 会强制代理执行所有 git 操作,但似乎都不是这样。

标签: gitjenkinsjenkins-pipeline

解决方案


推荐阅读