📖 Scenario: You work in a team where each Git branch needs its own Jenkins pipeline configuration. This helps run different build steps depending on the branch.
🎯 Goal: Create a Jenkinsfile that runs different stages based on the current Git branch name.
📋 What You'll Learn
Create a variable to hold the current Git branch name
Create a variable to hold the build stage name based on the branch
Use a when condition to run a stage only on the main branch
Print the branch name and the selected build stage
💡 Why This Matters
🌍 Real World
Teams often need different build or test steps for different branches, like main, develop, or feature branches. This Jenkinsfile setup helps automate that.
💼 Career
Knowing how to customize Jenkins pipelines per branch is a key skill for DevOps engineers and CI/CD pipeline developers.
Progress0 / 4 steps
1
Get the current Git branch name
Create a variable called branchName that gets the current Git branch name using env.BRANCH_NAME.
Jenkins
Hint
The current branch name is stored in env.BRANCH_NAME.
2
Set build stage name based on branch
Add a variable called buildStage in the environment section. Set it to "Build Main" if branchName is "main", otherwise set it to "Build Feature".
Jenkins
Hint
Use a ternary operator to set buildStage based on branchName.
3
Add a stage that runs only on main branch
Add a stage named Build Main that runs only when branchName is "main". Use the when directive with expression { branchName == 'main' }. Inside the stage, add a steps block with a echo command that prints "Building main branch".
Jenkins
Hint
The when directive controls if a stage runs. Use expression { branchName == 'main' } to check the branch.
4
Print the branch and build stage
Add a stage named Print Info that runs on all branches. Inside steps, add two echo commands: one to print "Current branch: ${branchName}" and another to print "Selected build stage: ${buildStage}".
Jenkins
Hint
Use echo inside steps to print variables with ${} syntax.
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
Step 1: Understand Jenkinsfile per branch concept
Each branch can have its own Jenkinsfile, allowing different pipeline steps or settings per branch.
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.
Final Answer:
It allows customizing the pipeline for each branch independently. -> Option A
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?
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.
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.
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
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'.
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'.
Final Answer:
Deploying feature branch -> Option C
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
Step 1: Understand env variable usage
In declarative pipelines, env variables must be accessed inside a script block to use Groovy expressions.
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.
Final Answer:
The env variable is used outside a script block. -> Option A
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
Step 1: Understand single Jenkinsfile with branch conditions
You can use when { branch 'pattern' } in declarative pipelines to run stages only on matching branches.
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.
Final Answer:
Use when { branch 'feature/*' } conditions inside the Jenkinsfile to run different stages. -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.