Bird
Raised Fist0
Jenkinsdevops~20 mins

Jenkinsfile per branch - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main benefit of having a separate Jenkinsfile in each branch?
easy
A. It allows customizing the pipeline for each branch independently.
B. It forces all branches to use the same pipeline configuration.
C. It disables pipeline execution on branches without a Jenkinsfile.
D. It merges all branch pipelines into one global pipeline.

Solution

  1. Step 1: Understand Jenkinsfile per branch concept

    Each branch can have its own Jenkinsfile, allowing different pipeline steps or settings per branch.
  2. Step 2: Compare options

    It allows customizing the pipeline for each branch independently. correctly states the benefit. Options A, B, and D describe incorrect behaviors.
  3. Final Answer:

    It allows customizing the pipeline for each branch independently. -> Option A
  4. Quick Check:

    Separate Jenkinsfiles = branch-specific pipelines [OK]
Hint: Separate Jenkinsfiles mean separate pipelines per branch [OK]
Common Mistakes:
  • Thinking one Jenkinsfile applies to all branches
  • Assuming Jenkinsfile absence disables pipeline
  • Believing pipelines merge automatically
2. Which of the following is the correct syntax to checkout from a specific branch in a multibranch pipeline?
easy
A. pipeline { agent any; stages { stage('Build') { steps { checkout([$class: 'GitSCM', branches: ['refs/heads/feature'], userRemoteConfigs: [[url: 'https://repo.git']]]) } } } }
B. pipeline { agent any; stages { stage('Build') { steps { git branch: 'main', url: 'https://repo.git' } } } }
C. pipeline { agent any; stages { stage('Build') { steps { load 'Jenkinsfile' } } } }
D. pipeline { agent any; stages { stage('Build') { steps { script { checkout scm } } } } }

Solution

  1. Step 1: Identify correct syntax for branch checkout

    Using the git step with branch and url is the simplest correct way to checkout a specific branch.
  2. Step 2: Analyze options

    The snippet using git branch: 'main', url: 'https://repo.git' is correct. The snippet using checkout([$class: 'GitSCM', branches: ['refs/heads/feature'], userRemoteConfigs: [[url: 'https://repo.git']]]) has invalid syntax for branches (expects list of maps like [[name: 'refs/heads/feature']]). The snippet using script { checkout scm } checks out the current SCM without specifying a branch. The snippet using load 'Jenkinsfile' loads a Groovy script but does not checkout a branch.
  3. Final Answer:

    pipeline { agent any; stages { stage('Build') { steps { git branch: 'main', url: 'https://repo.git' } } } } -> Option B
  4. Quick Check:

    Use git step with branch and url [OK]
Hint: Use git step with branch and url to specify branch [OK]
Common Mistakes:
  • Using load to get Jenkinsfile instead of checkout
  • Not specifying branch in checkout
  • Confusing checkout scm with git step
3. Given this Jenkinsfile snippet inside a branch:
pipeline {
  agent any
  stages {
    stage('Test') {
      steps {
        script {
          if (env.BRANCH_NAME == 'main') {
            echo 'Deploying main branch'
          } else {
            echo 'Deploying feature branch'
          }
        }
      }
    }
  }
}

What will Jenkins print if the branch is named feature/login?
medium
A. No output
B. Deploying main branch
C. Deploying feature branch
D. Pipeline fails with error

Solution

  1. Step 1: Understand branch name condition

    The script checks if env.BRANCH_NAME equals 'main'. If yes, it prints 'Deploying main branch', else 'Deploying feature branch'.
  2. Step 2: Apply branch name 'feature/login'

    Since the branch is 'feature/login', it does not equal 'main', so the else branch runs printing 'Deploying feature branch'.
  3. Final Answer:

    Deploying feature branch -> Option C
  4. Quick Check:

    Branch name check leads to else output [OK]
Hint: Check branch name string exactly for condition [OK]
Common Mistakes:
  • Assuming partial match triggers main branch output
  • Confusing env.BRANCH_NAME with other variables
  • Expecting no output if branch is not main
4. You have a Jenkinsfile in your feature branch but the pipeline fails with error: groovy.lang.MissingPropertyException: No such property: env for class. What is the likely cause?
medium
A. The env variable is used outside a script block.
B. The Jenkinsfile is missing the pipeline block.
C. The branch name is incorrect in the Jenkinsfile.
D. The Jenkinsfile is not committed to the branch.

Solution

  1. Step 1: Understand env variable usage

    In declarative pipelines, env variables must be accessed inside a script block to use Groovy expressions.
  2. Step 2: Analyze error cause

    The error indicates env is not recognized, likely because it is used outside a script block where Groovy code runs.
  3. Final Answer:

    The env variable is used outside a script block. -> Option A
  4. Quick Check:

    Use env only inside script blocks [OK]
Hint: Access env variables only inside script blocks [OK]
Common Mistakes:
  • Using env directly in steps without script
  • Assuming missing pipeline block causes env error
  • Not committing Jenkinsfile to branch
5. You want to maintain a single Jenkinsfile in the main branch but customize pipeline steps for feature/* branches without creating Jenkinsfiles in each feature branch. Which approach is best?
hard
A. Configure Jenkins to ignore feature branches.
B. Create symbolic links to the main Jenkinsfile in each feature branch.
C. Use separate Jenkinsfiles in each feature branch and merge them manually.
D. Use when { branch 'feature/*' } conditions inside the Jenkinsfile to run different stages.

Solution

  1. Step 1: Understand single Jenkinsfile with branch conditions

    You can use when { branch 'pattern' } in declarative pipelines to run stages only on matching branches.
  2. Step 2: Evaluate options

    Use when { branch 'feature/*' } conditions inside the Jenkinsfile to run different stages. uses branch conditions inside one Jenkinsfile, which fits the requirement. Create symbolic links to the main Jenkinsfile in each feature branch. is not practical or supported. Use separate Jenkinsfiles in each feature branch and merge them manually. contradicts the goal of single Jenkinsfile. Configure Jenkins to ignore feature branches. ignores feature branches, not customizing them.
  3. Final Answer:

    Use when { branch 'feature/*' } conditions inside the Jenkinsfile to run different stages. -> Option D
  4. Quick Check:

    Branch conditions enable single Jenkinsfile customization [OK]
Hint: Use when branch conditions for per-branch steps [OK]
Common Mistakes:
  • Trying to link Jenkinsfiles across branches
  • Creating multiple Jenkinsfiles despite single file goal
  • Ignoring feature branches instead of customizing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard