pipeline {
agent any
stages {
stage('Build') {
steps {
script {
if (env.BRANCH_NAME == 'main') {
echo 'Building main branch'
} else {
echo "Building branch: ${env.BRANCH_NAME}"
}
}
}
}
}
}The pipeline checks if the branch name is exactly 'main'. Since the branch is 'feature/new-ui', it prints the else branch message with the branch name.
Option A uses the correct 'when' syntax with 'branch' condition. Option A uses environment condition incorrectly. Option A has invalid syntax mixing 'if' inside stage. Option A uses invalid 'branch ==' syntax.
In multibranch pipelines, the 'branch' condition depends on the BRANCH_NAME environment variable. If it's not set correctly, the condition always passes. Case sensitivity is not the issue here.
pipeline {
agent any
stages {
stage('Init') {
steps {
echo 'Init stage'
}
}
stage('Build') {
when {
branch 'main'
}
steps {
echo 'Build stage'
}
}
stage('Test') {
when {
not {
branch 'main'
}
}
steps {
echo 'Test stage'
}
}
}
}'Init' stage always runs. 'Build' runs only on 'main' branch, so skipped. 'Test' runs on all branches except 'main', so it runs on 'hotfix'.
Using a shared library centralizes branch-specific logic, making the Jenkinsfile simpler and easier to maintain. Multiple 'when' conditions can get messy. Separate Jenkinsfiles increase duplication. Manual environment variable toggling is error-prone.