Day 9 โ Jenkins Pipeline Basics (Declarative) | #30DaysOfDevOps

Series: 30 Days DevOps Interview Preparation
๐ Introduction
Jenkins has evolved far beyond simple Freestyle jobs. Modern DevOps teams rely on Jenkins Pipelines because they provide automation as code, integrate seamlessly with version control, and allow for complex workflows.
Today, weโll dive into Declarative Pipelines โ the beginner-friendly, structured syntax that makes CI/CD both readable and maintainable.
1. What is a Jenkins Pipeline?
A Jenkins Pipeline is a set of plugins that lets you define your build, test, and deployment process in a Jenkinsfile
.
Key benefits:
CI/CD as code โ version-controlled in Git
Modular stages (Build โ Test โ Deploy)
Easy rollback & history tracking
Integrates with multiple tools (Docker, Kubernetes, AWS, GCP, etc.)
2. Declarative vs Scripted Pipelines
Feature | Declarative | Scripted |
Syntax | Structured pipeline {} block | Pure Groovy |
Readability | High | Medium (Groovy knowledge needed) |
Error handling | Built-in | Manual |
Flexibility | Moderate | High |
Best for beginners? | โ Yes | โ No |
3. Basic Structure of a Declarative Pipeline
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building application...'
}
}
stage('Test') {
steps {
echo 'Running tests...'
}
}
stage('Deploy') {
steps {
echo 'Deploying application...'
}
}
}
post {
success {
echo 'Pipeline completed successfully!'
}
failure {
echo 'Pipeline failed!'
}
}
}
4. Practical: Setting Up a Declarative Pipeline on AWS EC2
Step 1: Launch Jenkins
Start an AWS EC2 Ubuntu instance
Install Jenkins and Java
Open Jenkins UI on
http://<EC2-IP>:8080
Step 2: Create a New Pipeline Job
New Item โ Pipeline โ OK
In the Pipeline script section, paste your Jenkinsfile code.
Step 3: Save & Build
- Click Build Now and watch the console output.
5. Adding Real Commands
Example for a Java Maven project:
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean install'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
}
stage('Deploy') {
steps {
sh '''
echo "Deploying to server..."
scp -i /path/key.pem target/myapp.jar ec2-user@<EC2-IP>:/home/ec2-user/
'''
}
}
}
}
6. Common Declarative Pipeline Questions & Answers
Q1: What is the difference between Declarative and Scripted Pipelines?
Declarative has a strict, readable syntax inside a pipeline {}
block, while Scripted is written entirely in Groovy and is more flexible but harder to maintain.
Q2: Why store Jenkinsfile in Git?
Version control allows tracking changes, easy rollback, collaboration, and integration with Git hooks for automation.
Q3: What is agent
in Jenkins pipelines?
The agent
directive tells Jenkins where to run the pipeline (any
, specific label, or a Docker container).
Q4: How do you handle errors in Declarative pipelines?
Use the post
block with failure {}
or always {}
to define actions after a build failure.
Q5: How do you run a pipeline on a specific node?
agent { label 'my-node' }
Q6: How do you parameterize a pipeline?
parameters {
string(name: 'VERSION', defaultValue: '1.0', description: 'App version')
}
7. Best Practices
Always use
pipeline {}
structure for clarityKeep build, test, and deploy steps in separate stages
Store
Jenkinsfile
in the repo rootUse parameters for flexibility
Use
post
block for notifications and cleanup
โ Day 9 takeaway: Declarative Pipelines make CI/CD more maintainable, readable, and collaborative โ a must-have skill for any DevOps Engineer.
#DevOps #Jenkins #CICD #Pipelines #InterviewPreparation #30DaysOfDevOps
Subscribe to my newsletter
Read articles from Tathagat Gaikwad directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
