📖 Scenario: You work in a team that builds software. Your team wants to automate how code is tested and deployed. Instead of clicking buttons in Jenkins every time, you want to write the pipeline steps as code. This way, the pipeline can be saved, shared, and changed easily.
🎯 Goal: You will create a simple Jenkins pipeline script as code. This script will define steps to build and test a project. You will see how writing pipeline as code helps keep automation clear and repeatable.
📋 What You'll Learn
Create a Jenkins pipeline script with a pipeline block
Add an agent section to run the pipeline on any available node
Add a stages section with two stages: Build and Test
In the Build stage, add a step to print Building the project...
In the Test stage, add a step to print Running tests...
💡 Why This Matters
🌍 Real World
Teams use Pipeline as Code to automate software builds and tests reliably without manual clicks.
💼 Career
Knowing how to write Jenkins pipelines as code is a key skill for DevOps engineers and automation specialists.
Progress0 / 4 steps
1
Create the pipeline block
Write a Jenkins pipeline script starting with a pipeline block and an agent any section.
Jenkins
Hint
The pipeline block is the main container. agent any means Jenkins can run this anywhere.
2
Add stages section
Inside the pipeline block, add a stages section with two stages named Build and Test. Leave the stages empty for now.
Jenkins
Hint
The stages section holds all the steps your pipeline will run. Each stage groups related steps.
3
Add steps to stages
Inside the Build stage, add a steps block with a echo step printing Building the project.... Inside the Test stage, add a steps block with a echo step printing Running tests....
Jenkins
Hint
The steps block inside each stage tells Jenkins what to do. echo prints messages to the console.
4
Run and see the output
Run the pipeline script in Jenkins and observe the console output. The output should show Building the project... and Running tests... messages.
Jenkins
Hint
Look at the Jenkins console log after running the pipeline. You should see the messages from both stages.
Practice
(1/5)
1. Why is Pipeline as Code important in Jenkins?
easy
A. It stores build and deploy steps in files for easy sharing and tracking.
B. It allows Jenkins to run without any configuration files.
C. It automatically fixes errors in your code.
D. It replaces the need for any manual testing.
Solution
Step 1: Understand what Pipeline as Code means
Pipeline as Code means writing your build and deployment steps in files, usually stored in your project repository.
Step 2: Identify the benefits of storing steps in files
This makes the process easy to share with others, track changes over time, and update as needed without manual errors.
Final Answer:
It stores build and deploy steps in files for easy sharing and tracking. -> Option A
Quick Check:
Pipeline as Code = storing steps in files [OK]
Hint: Remember: Pipeline as Code means steps saved in files [OK]
Common Mistakes:
Thinking Pipeline as Code removes all manual work
Believing Jenkins runs without any config files
Assuming it automatically fixes code errors
2. Which of the following is the correct way to start a Jenkins pipeline script in a Jenkinsfile?