Skip to content

πŸš€ JENKINS FULL END-TO-END TUTORIAL


🧱 1. What is Jenkins?

Jenkins is an open-source automation server used to:

  • Build and test your code continuously (CI)

  • Deploy applications automatically (CD)

  • Orchestrate DevOps pipelines (with Docker, Kubernetes, etc.)

It supports plugins for everything β€” GitHub, Docker, SonarQube, AWS, ArgoCD, etc.


🧰 2. System Requirements

Requirement Recommended
OS Ubuntu 20.04 / 22.04 (or Windows)
RAM 4 GB minimum
Java Java 11+ (JDK 17 preferred)
Browser Chrome / Firefox

βš™οΈ 3. Jenkins Installation (Ubuntu)

Step 1: Update packages

sudo apt update && sudo apt upgrade -y

Step 2: Install Java

sudo apt install openjdk-17-jdk -y

Step 3: Add Jenkins repository and key

curl -fsSL https://pkg.jenkins.io/debian/jenkins.io.key | sudo tee \
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null

echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null

Step 4: Install Jenkins

sudo apt update
sudo apt install jenkins -y

Step 5: Start Jenkins

sudo systemctl start jenkins
sudo systemctl enable jenkins

Step 6: Access Jenkins

Go to β†’
πŸ‘‰ http://localhost:8080

Get the initial admin password:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

🧩 4. Setup Wizard

  1. Paste the admin password.

  2. Choose β€œInstall suggested plugins”.

  3. Create your admin user.

  4. You’ll land on the Jenkins Dashboard.


🧱 5. Jenkins Architecture Overview

Developer β†’ Git Push β†’ Jenkins Pipeline β†’ Build β†’ Test β†’ Deploy

Key Components:

Component Description
Jenkins Master Orchestrates builds and manages agents
Agent/Node Executes the actual build steps
Pipeline Script that defines your CI/CD flow
Job Unit of work (can be Freestyle or Pipeline)
Workspace Directory on agent where build happens

🧩 6. Jenkins Plugins (Essential Ones)

Go to: Manage Jenkins β†’ Plugins β†’ Available
Search & Install:

βœ… Git
βœ… Docker
βœ… Pipeline
βœ… Blue Ocean (modern UI)
βœ… Kubernetes
βœ… Snyk Security (optional)


πŸ—οΈ 7. Example Project: CI/CD Pipeline for a Python Web App

Let’s say your repo structure is:

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

Step 1: Dockerfile

FROM python:3.11-slim
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
CMD ["python", "app.py"]

Step 2: Jenkins Pipeline (Jenkinsfile)

Create a file named Jenkinsfile in your repo:

pipeline {
    agent any

    environment {
        DOCKER_HUB_USER = credentials('dockerhub-credentials')
        IMAGE_NAME = "yuvaraj/my-app"
    }

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

        stage('Install Dependencies') {
            steps {
                sh 'pip install -r requirements.txt'
            }
        }

        stage('Run Tests') {
            steps {
                sh 'pytest || echo "Tests Skipped"'
            }
        }

        stage('Build Docker Image') {
            steps {
                script {
                    sh "docker build -t $IMAGE_NAME:${env.BUILD_NUMBER} ."
                }
            }
        }

        stage('Push to Docker Hub') {
            steps {
                script {
                    sh "echo $DOCKER_HUB_USER_PSW | docker login -u $DOCKER_HUB_USER_USR --password-stdin"
                    sh "docker push $IMAGE_NAME:${env.BUILD_NUMBER}"
                }
            }
        }

        stage('Deploy to Kubernetes') {
            steps {
                script {
                    sh 'kubectl apply -f k8s/deployment.yaml'
                }
            }
        }
    }

    post {
        always {
            echo "Pipeline finished: ${currentBuild.currentResult}"
        }
    }
}

Step 3: Create Jenkins Credentials

Go to:

Manage Jenkins β†’ Credentials β†’ Global β†’ Add Credentials

Add:

  • Docker Hub Username/Password

  • Kubernetes Kubeconfig (if you’re deploying to a cluster)


Step 4: Create Pipeline Job

  1. Go to Jenkins Dashboard

  2. Click β€œNew Item” β†’ Pipeline

  3. Name: my-app-pipeline

  4. Under Pipeline β†’ Choose Pipeline script from SCM

  5. Select Git, paste repo URL, branch = main

  6. Save & Run βœ…


Step 5: Watch Pipeline in Action

You’ll see stages like:

[1] Checkout Code
[2] Install Dependencies
[3] Run Tests
[4] Build Docker Image
[5] Push to Docker Hub
[6] Deploy to Kubernetes

If all succeed β†’ Jenkins deploys automatically πŸŽ‰


🧠 8. Jenkins Pipeline Types

Type Description
Declarative Pipeline Easier syntax (recommended for beginners)
Scripted Pipeline Fully Groovy-based, more flexible

Example:

pipeline {
  agent any
  stages {
    stage('Build') { steps { echo 'Building...' } }
  }
}

πŸ” 9. Integrations

Tool Integration
GitHub Webhook Auto-trigger Jenkins when new code is pushed
Slack Send build notifications
SonarQube Code quality analysis
Trivy / Snyk Container security scanning
AWS / GCP Deploy using Jenkins pipelines

πŸ“Š 10. Jenkins + GitHub Webhook Setup

1. Go to your GitHub repo β†’ Settings β†’ Webhooks β†’ Add webhook

  • Payload URL: http://<your-server-ip>:8080/github-webhook/

  • Content type: application/json

  • Trigger: β€œJust the push event”

Now every push triggers Jenkins automatically πŸš€


🧩 11. Jenkins + Docker Integration

Install Docker plugin and ensure Jenkins user can run Docker:

sudo usermod -aG docker jenkins
sudo systemctl restart jenkins

Verify inside Jenkins:

docker ps

🧠 12. Jenkins + Kubernetes (Optional)

If you have Kubernetes (like Minikube or AWS EKS):

  • Create k8s/deployment.yaml

  • Use Jenkins kubectl apply -f step

  • You can even run Jenkins inside Kubernetes with a Helm chart.


πŸͺΆ 13. Monitoring Jenkins

  • URL: http://localhost:8080/monitoring

  • Integrate Prometheus plugin

  • View build metrics and job status in Grafana dashboards.


🧩 14. Best Practices

βœ… Store pipeline code in Jenkinsfile (GitOps-style).
βœ… Use credentials from Jenkins Secret Store.
βœ… Keep stages modular: build, test, deploy, notify.
βœ… Use Docker agents for isolated builds.
βœ… Enable security: disable anonymous access.
βœ… Use backup plugin or external volume for /var/lib/jenkins.


⚑ 15. Sample Real-World Flow

Developer β†’ Git Push β†’
GitHub Webhook triggers Jenkins β†’
Build + Test β†’
Docker Image β†’
Push to Docker Hub β†’
Deploy to Kubernetes β†’
Prometheus + Grafana monitor app β†’
Slack notification sent.