0
0
Jenkinsdevops~20 mins

Branch-specific pipeline behavior in Jenkins - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Branch Mastery in Jenkins Pipelines
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of branch-specific Jenkins pipeline snippet
Given this Jenkins pipeline snippet, what will be printed when the branch is 'feature/new-ui'?
Jenkins
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        script {
          if (env.BRANCH_NAME == 'main') {
            echo 'Building main branch'
          } else {
            echo "Building branch: ${env.BRANCH_NAME}"
          }
        }
      }
    }
  }
}
ABuilding main branch
BNo output
CError: BRANCH_NAME not defined
DBuilding branch: feature/new-ui
Attempts:
2 left
💡 Hint
Check the condition comparing env.BRANCH_NAME to 'main'.
Configuration
intermediate
2:00remaining
Correct Jenkinsfile syntax for branch-specific stage
Which Jenkinsfile snippet correctly runs the 'Deploy' stage only on the 'release' branch?
A
stage('Deploy') {
  when {
    branch 'release'
  }
  steps {
    echo 'Deploying release branch'
  }
}
B
stage('Deploy') {
  when {
    environment name: 'BRANCH_NAME', value: 'release'
  }
  steps {
    echo 'Deploying release branch'
  }
}
C
stage('Deploy') {
  if (env.BRANCH_NAME == 'release') {
    steps {
      echo 'Deploying release branch'
    }
  }
}
D
stage('Deploy') {
  when {
    branch == 'release'
  }
  steps {
    echo 'Deploying release branch'
  }
}
Attempts:
2 left
💡 Hint
Use the 'when' directive with 'branch' condition.
Troubleshoot
advanced
2:00remaining
Why does the Jenkins pipeline stage run on all branches despite 'when' condition?
A Jenkins pipeline has this stage: stage('Test') { when { branch 'develop' } steps { echo 'Running tests' } } But the stage runs on 'feature/login' branch too. What is the most likely cause?
AThe pipeline is running in a multibranch job but BRANCH_NAME is not set properly
BThe 'branch' condition is case-sensitive and branch name is 'Develop' with uppercase D
CThe 'when' directive is ignored if placed inside 'steps' block
DThe 'when' condition requires 'expression' block to work
Attempts:
2 left
💡 Hint
Check environment variables in multibranch pipelines.
🔀 Workflow
advanced
2:00remaining
Order of execution in branch-specific Jenkins pipeline
Given this Jenkins pipeline snippet, what is the order of printed messages when running on branch 'hotfix'?
Jenkins
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'
      }
    }
  }
}
A1,2,3
B3,1
C1,3
D2,3
Attempts:
2 left
💡 Hint
Check which stages run on 'hotfix' branch.
Best Practice
expert
3:00remaining
Best practice for managing branch-specific Jenkins pipeline logic
Which approach is best to maintain complex branch-specific logic in Jenkins pipelines for multiple branches with different build steps?
ACreate separate Jenkinsfiles per branch and configure jobs to use the correct file
BUse a shared library with functions that handle branch-specific logic called from a simple Jenkinsfile
CUse multiple 'when' conditions inside a single Jenkinsfile with complex nested logic
DUse environment variables to disable stages manually per branch
Attempts:
2 left
💡 Hint
Think about maintainability and code reuse.