CHAPTER 05
Jenkins Pipelines Fundamentals
Updated: May 15, 2026
25 min read
# CHAPTER 5
Jenkins Pipelines Fundamentals
1. Introduction
While Freestyle jobs (Chapter 4) were the standard for years, they have a massive flaw: they are configured by clicking buttons in a web browser. If your Jenkins server crashes, you lose all your job configurations. Furthermore, you cannot easily track changes to a web form in version control. Enter the Jenkins Pipeline. A Pipeline represents a paradigm shift known as "Pipeline as Code." Instead of clicking buttons, you write your entire deployment process in a text file called aJenkinsfile and store it in your Git repository alongside your application code. In this chapter, we will introduce the philosophy of Pipeline as Code and contrast Declarative vs. Scripted pipelines.
2. Learning Objectives
By the end of this chapter, you will be able to:- Define "Pipeline as Code" and its benefits over Freestyle jobs.
-
Understand the purpose of a
Jenkinsfile.
- Differentiate between Declarative and Scripted Pipeline syntax.
- Understand the core building blocks: Pipeline, Agent, Stages, and Steps.
- Create your first basic Pipeline job in Jenkins.
3. Beginner-Friendly Explanation
Imagine directing a stage play.- Freestyle Job: The director verbally tells each actor where to stand right before the curtain opens. If the director gets sick, the actors have no idea what to do.
-
Pipeline as Code: The director writes a comprehensive Script (The
Jenkinsfile). The script is handed to the actors (Jenkins). It says:
- *Act 1 (Stage 1):* Build the set.
- *Act 2 (Stage 2):* Test the lighting.
- *Act 3 (Stage 3):* Perform the play.
4. The Jenkinsfile
A Jenkinsfile is a plain text file that contains the definition of a Jenkins Pipeline.
It is checked into Source Control (like GitHub) right next to your index.php or app.js.
Why this is revolutionary:
-
1.
Version Control: If someone breaks the deployment script, you can look at GitHub, see exactly who changed the
Jenkinsfile, and instantly revert it to the previous working version.
-
2.
Portability: If your Jenkins server explodes, you just spin up a new empty Jenkins server, point it at your GitHub repo, and Jenkins automatically reads the
Jenkinsfileand rebuilds your entire CI/CD process instantly.
5. Declarative vs. Scripted Syntax
Jenkins pipelines are written in a language called Groovy. There are two ways to write them:- 1. Scripted Pipeline (The Old Way): Very flexible, but uses complex programming logic (if/else loops, try/catch blocks). It is harder for beginners to read.
- 2. Declarative Pipeline (The Modern Standard): Introduced to make pipelines easier to read and write. It uses a strict, structured block format. It is essentially a configuration file. We will focus entirely on Declarative Pipelines as they are the industry best practice.
6. The Core Anatomy of a Declarative Pipeline
Every Declarative Pipeline has four mandatory parts:-
1.
pipeline {}: The wrapper block containing the entire script.
-
2.
agent: Tells Jenkins *where* to run the code. (agent anymeans run on the Jenkins server itself. Later, we'll tell it to run inside a specific Docker container).
-
3.
stages {}: A wrapper that contains all the different phases of your process.
-
4.
steps {}: The actual commands you want to run (likeechoorshfor shell commands) inside a specific stage.
7. Mini Project: Create First Jenkins Pipeline
Let's create a "Hello World" Pipeline.Step-by-Step Walkthrough:
- 1. On the Jenkins dashboard, click New Item.
-
2.
Name it
My-First-Pipeline, select Pipeline (not Freestyle), and click OK.
- 3. Scroll down to the Pipeline section at the bottom.
- 4. Ensure the Definition is set to "Pipeline script".
- 5. In the large text box, type the following Declarative Pipeline code exactly:
groovy
- 6. Click Save.
- 7. Click Build Now.
- 8. Look at the "Stage View" on the project page. You will see beautiful green boxes indicating that the Build, Test, and Deploy stages all passed sequentially!
8. Real-World Scenarios
A development team managed 50 different microservices. Using Freestyle jobs, they had to manually click through the UI 50 times to set up the configurations. It took days. They switched to "Pipeline as Code." They wrote one standardJenkinsfile, copied it into all 50 GitHub repositories, and configured Jenkins to automatically scan the repositories. Jenkins found the 50 Jenkinsfiles and instantly auto-generated 50 perfect CI/CD pipelines without a single manual click in the UI.
9. Best Practices
-
Do NOT write the script in the UI long-term: In the mini-project above, we typed the code into the Jenkins UI text box for learning purposes. In the real world, you must change the definition from "Pipeline script" to "Pipeline script from SCM" (Source Control Management), telling Jenkins to pull the
Jenkinsfiledirectly from GitHub.
10. Troubleshooting Tips
-
Missing Brackets: Declarative pipelines are highly structured. If you miss a single closing curly brace
}, the entire pipeline will crash with a Groovy parsing error before it even starts. Pay close attention to indentation and bracket matching.
11. Exercises
- 1. List the three major benefits of using "Pipeline as Code" over configuring jobs manually in the Jenkins web UI.
- 2. Identify the four mandatory blocks required in a valid Declarative Pipeline.
12. FAQs
Q: What language is the Jenkinsfile written in? A: It is written in a Groovy-based Domain Specific Language (DSL). However, you don't need to be a Groovy programmer. Declarative syntax is very close to JSON or YAML—it's mostly just defining configuration blocks.13. Interview Questions
- Q: Contrast Declarative Pipelines with Scripted Pipelines in Jenkins. Why did the community shift heavily toward the Declarative model?
- Q: Explain the concept of "Infrastructure as Code" (IaC) and how "Pipeline as Code" aligns with that operational philosophy.
14. Summary
In Chapter 5, we leveled up our DevOps skills by embracing "Pipeline as Code." We abandoned fragile, UI-based configurations in favor of the resilient, version-controlledJenkinsfile. We explored the rigid but readable structure of Declarative Pipelines, understanding how agent, stages, and steps map out the automation journey. Finally, we executed a multi-stage pipeline, visualizing the workflow in the Jenkins dashboard.