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.

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

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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'  \
                     ]]
                ]
            ]
        }
    }
}