AB
A comprehensive guide to Jenkins, covering its fundamentals, installation, configuration, architecture, and best practices for implementing effective CI/CD pipelines.
Jenkins is an open-source automation server that facilitates the automation of software development processes like building, testing, and deploying applications. It plays a crucial role in Continuous Integration (CI) and Continuous Delivery (CD) pipelines, making it indispensable in modern DevOps practices.
Jenkins operates through a series of automated steps that together form a comprehensive CI/CD pipeline:
Before diving deeper into Jenkins, it’s important to understand the evolution of software development methodologies and how they led to the need for CI/CD tools like Jenkins.
The Waterfall Model is a linear and sequential approach to software development.
The Agile model emerged to address the limitations of the Waterfall approach by embracing flexibility and collaboration.
Despite its advantages, Agile doesn’t fully address the gap between development and operations teams, particularly in production environments. This gap often leads to deployment delays and production issues.
DevOps emerged as a natural extension of Agile, specifically addressing the collaboration between development and operations teams.
Important Note: DevOps isn’t just about tools and practices; it’s fundamentally a cultural shift that encourages collaboration and breaking down silos between development and operations teams.
CI/CD is the backbone of modern software development and deployment processes. Let’s clarify these terms:
Continuous Integration (CI): A development practice where developers integrate code into a shared repository frequently, preferably several times a day. Each integration is verified by an automated build and tests.
Continuous Delivery (CD): A software development practice where code changes are automatically prepared for a release to production. The software can be reliably released at any time.
Continuous Deployment (CD): An extension of continuous delivery where every change that passes all stages of the production pipeline is automatically released to customers without human intervention.
The key difference between Continuous Delivery and Continuous Deployment is that Continuous Delivery requires manual approval for production deployment, while Continuous Deployment is fully automated.
Jenkins can be installed in multiple ways depending on your requirements and infrastructure. Here are the most common methods:
Setting up Jenkins using Docker provides flexibility and isolation.
Pull the Jenkins Docker image:
docker pull jenkins/jenkins:lts
Run Jenkins in a container:
docker run -d -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts
Retrieve the initial admin password:
docker exec <container_id> cat /var/jenkins_home/secrets/initialAdminPassword
For cloud-based deployments, EC2 is a popular choice.
Launch an EC2 instance with sufficient resources (recommended: t2.medium or larger).
Install Java (Jenkins requires Java to run):
sudo yum install java-17-openjdk -y
Add the Jenkins repository and install:
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
sudo yum install jenkins -y
Start Jenkins:
sudo systemctl start jenkins
sudo systemctl enable jenkins
Configure security groups to allow traffic on port 8080.
Access Jenkins at http://<your-ec2-ip>:8080
.
If you need to change the default port (8080):
Edit the Jenkins configuration file:
sudo vi /etc/sysconfig/jenkins
Or for systems using systemd:
sudo vi /lib/systemd/system/jenkins.service
Update the port:
JENKINS_PORT="8090"
Or in systemd:
Environment="JENKINS_PORT=8090"
Reload and restart Jenkins:
sudo systemctl daemon-reload
sudo systemctl restart jenkins
Update firewall rules if necessary:
sudo firewall-cmd --permanent --add-port=8090/tcp
sudo firewall-cmd --reload
Jenkins CLI allows interaction with Jenkins server directly from the command line, enabling automation of administrative tasks.
Download the jenkins-cli.jar
file:
wget http://your-jenkins-server:8080/jnlpJars/jenkins-cli.jar
Run commands using the JAR file:
java -jar jenkins-cli.jar -s http://your-jenkins-server:8080/ -auth username:apitoken command
List all jobs:
java -jar jenkins-cli.jar -s http://your-jenkins-server:8080/ -auth username:apitoken list-jobs
Build a job:
java -jar jenkins-cli.jar -s http://your-jenkins-server:8080/ -auth username:apitoken build job-name -s -v
Install a plugin:
java -jar jenkins-cli.jar -s http://your-jenkins-server:8080/ -auth username:apitoken install-plugin plugin-name
Plugins are at the heart of Jenkins’ extensibility, adding features and integrations with other tools.
A Jenkins plugin is an extension that adds functionality to Jenkins. Plugins enable integration with version control systems, build tools, cloud providers, and much more.
Pipeline steps are the individual actions performed during pipeline execution.
git: Checkout code from a Git repository.
git 'https://github.com/your-repo.git'
sh: Execute shell commands.
sh 'npm install'
docker: Build or run Docker containers.
docker.build('my-image:latest')
archiveArtifacts: Save build artifacts for later use.
archiveArtifacts artifacts: 'build/*.jar', allowEmptyArchive: true
junit: Process JUnit test results.
junit 'test-results/*.xml'
The Manage Jenkins section is the central hub for Jenkins administration, containing crucial configuration options.
System Configuration
Global Tool Configuration
Feature | System Configuration | Global Tool Configuration |
---|---|---|
Scope | Applies globally to Jenkins | Affects tools used in specific jobs |
Purpose | Controls Jenkins’ overall behavior | Manages build tools and versions |
Examples | Email setup, Jenkins URL, security | JDK versions, Maven installations |
SonarQube integration requires configurations in both sections:
System Configuration:
Global Tool Configuration:
This dual configuration allows Jenkins to communicate with SonarQube (system config) and use the appropriate scanner tool during builds (tool config).
LDAP (Lightweight Directory Access Protocol) is a protocol for accessing and maintaining directory information services over an IP network, commonly used for centralized authentication.
Jenkins can authenticate users against an LDAP directory, eliminating the need for separate user accounts within Jenkins and centralizing user management.
The Role-Based Authorization Strategy plugin provides fine-grained access control in Jenkins.
Install the Plugin:
Enable the Plugin:
Define Roles:
Assign Roles to Users:
Save the Configuration.
Jenkins uses a master-agent (formerly master-slave) architecture to distribute build workloads across multiple machines.
Distributed builds involve running jobs across multiple nodes (master and agents) to improve performance and scalability.
Jenkins uses the Java Network Launch Protocol (JNLP) for secure communication between the master and agent nodes.
JNLP (Java Network Launch Protocol) is a protocol that allows Java applications to be launched via a web browser. In Jenkins, it’s used to establish a secure connection between the master and agent nodes.
/home/jenkins
or D:\jenkins
).linux
, windows
, docker
).Using Docker containers as Jenkins agents provides isolation and consistency.
Pull the official Jenkins agent image:
docker pull jenkins/agent:latest
Run the agent container:
docker run -d --name jenkins-agent \
-v /var/run/docker.sock:/var/run/docker.sock \
jenkins/agent:latest \
-url http://jenkins-master:8080 \
-workDir=/home/jenkins/agent \
<secret-key> <agent-name>
Configure the Jenkins master to recognize the Docker agent.
Regular backups are essential for disaster recovery and migration.
To back up the entire Jenkins installation:
tar -czf jenkins-backup.tar.gz /var/jenkins_home
Or for a Docker installation:
docker cp jenkins-container:/var/jenkins_home ./jenkins-backup
There are several ways to trigger downstream jobs:
Build Triggers Method:
Pipeline Method:
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building..'
}
}
}
post {
success {
build job: 'downstream-job', wait: false
}
}
}
The Blue Ocean plugin provides modern visualization for pipelines.
Alternatively, use the Delivery Pipeline Plugin:
Post blocks execute actions based on the pipeline’s result:
pipeline {
agent any
stages {
stage('Test') {
steps {
echo 'Running tests...'
}
}
}
post {
always { echo "This will always run" }
success { echo "This runs on success" }
failure { echo "This runs on failure" }
unstable { echo "This runs if unstable" }
changed { echo "This runs if the status changed from the previous run" }
}
}
Parallel execution speeds up pipelines by running stages concurrently:
pipeline {
agent none
stages {
stage('Parallel Stages') {
parallel {
stage('Windows Tests') {
agent { label "windows" }
steps {
echo 'Running tests on Windows'
}
}
stage('Linux Tests') {
agent { label "linux" }
steps {
echo 'Running tests on Linux'
}
}
}
}
}
}
A Jenkinsfile is a text file containing the pipeline definition, stored with the source code:
Create a Jenkinsfile in your repository root:
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building..'
sh 'npm install'
}
}
stage('Test') {
steps {
echo 'Testing..'
sh 'npm test'
}
}
stage('Deploy') {
steps {
echo 'Deploying....'
}
}
}
}
Configure the Pipeline Job in Jenkins:
Jenkinsfile
.Jenkins remains one of the most powerful and flexible CI/CD automation servers available. Its extensive plugin ecosystem, distributed build capabilities, and pipeline-as-code support make it suitable for projects of any size, from small teams to large enterprises.
By implementing Jenkins in your development workflow, you can achieve faster release cycles, improved code quality, and greater collaboration between development and operations teams - the core principles of DevOps.