📖 Scenario: You are working as a DevOps engineer. Your team wants to automate the process of building, testing, and deploying a simple application using Jenkins. This will save time and reduce errors by running these steps automatically whenever code changes.
🎯 Goal: Build a simple Jenkins pipeline script that models the CI/CD process with stages for build, test, and deploy. You will create the pipeline steps one by one and finally print the pipeline structure.
📋 What You'll Learn
Create a Jenkins pipeline script with three stages: Build, Test, Deploy
Define a variable for the application name
Use the variable in the pipeline stages
Print the pipeline stages and application name at the end
💡 Why This Matters
🌍 Real World
Automating software build, test, and deployment saves time and reduces human errors in real projects.
💼 Career
Understanding Jenkins pipelines is essential for DevOps roles to implement continuous integration and continuous delivery.
Progress0 / 4 steps
1
Define the application name variable
Create a variable called appName and set it to the string "MyApp".
Jenkins
Hint
Use def appName = "MyApp" to create the variable.
2
Create the Jenkins pipeline with stages
Add a pipeline block with an agent any and a stages section containing three stages named Build, Test, and Deploy.
Jenkins
Hint
Use pipeline { agent any stages { stage('Build') { steps { ... } } } structure.
3
Use the appName variable inside each stage
Inside each stage's steps, add an echo statement that prints the action and uses the appName variable with ${appName} syntax. For example, in the Build stage, print "Building ${appName}...".
Jenkins
Hint
Use echo "Building ${appName}..." inside the Build stage steps, and similarly for Test and Deploy.
4
Print the pipeline stages and application name
Add a post section with a always block that prints "Pipeline completed for ${appName}" using echo.
Jenkins
Hint
Use post { always { echo "Pipeline completed for ${appName}" } } after the stages block.
Practice
(1/5)
1. What is the main purpose of a CI/CD pipeline in Jenkins?
easy
A. To manually run tests on software
B. To store software documentation
C. To write code faster
D. To automate building, testing, and deploying software
Solution
Step 1: Understand CI/CD pipeline role
A CI/CD pipeline automates the process of building, testing, and deploying software to make delivery faster and more reliable.
Step 2: Identify Jenkins pipeline function
Jenkins pipelines organize these automation steps into stages for clarity and control.
Final Answer:
To automate building, testing, and deploying software -> Option D
In Jenkins declarative pipelines, stages are defined using stage('Name') { ... } syntax.
Step 2: Check each option's syntax
stage('Build') { steps { echo 'Building...' } } uses correct syntax with parentheses and braces. Options B, C, and D use invalid symbols or assignment.