0
0
Jenkinsdevops~20 mins

Jenkinsfile per branch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Jenkinsfile Branch Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of Jenkinsfile branch condition
Given this Jenkinsfile snippet, what will be the output when the branch is 'feature-xyz'?
Jenkins
pipeline {
  agent any
  stages {
    stage('Build') {
      when {
        branch 'main'
      }
      steps {
        echo 'Building main branch'
      }
    }
    stage('Feature Build') {
      when {
        branch pattern: 'feature-.*', comparator: 'regex'
      }
      steps {
        echo 'Building feature branch'
      }
    }
  }
}
ANo output
BBuilding main branch
CBuilding feature branch
DBuild failed with error
Attempts:
2 left
💡 Hint
Check the branch conditions in the 'when' blocks.
Configuration
intermediate
2:00remaining
Correct Jenkinsfile syntax for per-branch pipeline
Which Jenkinsfile snippet correctly runs different stages for 'main' and 'develop' branches?
A
pipeline {
  agent any
  stages {
    stage('Main Build') {
      when { branch 'main' }
      steps { echo 'Build main' }
    }
    stage('Develop Build') {
      when { branch 'develop' }
      steps { echo 'Build develop' }
    }
  }
}
B
pipeline {
  agent any
  stages {
    stage('Build') {
      when { branch 'main' || branch 'develop' }
      steps { echo 'Build main or develop' }
    }
  }
}
C
pipeline {
  agent any
  stages {
    stage('Main Build') {
      when { branch == 'main' }
      steps { echo 'Build main' }
    }
    stage('Develop Build') {
      when { branch == 'develop' }
      steps { echo 'Build develop' }
    }
  }
}
D
pipeline {
  agent any
  stages {
    stage('Build') {
      when { branch 'main' && branch 'develop' }
      steps { echo 'Build both' }
    }
  }
}
Attempts:
2 left
💡 Hint
Check the syntax of the 'when' condition for branch matching.
Troubleshoot
advanced
2:00remaining
Why does the Jenkinsfile fail on branch detection?
This Jenkinsfile snippet fails with a syntax error. What is the cause?
Jenkins
pipeline {
  agent any
  stages {
    stage('Build') {
      when {
        branch == 'main'
      }
      steps {
        echo 'Building main'
      }
    }
  }
}
AAgent declaration is incorrect
BUsing 'branch == 'main'' is invalid syntax in 'when' condition
CMissing 'steps' block inside 'stage'
DPipeline block is missing 'environment' section
Attempts:
2 left
💡 Hint
Check the syntax rules for 'when' branch condition in declarative pipeline.
🔀 Workflow
advanced
2:00remaining
Jenkinsfile multi-branch pipeline behavior
In a multi-branch pipeline, what happens if a Jenkinsfile contains a 'when' condition that excludes the current branch?
AThe stage is skipped and pipeline continues with other stages
BJenkins retries the pipeline on a different branch automatically
CThe entire pipeline is skipped and marked as success
DThe pipeline fails immediately with an error
Attempts:
2 left
💡 Hint
Think about how 'when' conditions control stage execution.
Best Practice
expert
3:00remaining
Best practice for managing Jenkinsfiles per branch
What is the recommended approach to maintain different Jenkinsfiles for multiple branches in a Git repository?
ADuplicate the entire repository per branch with its own Jenkinsfile
BCreate separate Jenkinsfiles named Jenkinsfile-main, Jenkinsfile-develop, and switch Jenkinsfile path in Jenkins config per branch
CStore Jenkinsfiles in separate folders per branch and configure Jenkins to load from those folders
DUse a single Jenkinsfile with 'when' conditions to handle branch-specific logic
Attempts:
2 left
💡 Hint
Consider maintainability and Jenkins multi-branch pipeline features.