0
0
Jenkinsdevops~30 mins

Avoiding hard-coded values in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
Avoiding Hard-Coded Values in Jenkins Pipeline
📖 Scenario: You are creating a Jenkins pipeline script to build and deploy a simple application. To make your pipeline flexible and easy to maintain, you want to avoid hard-coding values like the branch name and build environment directly in the script.
🎯 Goal: Build a Jenkins pipeline script that uses variables for branch name and environment instead of hard-coded values. This will help you reuse the pipeline for different branches and environments easily.
📋 What You'll Learn
Create variables for branch name and environment
Use these variables in the pipeline stages
Print the values of these variables during the build
Avoid any hard-coded strings for branch or environment in the pipeline steps
💡 Why This Matters
🌍 Real World
In real projects, hard-coded values make pipelines rigid and hard to update. Using variables allows teams to reuse pipelines for multiple branches and environments without changing the code.
💼 Career
DevOps engineers and Jenkins administrators often create pipelines that must work across many projects and environments. Knowing how to avoid hard-coded values is essential for writing maintainable and scalable CI/CD pipelines.
Progress0 / 4 steps
1
Create variables for branch and environment
Create two variables called branchName and environment and set them to "main" and "production" respectively.
Jenkins
Need a hint?

Use def to declare variables in Jenkins scripted pipeline.

2
Use variables in the pipeline stages
Add a pipeline block with an agent any. Inside stages, create a stage called Build that echoes the message "Building branch: ${branchName}" using the branchName variable.
Jenkins
Need a hint?

Use pipeline and stage blocks. Use echo to print messages.

3
Add a Deploy stage using the environment variable
Inside the existing pipeline block, add a new stage called Deploy that echoes the message "Deploying to environment: ${environment}" using the environment variable.
Jenkins
Need a hint?

Add another stage block inside stages with echo step.

4
Print both variables in the pipeline
Add a final stage called Print Info inside the pipeline that echoes both branchName and environment in the format: "Branch: ${branchName}, Environment: ${environment}".
Jenkins
Need a hint?

Use echo to print both variables in one message inside a new stage.