Multiple SCM repositories in Jenkins - Commands & Configuration
pipeline {
agent any
stages {
stage('Checkout Multiple Repos') {
steps {
script {
// Checkout main app repository
checkout([$class: 'GitSCM', branches: [[name: '*/main']],
userRemoteConfigs: [[url: 'https://github.com/example-org/main-app.git']]])
// Checkout shared library repository into 'libs' folder
dir('libs') {
checkout([$class: 'GitSCM', branches: [[name: '*/main']],
userRemoteConfigs: [[url: 'https://github.com/example-org/shared-libs.git']]])
}
}
}
}
stage('Build') {
steps {
echo 'Building the project using code from multiple repositories'
}
}
}
}This Jenkinsfile defines a pipeline with two stages. In the first stage, it checks out two Git repositories: the main app repository into the workspace root, and a shared library repository into a subfolder named 'libs'. This allows the build stage to use code from both places.
The checkout step uses the Git plugin to pull code. The dir('libs') block changes the folder before checking out the second repo.
jenkins-jobs --conf jenkins.ini update my-multi-scm-job.yaml
java -jar jenkins-cli.jar -s http://localhost:8080 build my-multi-scm-job -s-s - Waits for the build to complete and shows the console outputjava -jar jenkins-cli.jar -s http://localhost:8080 console my-multi-scm-jobIf you remember nothing else from this pattern, remember: Jenkins can check out multiple source code repositories into different folders in one job using multiple checkout steps.
Practice
What is the main benefit of using multiple SCM repositories in a Jenkins job?
Solution
Step 1: Understand Jenkins SCM usage
Jenkins uses SCM to get source code for building projects.Step 2: Identify multiple SCM benefit
Multiple SCM lets Jenkins pull from many repositories in one job, useful for projects with dependencies.Final Answer:
It allows Jenkins to fetch code from several repositories in one build. -> Option AQuick Check:
Multiple SCM = fetch multiple repos [OK]
- Thinking it merges repos permanently
- Assuming it improves server speed
- Believing it limits branches
Which Jenkins pipeline syntax correctly defines two SCM repositories using checkout steps?
checkout([$class: 'GitSCM', userRemoteConfigs: [[url: 'https://repo1.git']]])
checkout([$class: 'GitSCM', userRemoteConfigs: [[url: 'https://repo2.git']]])
2) checkout([$class: 'GitSCM', userRemoteConfigs: [[url: 'https://repo1.git', url: 'https://repo2.git']]])
3) checkout([$class: 'GitSCM', url: 'https://repo1.git'])
checkout([$class: 'GitSCM', url: 'https://repo2.git'])
4) checkout([$class: 'GitSCM', userRemoteConfigs: ['https://repo1.git', 'https://repo2.git']])Solution
Step 1: Check syntax for multiple SCM in Jenkins pipeline
Eachcheckoutstep handles one SCM repo; multiple repos require multiplecheckoutcalls.Step 2: Analyze options
Option 1 uses two separatecheckoutcalls with correctuserRemoteConfigs. Option 2 tries to combine repos in one config incorrectly. Option 3 uses wrong keyurldirectly. Option 4 uses wrong list format.Final Answer:
Option 1 -> Option AQuick Check:
Separate checkout per repo = Option 1 [OK]
- Combining multiple URLs in one userRemoteConfigs entry
- Using 'url' key instead of userRemoteConfigs
- Passing URLs as plain list without map
Given this Jenkins pipeline snippet, what will be the result?
stage('Checkout') {
steps {
checkout([$class: 'GitSCM', userRemoteConfigs: [[url: 'https://repo1.git']]])
checkout([$class: 'GitSCM', userRemoteConfigs: [[url: 'https://repo2.git']]])
}
}Choose the correct statement about the workspace after this stage runs.
Solution
Step 1: Understand default checkout behavior
By default, Jenkins checks out each repo into the same workspace folder, overwriting previous content.Step 2: Analyze the effect of two checkout calls
The second checkout replaces the first repo's files because both use the root workspace folder without specifying subdirectories.Final Answer:
The workspace contains only repo2 code, repo1 is overwritten. -> Option BQuick Check:
Multiple checkout without directories overwrites workspace [OK]
- Assuming repos merge automatically
- Thinking Jenkins creates subfolders by default
- Believing multiple checkouts cause build failure
In a Jenkins job configured with multiple SCM repositories, the build fails with this error: hudson.plugins.git.GitException: Command "git clone" failed. What is the most likely cause?
Solution
Step 1: Understand the error context
Git clone failure usually means the repository URL is invalid or missing.Step 2: Check multiple SCM config
IfuserRemoteConfigsis empty or missing URLs, Jenkins cannot clone repos, causing this error.Final Answer:
The userRemoteConfigs list is empty or missing URLs. -> Option DQuick Check:
Missing repo URLs cause git clone failure [OK]
- Blaming workspace cleanup for git clone errors
- Assuming Jenkinsfile syntax causes git clone failure
- Ignoring network issues as cause
You want to configure a Jenkins pipeline to checkout two Git repositories into separate folders repo1 and repo2 inside the workspace. Which pipeline snippet correctly achieves this?
checkout([$class: 'GitSCM', userRemoteConfigs: [[url: 'https://repo1.git']], extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'repo1']]])
checkout([$class: 'GitSCM', userRemoteConfigs: [[url: 'https://repo2.git']], extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'repo2']]])Solution
Step 1: Understand RelativeTargetDirectory extension
This extension tells Jenkins to checkout the repo into a specified subfolder inside the workspace.Step 2: Analyze the snippet
Each checkout uses RelativeTargetDirectory with different folder names, so repos go into separate folders without overwriting.Final Answer:
This snippet correctly checks out both repos into separate folders. -> Option CQuick Check:
RelativeTargetDirectory separates repos in workspace [OK]
- Not using RelativeTargetDirectory causes overwriting
- Assuming Jenkins merges repos automatically
- Believing second checkout is ignored
Ready to prove it?
Pass the test and this pattern moves unknown
