Introduction
In modern software development, speed and reliability are everything. Teams are expected to deliver features rapidly while maintaining the stability of production applications. This is where CI/CD pipelines come into play — automating the entire software delivery lifecycle from code integration to deployment.
And when it comes to CI/CD tools, Jenkins stands tall as one of the most powerful and widely used automation servers.
This guide is a complete Jenkins CI/CD pipeline tutorial for beginners, using the Jenkins Pipeline (Jenkinsfile) approach. You will learn:
- What Jenkins and Jenkins Pipelines are
- How CI/CD works in Jenkins
- How to set up a CI/CD pipeline step-by-step
- How to write a Jenkinsfile
- Best practices for secure, efficient pipelines
- Common mistakes and how to avoid them
By the end of this guide, you’ll be ready to build your first CI/CD pipeline using Jenkins like a pro.
What Is Jenkins?
Jenkins is an open-source automation server that helps teams automate software builds, testing, integration, and deployment.
Why Jenkins Is Popular
- Free and open-source
- Highly extensible (1,800+ plugins)
- Supports all major languages
- Integrates with Git, Docker, Kubernetes, AWS, Azure, GCP
- Enables both Freestyle and Pipeline-based CI/CD
But this guide focuses specifically on Jenkins Pipelines, the modern and recommended method.
What Is a Jenkins CI/CD Pipeline?
A Jenkins CI/CD pipeline is a series of automated steps (stages) that execute whenever code is pushed, such as:
- Pull code from Git
- Build the application
- Run tests
- Package artifacts
- Deploy to servers
- Notify teams
Pipelines are defined using Pipeline as Code, written in a file called a Jenkinsfile.
This provides:
- Version-controlled pipelines
- Reproducible build automation
- Better collaboration
- Easier debugging
Types of Jenkins Pipelines
There are two main Jenkins Pipeline syntaxes:
1. Declarative Pipeline (Recommended for Beginners)
Simpler, structured, easier to read.
2. Scripted Pipeline
More flexible, written in Groovy, used for advanced automation.
In this tutorial, we’ll use Declarative Pipeline, the standard for modern CI/CD.
How Jenkins CI/CD Pipeline Works (Step-by-Step Overview)
A typical Jenkins CI/CD pipeline consists of:
1. Source Stage
Pull code from: - GitHub
- GitLab
- Bitbucket
2. Build Stage
Compile code, install dependencies, run build commands.
3. Test Stage
Run unit tests, integration tests, static analysis.
4. Package Stage
Build artifacts (e.g., JAR, WAR, Docker image).
5. Deploy Stage
Deploy to: - Test server
- Production
- Kubernetes cluster
- Docker environment
6. Notify Stage
Send success/failure notifications to Slack, email, etc.
Prerequisites to Set Up Jenkins CI/CD Pipeline
To follow this tutorial, you need:
- Jenkins installed (LTS recommended)
- Java installed
- Git installed
- A GitHub repository
- Jenkins Pipeline plugin (usually preinstalled)
- Optional: Docker installed (for Docker-based pipelines)
How to Set Up a CI/CD Pipeline in Jenkins (Beginner-Friendly Guide)
Step 1: Install Jenkins on Your Machine or Server
You can install Jenkins on: - Ubuntu
- Windows
- macOS
- Docker
Example installation for Ubuntu:
sudo apt update
sudo apt install openjdk-11-jdk -y
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt update
sudo apt install jenkins -yStart Jenkins:
sudo systemctl start jenkinsStep 2: Install Necessary Jenkins Plugins
Navigate to:
Manage Jenkins → Plugins → Available
Install these plugins:
- Git Plugin
- Pipeline Plugin
- GitHub Integration Plugin
- Blue Ocean (optional for UI visualization)
- Docker Pipeline Plugin
Step 3: Create a New Pipeline Job
- Go to Dashboard
- Click New Item
- Select Pipeline
- Enter a job name
- Click OK
Your pipeline job is now created.
Step 4: Connect Jenkins to Your Git Repository
Under:
Pipeline → Definition → Pipeline script from SCM
Configure:
- Repository URL
- Credentials
- Branch name
- Jenkinsfile path
This will pull the Jenkinsfile from your Git repo.
Writing Your First Jenkins Pipeline (Jenkinsfile)
Below is a complete Declarative Jenkins Pipeline example:
pipeline {
agent any
stages {
stage('Clone Repository') {
steps {
git branch: 'main', url: 'https://github.com/your/repository.git'
}
}
stage('Build') {
steps {
sh 'echo "Building the application..."'
sh './build.sh'
}
}
stage('Run Tests') {
steps {
sh 'echo "Running tests..."'
sh './run-tests.sh'
}
}
stage('Package') {
steps {
sh 'echo "Packaging application..."'
sh 'zip -r app.zip .'
}
}
stage('Deploy') {
steps {
sh 'echo "Deploying application..."'
sh './deploy.sh'
}
}
}
post {
success {
echo 'Pipeline completed successfully!'
}
failure {
echo 'Pipeline failed!'
}
}
}Understanding the Jenkinsfile (Explained for Beginners)
agent any
Uses any available Jenkins node/agent.
Clone Stage
Downloads the code from your GitHub/GitLab repository.
Build Stage
Runs build commands (install dependencies, compile code, etc.)
Test Stage
Executes unit tests, integration tests, or linting.
Package Stage
Creates a deployable artifact.
Deploy Stage
Deploys the application to your target environment.
Adding Docker Build & Push to Jenkins Pipeline
If you are using Docker, here is how to build and push images:
stage('Build Docker Image') {
steps {
script {
dockerImage = docker.build("myapp:${env.BUILD_NUMBER}")
}
}
}
stage('Push Docker Image') {
steps {
script {
docker.withRegistry('https://registry.hub.docker.com', 'docker-credentials') {
dockerImage.push()
}
}
}
}Best Practices for Jenkins CI/CD Pipelines
1. Use Declarative Pipelines
They are easier to maintain and read.
2. Always Use a Jenkinsfile
This ensures version control and easier debugging.
3. Add Mandatory Automated Tests
No CI pipeline is complete without proper testing.
4. Use Environment Variables
Avoid hardcoding sensitive values.
5. Secure Credentials
Store secrets using Jenkins Credentials Manager.
6. Use Shared Libraries
Encourage reusability across multiple pipelines.
7. Run Parallel Stages
Speed up test execution and builds.
Common Mistakes to Avoid
❌ Using Freestyle Jobs for CI/CD
Freestyle jobs are outdated and lack flexibility.
✔ Always use Jenkins Pipelines (Declarative or Scripted).
❌ Hardcoding Credentials
Never store passwords or tokens in your Jenkinsfile.
✔ Use Jenkins Credentials Manager instead.
❌ Creating Long, Unmanageable Pipelines
Long pipelines become difficult to maintain.
✔ Break pipelines into reusable Shared Libraries.
❌ No Notification System
Teams should know when builds pass or fail.
✔ Add Slack or email notifications.
❌ Not Cleaning Workspace
Leftover files can break deployments.
✔ Use cleanWs() to clean the workspace.
Real-World Use Cases of Jenkins CI/CD Pipelines
1. Automated Application Deployment
Automatically build, test, and deploy code on every commit.
2. Docker Build & Deployment
Build Docker images, push to registry, and deploy containers automatically.
3. Kubernetes Deployments
Use Jenkins to apply Kubernetes manifests or trigger Helm charts.
4. Microservices Orchestration
Manage CI/CD across multiple microservices.
5. Infrastructure Automation
Combine Jenkins with Terraform or Ansible for provisioning.
Short Summary
Jenkins CI/CD pipelines allow teams to automate their build, test, delivery, and deployment processes using code-defined workflows.
By utilizing Jenkins Pipeline (Jenkinsfile), cloud teams achieve consistency, faster delivery, improved reliability, and better collaboration.
Conclusion
Setting up a CI/CD pipeline in Jenkins is one of the most important skills for DevOps engineers and software teams.
With its Pipeline-as-Code approach, Jenkins delivers automation, flexibility, and reliability across all stages of the development lifecycle.
By following this guide, you now know:
- How Jenkins pipelines work
- How to write a Jenkinsfile
- How to integrate builds, tests, Docker, and deployment
- How to apply industry best practices
- How to avoid common mistakes
With continued practice, you will quickly master Jenkins CI/CD pipelines and apply them confidently in real-world environments.
FAQs
1. What is a Jenkins CI/CD pipeline?
A Jenkins CI/CD pipeline is an automated workflow that builds, tests, and deploys applications using a Jenkinsfile.
2. Is Jenkins good for beginners?
Yes. Jenkins is one of the easiest CI/CD tools for beginners.
3. What language does Jenkinsfile use?
Jenkinsfiles use a Groovy-based syntax.
4. Does Jenkins support Docker and Kubernetes?
Absolutely. Jenkins integrates seamlessly with Docker, Kubernetes, AWS, Azure, and more.
5. Can Jenkins deploy to cloud platforms?
Yes—Jenkins can deploy to AWS, Azure, Google Cloud, DigitalOcean, and on-prem servers.
References
- https://en.wikipedia.org/wiki/Jenkins_(software)
- https://en.wikipedia.org/wiki/Continuous_integration
- https://en.wikipedia.org/wiki/DevOps
- https://en.wikipedia.org/wiki/Continuous_delivery
- https://en.wikipedia.org/wiki/Software_testing
Comments
Post a Comment