Skip to main content

How to Set Up a CI/CD Pipeline in Jenkins

 

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.

How to Set Up a CI/CD Pipeline in Jenkins



What Is Jenkins?

Jenkins is an open-source automation server that helps teams automate software builds, testing, integration, and deployment.

  • 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?

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:

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 -y

Start Jenkins:

sudo systemctl start jenkins

Step 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

  1. Go to Dashboard
  2. Click New Item
  3. Select Pipeline
  4. Enter a job name
  5. 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

Popular posts from this blog

SEO Course in Jaipur – Transform Your Career with Artifact Geeks

 Are you looking for an SEO course in Jaipur that combines industry insights with hands-on training? Artifact Geeks offers a top-rated, comprehensive SEO course tailored for beginners, marketers, and professionals to enhance their digital marketing skills. With over 12 years of experience in the digital marketing industry, Artifact Geeks has empowered countless students to grow their knowledge, build effective strategies, and advance their careers. Why Choose an SEO Course in Jaipur? Jaipur’s dynamic business environment has created a high demand for skilled digital marketers, especially those with SEO expertise. From startups to established businesses, companies in Jaipur understand the importance of a strong online presence. This growing demand makes it the perfect time to learn SEO, and Artifact Geeks offers a practical and transformative approach to mastering SEO skills right in the heart of Jaipur. What You’ll Learn in the SEO Course Artifact Geeks’ SEO course in Jaipur cover...

MERN Stack Explained

  Introduction If you’ve ever searched for the most in-demand web development technologies, you’ve definitely come across the  MERN stack . It’s one of the fastest-growing and most widely used tech stacks in the world—powering everything from small startup apps to enterprise-level systems. But what makes MERN so popular? Why do companies prefer MERN developers? And most importantly—what  MERN stack basics  do beginners need to learn to get started? In this complete guide, we’ll break down the MERN stack in the simplest, most practical way. You’ll learn: What the MERN stack is and how each component works Why MERN is ideal for full stack development Real-world use cases, examples, and workflows Essential MERN stack skills for beginners Step-by-step explanations to build a MERN project How MERN compares to other tech stacks By the end, you’ll clearly understand MERN from end to end—and be ready to start your journey as a MERN stack developer. What Is the MERN Stack? Th...

Building File Upload System with Node.js

  Introduction Every modern application allows users to upload something. Profile pictures Documents Certificates Videos Assignments Product images From social media platforms to enterprise SaaS products file uploading is a core backend feature Yet many developers underestimate how complex it actually is A secure and scalable nodejs file upload system must handle Large files without crashing the server File validation and security checks Storage management Performance optimization Cloud integration Without proper architecture file uploads can become the biggest security and performance risk in your application In this complete guide you will learn how to build a production ready file upload system with Node.js step by step What Is Node.js File Upload A Node.js file upload system allows users to transfer files from their browser to a server using HTTP requests Basic workflow User to Browser to Server to Storage to Response When users upload files 1 Browser sends multipart form data ...