Complete the code to specify the branch name in the Jenkins pipeline.
pipeline {
agent any
stages {
stage('Build') {
when {
branch [1]
}
steps {
echo 'Building branch'
}
}
}
}The branch condition expects the branch name as a string. Here, 'main' is the correct branch to trigger the build.
Complete the code to run the stage only on the 'develop' branch.
stage('Test') { when { branch [1] } steps { echo 'Running tests on develop branch' } }
The when condition with branch 'develop' ensures this stage runs only on the develop branch.
Fix the error in the branch condition to correctly check for the 'feature' branch.
when {
branch [1]
}The branch name must be a string literal in quotes. Using 'feature' is correct syntax.
Fill both blanks to run the stage only on branches starting with 'release' and skip others.
stage('Deploy') { when { expression { return env.BRANCH_NAME.[1]('release') [2] true } } steps { echo 'Deploying release branch' } }
The startsWith method checks if the branch name begins with 'release'. The == true ensures the expression returns a boolean.
Fill all three blanks to create a pipeline that runs different stages for 'main', 'develop', and other branches.
pipeline {
agent any
stages {
stage('Build') {
when {
branch [1]
}
steps {
echo 'Building main branch'
}
}
stage('Test') {
when {
branch [2]
}
steps {
echo 'Testing develop branch'
}
}
stage('Deploy') {
when {
not {
branch [3]
}
}
steps {
echo 'Deploying other branches'
}
}
}
}The first stage runs on 'main', the second on 'develop', and the deploy stage runs on branches that are not 'develop'.