Jenkins Course
What is a Jenkins Pipeline
Section I gave you the foundation. Section II is where Jenkins gets real. Pipelines are how professionals automate software delivery — and the Jenkinsfile is the file that makes it all possible.
This lesson covers
What a pipeline is → Why it exists → The Jenkinsfile → Stages and steps → Your first working pipeline → Reading the pipeline view in the Jenkins UI
In Section I, you built a Freestyle job by clicking through forms in Jenkins. That worked — but the configuration only existed inside Jenkins. If Jenkins went down, the job was gone. If a colleague wanted to see what the job did, they had to log into Jenkins and click around. There was no history of who changed what. No way to review a change before it went live.
A Jenkins Pipeline solves all of that. The entire build process is defined in a single file — the Jenkinsfile — that lives in your code repository alongside your application. It travels with your code. It gets reviewed in pull requests. It gets versioned in Git. And it runs the same way on any Jenkins server that picks it up.
The Analogy That Makes It Click
A Jenkinsfile is like a recipe card kept inside the dish itself. When someone else wants to make the same dish, they don't need to call you and ask what steps you followed. The recipe travels with the food. Any cook, in any kitchen, can reproduce it exactly. That's what pipeline-as-code means — the instructions travel with the code.
The Three Building Blocks of Every Pipeline
No matter how complex a Jenkins Pipeline gets, it is always built from the same three things:
🏗
Stage
A named phase of your pipeline — Checkout, Build, Test, Deploy. Stages show up as labelled blocks in the Jenkins UI. If a stage fails, Jenkins stops there and marks it red.
⚡
Step
A single action inside a stage — run a shell command, check out code, send a notification. Steps are the actual work. A stage is just a container that groups related steps together.
📬
Post
Actions that run after all stages complete — success notifications, failure alerts, cleanup. Post always runs, even if a stage failed, so your team always gets notified.
What a Pipeline Looks Like in the Jenkins UI
When a Pipeline job runs, Jenkins shows you a visual stage view — each stage as a box, colour-coded by result. This is the view you'll stare at while waiting for a deployment to finish. Here's what it looks like:
Each stage shows its name and how long it took. Green means it passed. Click any stage box to jump straight to its console output. This view is what separates Pipeline jobs from Freestyle jobs visually.
And here's what it looks like when a stage fails — Jenkins stops at the red stage and shows you exactly where to look:
Test failed → Deploy was skipped automatically. Jenkins never deploys broken code. The post block still runs to send a failure notification to the team.
Your First Jenkinsfile
The scenario:
You're a backend developer at a SaaS startup. Your team has been deploying the notification-service manually for months. You've been asked to write the first Jenkinsfile for this service — one that checks out the code, runs tests, and prints a success message. Simple, clean, and something the whole team can build on top of.
New terms introduced in this code:
- Jenkinsfile — a text file named exactly
Jenkinsfile(capital J, no extension) that lives in the root of your code repository. This is the file Jenkins reads to know what your pipeline should do. - Declarative Pipeline syntax — the modern, recommended way to write Jenkinsfiles. It has a strict, readable structure. You'll learn the older Scripted syntax in Lesson 12 so you understand the difference.
- pipeline { } — the outermost block. Everything in your Jenkinsfile lives inside this block.
- agent any — tells Jenkins to run this pipeline on any available agent. You can replace
anywith a specific label likeagent { label 'linux' }to target a specific machine. - stages { } — the container that holds all your stage blocks.
- stage('Name') { } — a named phase of your pipeline. The name appears in the Jenkins UI stage view.
- steps { } — the container inside a stage where you put the actual commands to run.
- sh — runs a shell command on the agent. This is the most common step — it's how you run tests, build commands, and deployment scripts.
- echo — prints a message to the console output. Useful for logging what your pipeline is doing.
- post { } — runs after all stages.
successruns only if everything passed.failureruns only if something failed.alwaysruns no matter what.
// This is a Declarative Pipeline — the modern standard for Jenkins
// Save this file as 'Jenkinsfile' in the root of your repository
pipeline {
// Run on any available agent (we'll get specific about agents in Lesson 14)
agent any
// 'stages' is the container that holds all your pipeline stages
stages {
// Stage 1: Pull the latest code from the repository
stage('Checkout') {
steps {
// 'checkout scm' tells Jenkins to pull the code from whatever
// source control repo triggered this build
checkout scm
echo 'Code checked out successfully'
}
}
// Stage 2: Run the test suite
stage('Test') {
steps {
// 'sh' runs a shell command on the agent
// './gradlew test' is the Gradle command to run all tests
sh './gradlew test'
}
}
// Stage 3: Confirm everything passed
stage('Complete') {
steps {
echo 'All stages passed — notification-service is healthy'
}
}
}
// 'post' runs after all stages, regardless of what happened
post {
// 'success' only runs if every stage above passed
success {
echo 'Pipeline finished successfully — ready to deploy'
}
// 'failure' only runs if a stage above failed
failure {
echo 'Pipeline failed — check the Test stage for details'
}
}
}
Where to practice: Create a free GitHub repository and add this file as Jenkinsfile in the root. Then in Jenkins, create a new Pipeline job → under Pipeline Definition choose Pipeline script from SCM → point it at your GitHub repo. Jenkins will find the Jenkinsfile automatically and run it. Full walkthrough at jenkins.io — Getting Started with Pipelines.
Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] node
Running on agent-linux-01 in /var/jenkins_home/workspace/notification-service
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Checkout)
[Pipeline] checkout
Cloning repository https://github.com/acmecorp/notification-service.git
> git checkout main
[Pipeline] echo
Code checked out successfully
[Pipeline] }
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] sh
+ ./gradlew test
BUILD SUCCESSFUL in 54s
28 tests completed, 0 failed
[Pipeline] }
[Pipeline] stage
[Pipeline] { (Complete) }
[Pipeline] echo
All stages passed — notification-service is healthy
[Pipeline] }
[Pipeline] stage (post)
[Pipeline] echo
Pipeline finished successfully — ready to deploy
[Pipeline] End of Pipeline
Finished: SUCCESS
What just happened?
[Pipeline] Start of Pipeline— Jenkins read the Jenkinsfile and started executing it from the top.Running on agent-linux-01— theagent anydirective was satisfied byagent-linux-01having a free executor. This is the machine that ran all three stages.[Pipeline] { (Checkout)— Jenkins entered the Checkout stage. Every stage entry and exit is logged with these curly brace markers so you can see exactly where time was spent.+ ./gradlew test— the+prefix means Jenkins is printing the command before running it. This comes from running the shell with the-xflag. It's your confirmation that the exact command you wrote is being executed.28 tests completed, 0 failed— this is Gradle's output, not Jenkins'. Jenkins captured it and printed it to the console. If even one test had failed, Gradle would have returned exit code 1, Jenkins would have caught it, and the Test stage would have turned red.- The post success block ran — because all stages passed, the
successpost condition fired. If the Test stage had failed, thefailurecondition would have run instead, and Deploy would have been skipped entirely. Finished: SUCCESS— Jenkins' final verdict. The stage view in the browser will show all three stage boxes in green.
Creating a Pipeline Job in the Jenkins UI
Here's exactly how you wire up your Jenkinsfile to a Jenkins Pipeline job — step by step in the UI:
Click New Item in the left sidebar
Type a job name like notification-service-pipeline. Select Pipeline from the list. Click OK.
Scroll down to the Pipeline section
Under Definition, change the dropdown from Pipeline script to Pipeline script from SCM. This tells Jenkins to read the Jenkinsfile from your repository instead of a text box.
Set SCM to Git
Paste in your repository URL. Add your credentials if it's a private repo. Set the branch to */main.
Leave Script Path as Jenkinsfile
The default value is Jenkinsfile. Jenkins will look for this file in the root of your repository. Only change this if your Jenkinsfile is in a subfolder.
Click Save, then Build Now
Jenkins pulls the Jenkinsfile from your repo and runs it. Watch the stage view update in real time.
Why Pipeline-as-Code Changes Everything
Version controlled
Every change to your pipeline is a Git commit. You can see who changed what, when, and why. You can roll back a bad pipeline change the same way you roll back bad application code.
Reviewed in pull requests
A colleague changing the deploy step gets the same code review as changing application logic. No more surprise pipeline changes that break production deploys.
Portable
The Jenkinsfile travels with the code. If you migrate to a new Jenkins server, the pipeline comes with the repository. No manual job recreation required.
Auditable
Compliance teams asking "what was deployed on March 14th and who approved the pipeline change?" — Git history has the answer. No digging through Jenkins UI config history.
Teacher's Note
The Jenkinsfile is the most important file in your repository that isn't application code. Treat it like application code — review it, test it, version it.
Practice Questions
1. What is the name of the file you create in your repository to define a Jenkins Pipeline?
2. Which block in a Declarative Pipeline runs actions after all stages complete, regardless of whether they passed or failed?
3. When creating a Pipeline job in Jenkins, which Definition option tells Jenkins to read the Jenkinsfile from your Git repository?
Quiz
1. What is a Pipeline stage in Jenkins?
2. What happens to the remaining stages when one stage fails in a Declarative Pipeline?
3. What is the main advantage of storing your pipeline in a Jenkinsfile rather than configuring it in the Jenkins UI?
Up Next · Lesson 12
Declarative vs Scripted Pipeline
Two ways to write a Jenkinsfile — side by side, with exactly when to use each one and why most teams pick Declarative.