Question

Using Multiple Git Repositories with Jenkins - CI/CD

Hi all,

It’s been a long time since I last posted a question here but I’m back! :D

Anyway, I’m currently trying some stuff with Jenkins and I’ve started creating a small project. I have an issue where I have 3 repositories (Jenkinsfile, script and some additional libraries) which I want to include in my pipeline.

I’m using SCM and my Jenkinsfile is in one of the repositories.

So far all my attempts have failed. Here is what I have:

node {
  stage 'prepare'
  checkout scm

and I am defining the Jenkinsfile repo in the job configuration.

So what I would like to do is check out all three repositories, then use the Jenkinsfile to use the rest.

Anyone has any suggestions?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
April 15, 2021
Accepted Answer

Hi @brendRos,

I think in your case the RelativeTargetDirectory plugin of the GitSCM class should help. You can read more about it here:

Relative Target Directory

Or here

Jenkins Documentation

When you open the Jenkins Documentation(above link) select - $class: ‘GitSCM’ and then inside you’ll be able to find $class: ‘RelativeTargetDirectory’

$class: 'RelativeTargetDirectory'
relativeTargetDir
Specify a local directory (relative to the workspace root) where the Git repository will be checked out. If left empty, the workspace root itself will be used.
Type: String

Here is an example of how to use it

stages {
    stage('GitCheckout') {
        steps {
            checkout \
                scm: [ $class : 'GitSCM', branches: [[name: '*/master']], extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'subdirectory2']], submoduleCfg: [], userRemoteConfigs: [[url: 'repo2.git']]])]
        }
    }
}

Please remember to change the relativeTargetDir - subdirectory2 in this case to the one you want.

Additionally, in userRemoteConfigs: [[url: ‘repo2.git’]]] add your actual git repo.

Regards, KFSys

KFSys
Site Moderator
Site Moderator badge
April 17, 2021

Hi @brendRos,

I’m starting a new thread/answer just because the previous one became too long and it might be confusing to read afterwards.

I’m going to try and answer to both question in a single code block. The code block will include both a better readable syntax and how to post multiple Git Repositories to be checked out:


stages {
    stage('GitCheckout') {
        steps {
            checkout \
                scm: [ $class : 'GitSCM', \
                     branches: [[name: '*/master']], \
                     extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'subdirectory1']], 
                                 [ $class: 'ScmName', name: 'subdirectory1' ]], \
                     userRemoteConfigs: [[ \
                         url: 'repo1.git'  \
                     ]]
                ]
            ]
            checkout \
                scm: [ $class : 'GitSCM', \
                     branches: [[name: '*/master']], \
                     extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'subdirectory2']], 
                                 [ $class: 'ScmName', name: 'subdirectory2' ]], \
                     userRemoteConfigs: [[ \
                         url: 'repo1.git'  \
                     ]]
                ]
            ]
        }
    }
}

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel