DEV Community

Ismail Shaikh
Ismail Shaikh

Posted on

End-to-End CI/CD Pipeline using Jenkins

πŸ“Œ Scenario (Real-World)

We will deploy an application using this flow:

Developer β†’ Git β†’ Jenkins β†’ Build β†’ Test β†’ Package β†’ Deploy

Below is Text Based Diagram.

Developer
|
v
Git Repository (GitHub / Bitbucket)
|
v
Jenkins (Pipeline)
|
β”œβ”€β”€ Build
β”œβ”€β”€ Test
β”œβ”€β”€ Package
└── Deploy
|
v
Server / Cloud (EC2, VM, etc.)

  1. Jenkins Server

Jenkins installed on EC2 / VM / Local

Java installed

Jenkins running on port 8080

2 Required Jenkins Plugins

Install from Manage Jenkins β†’ Plugins:

βœ” Git
βœ” Pipeline
βœ” SSH Agent
βœ” Credentials Binding

Step 2: Application Source Code

my-app/
β”œβ”€β”€ app.py
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ Dockerfile
└── Jenkinsfile

Step 3: Jenkins Credentials Setup
Add credentials in Jenkins

Manage Jenkins β†’ Credentials

Git credentials

Server SSH key

Docker registry credentials (if needed)

Jenkins NEVER stores passwords in plain text.

Step 4: Create Jenkins Pipeline Job

Jenkins Dashboard β†’ New Item

Select Pipeline

Name: my-app-pipeline

Pipeline Definition:

Pipeline script from SCM

SCM: Git

Repo URL

Branch: main

Script path: Jenkinsfile

Step 5: Jenkinsfile (Heart of Pipeline)

Complete End-to-End Jenkinsfile

pipeline {
agent any

environment {
    APP_NAME = "my-app"
    DEPLOY_SERVER = "ubuntu@10.0.0.10"
    DEPLOY_PATH = "/var/www/my-app"
}

stages {

    stage('Checkout Code') {
        steps {
            git branch: 'main',
                url: 'https://github.com/your-org/my-app.git'
        }
    }

    stage('Build') {
        steps {
            echo "Building application..."
            sh 'pip install -r requirements.txt'
        }
    }

    stage('Test') {
        steps {
            echo "Running tests..."
            sh 'pytest || echo "Tests skipped"'
        }
    }

    stage('Package') {
        steps {
            echo "Packaging application..."
            sh 'tar -czf app.tar.gz *'
        }
    }

    stage('Deploy') {
        steps {
            echo "Deploying application..."
            sshagent(['server-ssh-key']) {
                sh """
                scp app.tar.gz ${DEPLOY_SERVER}:/tmp/
                ssh ${DEPLOY_SERVER} '
                    mkdir -p ${DEPLOY_PATH} &&
                    tar -xzf /tmp/app.tar.gz -C ${DEPLOY_PATH} &&
                    systemctl restart my-app
                '
                """
            }
        }
    }
}

post {
    success {
        echo "βœ… Deployment Successful"
    }
    failure {
        echo "❌ Deployment Failed"
    }
}
Enter fullscreen mode Exit fullscreen mode

}

Step 6: Deployment Server Setup

On target server (EC2 / VM):

sudo mkdir -p /var/www/my-app
sudo useradd my-app
sudo systemctl enable my-app

Example systemd service:

[Unit]
Description=My App Service

[Service]
ExecStart=/usr/bin/python3 /var/www/my-app/app.py
Restart=always

[Install]
WantedBy=multi-user.target

Step 7: Triggering the Pipeline
Option 1: Manual Trigger

Click Build Now

Option 2: Auto Trigger (Recommended)

Enable Git Webhook

Git Push β†’ Jenkins Auto Build β†’ Auto Deploy

Step 8: What Happens End-to-End (Interview Explanation)

πŸ‘‰ When developer pushes code:

Git notifies Jenkins

Jenkins pulls latest code

Jenkins builds application

Jenkins runs tests

Jenkins packages the app

Jenkins deploys to server

Application restarts

New version goes live πŸš€

Step 9: Common Interview Questions
❓ Why Jenkinsfile?

βœ” Version controlled
βœ” Reproducible
βœ” Automated

❓ What if deployment fails?

βœ” Pipeline stops
βœ” Old version remains running

❓ Is Jenkins modifying infra?

❌ No, pipeline is read-only unless explicitly allowed

Step 10: Variations (Real Projects)

You can extend this pipeline with:

Docker build & push

Kubernetes deployment

AWS CodeDeploy

Blue-Green deployment

Rollback stage

Final Summary (One-Line)

Jenkins CI/CD pipeline automatically builds, tests, packages, and deploys application whenever code changes, ensuring fast, reliable, and repeatable deployments.

Top comments (0)