Question

Using Multiple Git Repositories with Jenkins - CI/CD

Posted April 15, 2021 2.4k views
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?

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.

×
Submit an Answer
2 answers

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

  • Hi,

    Thank you so much I can see that it’s checking out the git repository. Having said that, I’m unable to execute the script inside the repository.

    I’m trying as this

        stage('script') {
            sh `bash myscript.sh`
        }
    
    

    Have I done something else wrong?

    • Hi @brendRos,

      You just need to add subdirectory you mentioned in your Jenkinsfile here;

      [$class: 'RelativeTargetDirectory', relativeTargetDir: 'subdirectory2']]
      

      To the path for the script so basycally:

          stage('script') {
              sh `bash subdirectory2/myscript.sh`
          }
      
      • That worked as expected, great!

        I do have another question, I’m trying to add a second git repository to my Jenkinsfile but I’m not sure why it’s not working, I’ve been trying with separating them with a coma like so

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

        And some other ways but to no avail.

        Additionally, I’ve find it difficult to read to be honest, is there any better way to syntaxing it?

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

  • Additionally,

    just remembered, if you decide to make your Git Repo Private and you have added your authentication in Jekins and Git, this is how you can use it in your checkout:

                         userRemoteConfigs: [[ \
                             credentialsId: 'IDHERE',
                             url: 'repo1.git'  \
                         ]]