0
0
Jenkinsdevops~15 mins

Conditional deployment logic in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
Conditional deployment logic
📖 Scenario: You are managing a Jenkins pipeline that deploys applications to different environments based on the branch name. For example, code from the main branch should deploy to production, while code from the develop branch should deploy to staging.
🎯 Goal: Build a Jenkins pipeline script that checks the branch name and conditionally deploys to the correct environment.
📋 What You'll Learn
Create a variable to hold the branch name
Add a variable to define the production branch name
Use conditional logic to decide deployment environment
Print the deployment environment based on the branch
💡 Why This Matters
🌍 Real World
In real projects, deployment pipelines must decide where to deploy code based on the branch or tag. This prevents accidental production deployments.
💼 Career
DevOps engineers often write Jenkins pipelines with conditional steps to automate safe and efficient deployments.
Progress0 / 4 steps
1
Set the branch name variable
Create a variable called branchName and set it to the string develop.
Jenkins
Need a hint?

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

2
Define the production branch name
Create a variable called productionBranch and set it to the string main.
Jenkins
Need a hint?

Use def productionBranch = 'main' to define the production branch.

3
Add conditional deployment logic
Use an if statement to check if branchName equals productionBranch. If yes, set a variable deployEnv to production. Otherwise, set deployEnv to staging.
Jenkins
Need a hint?

Use if (branchName == productionBranch) { ... } else { ... } to set deployEnv.

4
Print the deployment environment
Write a println statement to display the text Deploying to: followed by the value of deployEnv.
Jenkins
Need a hint?

Use println("Deploying to: ${deployEnv}") to print the deployment environment.