Complete the code to checkout the current branch in Jenkins pipeline.
checkout([$class: 'GitSCM', branches: [[name: '[1]']], userRemoteConfigs: [[url: 'https://github.com/example/repo.git']]])
Using env.BRANCH_NAME dynamically checks out the current branch in Jenkins pipeline.
Complete the code to select the Jenkinsfile path based on the branch name.
def jenkinsfilePath = (env.BRANCH_NAME == 'main') ? '[1]' : 'Jenkinsfile.dev'
The default Jenkinsfile is usually named Jenkinsfile. For the main branch, we use this path.
Fix the error in the pipeline script to load the correct Jenkinsfile per branch.
pipeline {
agent any
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Build') {
steps {
load '[1]'
}
}
}
}The Jenkinsfile path should be constructed with a dash before the branch name, like Jenkinsfile-feature.
Fill both blanks to define a pipeline that loads a Jenkinsfile based on the branch name and runs it.
def file = 'Jenkinsfile[1][2]' load file
The Jenkinsfile name is constructed by joining 'Jenkinsfile', a dash, and the branch name from env.BRANCH_NAME.
Fill all three blanks to create a Jenkins pipeline that checks out the current branch, selects the Jenkinsfile path, and loads it.
pipeline {
agent any
stages {
stage('Checkout') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '[1]']], userRemoteConfigs: [[url: 'https://github.com/example/repo.git']]])
}
}
stage('Load Jenkinsfile') {
steps {
def file = '[2][3]'
load file
}
}
}
}The pipeline checks out the current branch using env.BRANCH_NAME, then constructs the Jenkinsfile path by joining 'Jenkinsfile-' and the branch name from env.BRANCH_NAME.