25 May 2017

Multiple GIT tools in Jenkins Scripted Pipeline

In a Jenkins scripted pipeline script, perhaps a multi-branch pipeline job or a regular pipeline job, perhaps loaded from a Jenkinsfile in your repo or an inline script defined in the job config, this command will load a random git repo contents into your workspace

   git branch: 'master', credentialsId: 'GitHub-user-read', url: 'https://github.com/your-organization/your-repo.git'

When it runs, it will run using the 'git' tool that you set up under Manage Jenkins > Global Tools > Git (or on an Operations Center in a Global Tools menu element).

If you have more than one git tool, it will use the 1st one.

If you have more than one and want to use one that isn't first, you can't with the 'git' DSL step. But there is hope. I'll get to it below.

So imagine some situations:

  • You have multiple versions of git installed on your node agents (or on the master if you run the jobs there). Each one is in its own path. For example, two version loaded in folders /opt/tools/git-1.9 and /opt/tools/git-2.12
  • You have the same version of git installed on Linux and Windows node agents. For example, Linux is installed in /opt/tools/git/bin/git and Windows has it in c:\Program Files\Git\bin\git.exe as the two executable files.
  • Similar to the last example, the executables are in the same places but the master is Linux and the node agent is Windows (or vice versa).
When you create any sort of non-pipeline job, you get to specify which version of git you need to load the code. You can pick the version that applies to the machine on which the executors reside -- Linux or Windows, Node Agent or Master. Or you pick the version of git that you need.

When you have a pipeline job using a Jenkinsfile (or other groovy script file), you again get the option to specify the git tool that is appropriate to load the repo from git. In this case, the Jenkinsfile is loaded on the master so you use the git tool with the right path to get to the master's copy of the git executable.

But in a script file, the 'git' DSL step works with the 1st defined git tool.

If you need to use the 2nd tool, change this:


    git branch: 'master', credentialsId: 'GitHub-user-read', url: 'https://github.com/your-organization/your-repo.git'

to this, adding the 'git-tool-name' as you specified it on the Global Tools configuration page:


    checkout([
        $class: 'GitSCM', 
        branches: [[name: '*/master']], 
        gitTool: 'git-tool-name', 
        userRemoteConfigs: [
            [
                credentialsId: 'GitHub-user-read', 
                url: 'https://github.com/your-organization/your-repo.git'
            ]
        ]
    ])





2 comments: