0
0
Jenkinsdevops~30 mins

Branch-specific pipeline behavior in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
Branch-specific pipeline behavior
📖 Scenario: You work in a team that uses Jenkins to automate software builds. Different branches in your code repository need different build steps. For example, the main branch runs full tests, but feature branches run only quick checks.
🎯 Goal: Build a Jenkins pipeline script that runs different steps depending on the branch name. You will create a variable for the branch, set a condition to check the branch, and run branch-specific commands.
📋 What You'll Learn
Create a variable branchName to hold the branch name.
Add a condition to check if branchName is 'main'.
Run a shell command echo "Running full tests" if on main branch.
Run a shell command echo "Running quick checks" if on any other branch.
Print a message showing which branch is being built.
💡 Why This Matters
🌍 Real World
Teams use branch-specific pipelines to run different tests or deployments depending on the code branch, saving time and resources.
💼 Career
Knowing how to write conditional Jenkins pipelines is a key skill for DevOps engineers to automate CI/CD workflows efficiently.
Progress0 / 4 steps
1
Set the branch name variable
Create a variable called branchName and set it to the string 'main'.
Jenkins
Need a hint?

Use def branchName = 'main' to create the variable.

2
Add a condition to check the branch
Add an if statement that checks if branchName is equal to 'main'.
Jenkins
Need a hint?

Use if (branchName == 'main') to check the branch.

3
Add branch-specific shell commands
Inside the if block, add sh 'echo "Running full tests"'. Add an else block with sh 'echo "Running quick checks"'.
Jenkins
Need a hint?

Use sh 'echo "Running full tests"' inside the if block and sh 'echo "Running quick checks"' inside the else block.

4
Print the branch being built
Add a println statement after the if-else block to print "Building branch: " followed by the branchName variable.
Jenkins
Need a hint?

Use println("Building branch: ${branchName}") to print the branch name.