3

I have the following problem: I am running Jenkins on GCE (Google Compute Engine) and it is configured to start up agents dynamically when needed. The master and agents both have git installed, but at different paths. On the master Git is located at /opt/bitnami/git/bin/git and on the agent at /usr/bin/git.

I have two Git tools configured in Global tool configuration as shown below: Git global tool configuration

My pipeline job is setup as below: Jenkins pipeline job configuration

The relevant part of the Jenkinsfile here:

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
            }
        }

When I run this job, it fails because it uses the same Git path for both master and the agent. I get the following error:

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
    .....

I have tried several fixes:

  1. Set the Git executable in the job to git-agent. This fails because the git path does not exist on master.
  2. All combinations I can think of checking/unchecking Lightweight checkout and removing/adding checkout scm from the Jenkinsfile.
  3. I cannot do something like this because my nodes are dynamic.

Is there any way to define different git paths for master/agents? Or is there a way to have only one of them perform any git operations? I thought by unchecking Lightweight checkout that would force master to do all Git operations, conversely I thought calling checkout scm in the Jenkinsfile would force the agent to do all git operations, but neither of those seem to be the case.

wearebob
  • 376
  • 2
  • 15
  • Why not use `install automatically` setting? – Sers Dec 10 '19 at 21:06
  • It does not need to be installed anywhere, both machines already have git installed. I did try checking `install automatically` just to test it out and had the same issue – wearebob Dec 10 '19 at 21:48
  • I was able to get this working for now by setting up a hard link for the git path, i.e. `ln /opt/bitnami/git/bin/git /usr/bin/git` but this doesn't feel like a good solution so I'm leaving this open in hopes of a solution through Jenkins/code – wearebob Dec 10 '19 at 22:05
  • is there any chance that `git` tool is not supported? – Yuri G. Dec 11 '19 at 06:28
  • Dunno about a pipeline config,but on a std config you can set/override the Global config at the [node level](https://stackoverflow.com/a/35599130/598141). Am sure that's covered in the syntax. Alternatively, if these are the default locations and are in the system profile path (ie: /etc/profile), then you could pick "Default" or "System". – Ian W Dec 11 '19 at 08:12
  • @YuriG. I don't believe so, I have the Git plugin installed and the Git client plugin which should give me the ability to use the git tool. I also see it listed as one of the valid options in the error message if I use an invalid tool name – wearebob Dec 11 '19 at 16:45
  • @IanW I would think there's a way to cover that in the pipeline syntax too, but I haven't been able to find a way yet. I was thinking tool was the way to do that, but no such luck. – wearebob Dec 11 '19 at 16:46
  • @wearebob try to use `tool git ''Default` for the checkout stage only – Yuri G. Dec 11 '19 at 16:59
  • Can you abstract using [withEnv](https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#withenv-set-environment-variables) ? – Ian W Dec 12 '19 at 03:34
  • Were you able to overcome this issue? I am in same boat and can't find any working solution. – ManishChristian Mar 03 '21 at 20:31
  • @ManishChristian I'm not able to check right now what I ended up doing, but I believe I'm still using the sort of hacky solution above. Set up a link on the master so that the same git path works on both machines – wearebob Mar 08 '21 at 16:30
  • 3
    @wearebob, thank you. By the way, I was able to fix this issue by specifying git location under tools section for Windows agent by going into Jenkins node section. – ManishChristian Mar 08 '21 at 20:50

1 Answers1

4

Posting an answer from @ManishChristian's comment, since it worked for me:

In the Jenkins node configuration there is an option at the bottom to add tool directories specific to that node. Git path can be specified there

mattgately
  • 932
  • 8
  • 22