Jenkins - Complete CI/CD Guide

Master continuous integration and deployment with the world's leading automation server

What You'll Learn

Introduction to Jenkins

Jenkins is an open-source automation server that enables developers to build, test, and deploy their software reliably. As the most popular CI/CD tool, Jenkins automates the software development lifecycle and facilitates continuous integration and continuous delivery.

Key Facts

  • Created by: Kohsuke Kawaguchi (2011, forked from Hudson)
  • Language: Java
  • License: MIT License
  • Plugins: 1,800+ available plugins
  • Community: Largest CI/CD community with millions of users

What is Continuous Integration/Continuous Delivery?

Continuous Integration (CI) is the practice of automatically building and testing code every time a team member commits changes to version control.

Continuous Delivery/Deployment (CD) extends CI by automatically deploying all code changes to a testing or production environment after the build stage.

Key Benefit: Jenkins helps teams catch bugs early, reduce integration problems, and deliver software faster and more reliably.

Why Choose Jenkins?

Open Source & Free

Completely free to use with no licensing costs. Large community support and active development.

Highly Extensible

Over 1,800 plugins available for integrating with virtually any tool in your DevOps toolchain.

Platform Independent

Written in Java, runs on Windows, Mac, Linux, and Unix systems. Supports all major version control systems.

Pipeline as Code

Define your entire build/test/deploy pipeline in a Jenkinsfile stored with your source code.

Distributed Builds

Scale horizontally by distributing builds across multiple machines for faster execution.

Industry Standard

Trusted by thousands of organizations worldwide, from startups to Fortune 500 companies.

Jenkins vs. Alternatives

Feature Jenkins GitLab CI GitHub Actions CircleCI
Cost Free (self-hosted) Free tier + paid Free tier + paid Free tier + paid
Plugins 1,800+ Limited Marketplace Orbs
Hosting Self-hosted Cloud or self-hosted Cloud only Cloud or self-hosted
Setup Complexity Moderate Easy Very Easy Easy

Jenkins Architecture

Master-Agent Architecture

Jenkins follows a master-agent (formerly master-slave) architecture:

Jenkins Master (Controller)

Jenkins Agents (Nodes)

Jenkins Master
    │
    ├── Agent 1 (Linux)     → Runs Java builds
    ├── Agent 2 (Windows)   → Runs .NET builds
    ├── Agent 3 (macOS)     → Runs iOS builds
    └── Agent 4 (Docker)    → Runs containerized builds

Installation & Setup

System Requirements

Installation Methods

1. Docker (Recommended for Testing)

# Pull and run Jenkins in Docker
docker run -d -p 8080:8080 -p 50000:50000 \
  --name jenkins \
  -v jenkins_home:/var/jenkins_home \
  jenkins/jenkins:lts

# Get initial admin password
docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword

2. Ubuntu/Debian

# Add Jenkins repository
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.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-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null

# Install Jenkins
sudo apt update
sudo apt install jenkins

# Start Jenkins
sudo systemctl start jenkins
sudo systemctl enable jenkins

3. Windows

Download the Windows installer from jenkins.io and follow the installation wizard.

Initial Configuration

  1. Access Jenkins at http://localhost:8080
  2. Retrieve the initial admin password from /var/jenkins_home/secrets/initialAdminPassword
  3. Install suggested plugins or select custom plugins
  4. Create the first admin user
  5. Configure Jenkins URL

Jobs & Builds

Job Types

1. Freestyle Project

The simplest job type. Good for simple tasks but less flexible than pipelines.

Use Cases:
  • Simple build scripts
  • Basic CI workflows
  • Quick prototypes

2. Pipeline

Define your entire CI/CD process as code. Most flexible and powerful.

3. Multibranch Pipeline

Automatically creates pipelines for each branch in your repository.

4. Organization Folder

Scans an entire GitHub/Bitbucket organization for repositories with Jenkinsfiles.

Creating a Freestyle Job

  1. Click "New Item" from the Jenkins dashboard
  2. Enter a job name and select "Freestyle project"
  3. Configure Source Code Management (e.g., Git repository)
  4. Add Build Triggers (e.g., Poll SCM, GitHub hook)
  5. Add Build Steps (e.g., Execute shell, Invoke Maven)
  6. Configure Post-build Actions (e.g., Archive artifacts, Send notifications)
  7. Save and build

Pipeline as Code

Jenkins Pipeline allows you to define your build process as code in a Jenkinsfile.

Declarative Pipeline (Recommended)

pipeline {
    agent any

    environment {
        APP_NAME = 'my-app'
        VERSION = '1.0.0'
    }

    stages {
        stage('Checkout') {
            steps {
                git branch: 'main',
                    url: 'https://github.com/user/repo.git'
            }
        }

        stage('Build') {
            steps {
                sh 'npm install'
                sh 'npm run build'
            }
        }

        stage('Test') {
            steps {
                sh 'npm test'
            }
        }

        stage('Deploy') {
            when {
                branch 'main'
            }
            steps {
                sh './deploy.sh'
            }
        }
    }

    post {
        success {
            echo 'Pipeline succeeded!'
            mail to: 'team@example.com',
                 subject: "Build Successful: ${env.JOB_NAME}",
                 body: "Good news! Build ${env.BUILD_NUMBER} succeeded."
        }
        failure {
            echo 'Pipeline failed!'
            mail to: 'team@example.com',
                 subject: "Build Failed: ${env.JOB_NAME}",
                 body: "Build ${env.BUILD_NUMBER} failed. Please investigate."
        }
    }
}

Scripted Pipeline (More Flexible)

node {
    stage('Checkout') {
        checkout scm
    }

    stage('Build') {
        try {
            sh 'make build'
        } catch (Exception e) {
            echo "Build failed: ${e.getMessage()}"
            throw e
        }
    }

    stage('Test') {
        parallel(
            'Unit Tests': {
                sh 'npm run test:unit'
            },
            'Integration Tests': {
                sh 'npm run test:integration'
            }
        )
    }

    stage('Deploy') {
        if (env.BRANCH_NAME == 'main') {
            sh 'kubectl apply -f deployment.yaml'
        }
    }
}

Pipeline Features

Plugins & Extensions

Jenkins' power comes from its extensive plugin ecosystem.

Essential Plugins

Version Control

Build Tools

Deployment & Cloud

Testing & Quality

Notifications

Installing Plugins

  1. Navigate to "Manage Jenkins" → "Manage Plugins"
  2. Go to the "Available" tab
  3. Search for the plugin
  4. Check the box and click "Install without restart"

Version Control Integration

GitHub Integration

Webhook Configuration

# In GitHub repository settings:
# 1. Go to Settings → Webhooks → Add webhook
# 2. Payload URL: http://your-jenkins-url/github-webhook/
# 3. Content type: application/json
# 4. Events: Push events, Pull request events

Multibranch Pipeline with GitHub

// Jenkinsfile in repository root
pipeline {
    agent any

    stages {
        stage('Build PR') {
            when {
                changeRequest()
            }
            steps {
                echo "Building PR #${env.CHANGE_ID}"
                sh 'npm install && npm run build'
            }
        }

        stage('Deploy Main') {
            when {
                branch 'main'
            }
            steps {
                echo "Deploying main branch"
                sh './deploy.sh production'
            }
        }
    }
}

GitLab Integration

Install the GitLab Plugin and configure webhooks similarly to GitHub.

Bitbucket Integration

Use Bitbucket Branch Source Plugin for Bitbucket Cloud/Server integration.

Automated Testing

Unit Testing

stage('Unit Tests') {
    steps {
        sh 'npm test'
        junit 'test-results/*.xml'  // Publish test results
    }
}

Integration Testing

stage('Integration Tests') {
    steps {
        // Start test environment
        sh 'docker-compose up -d'

        // Run tests
        sh 'npm run test:integration'

        // Cleanup
        sh 'docker-compose down'
    }
}

Code Coverage

stage('Code Coverage') {
    steps {
        sh 'npm run coverage'
        publishHTML target: [
            reportDir: 'coverage',
            reportFiles: 'index.html',
            reportName: 'Coverage Report'
        ]
    }
}

Continuous Deployment

Docker Deployment

stage('Build Docker Image') {
    steps {
        script {
            docker.build("myapp:${env.BUILD_NUMBER}")
        }
    }
}

stage('Push to Registry') {
    steps {
        script {
            docker.withRegistry('https://registry.example.com', 'docker-credentials') {
                docker.image("myapp:${env.BUILD_NUMBER}").push()
                docker.image("myapp:${env.BUILD_NUMBER}").push('latest')
            }
        }
    }
}

Kubernetes Deployment

stage('Deploy to Kubernetes') {
    steps {
        script {
            kubernetesDeploy(
                configs: 'k8s/*.yaml',
                kubeconfigId: 'kubeconfig-credentials',
                enableConfigSubstitution: true
            )
        }
    }
}

Blue-Green Deployment

stage('Blue-Green Deploy') {
    steps {
        // Deploy to green environment
        sh 'kubectl apply -f green-deployment.yaml'

        // Wait for health check
        sh 'kubectl wait --for=condition=ready pod -l app=green'

        // Switch traffic to green
        sh 'kubectl patch service myapp -p \'{"spec":{"selector":{"version":"green"}}}\''

        // Cleanup blue environment
        sh 'kubectl delete deployment blue'
    }
}

Security & Access Control

User Authentication

Authorization Strategies

Matrix-based Security

Fine-grained permissions per user or group for specific projects.

Role-Based Strategy

Define roles with specific permissions and assign users to roles.

Credentials Management

// Using credentials in Pipeline
pipeline {
    agent any

    stages {
        stage('Deploy') {
            steps {
                withCredentials([
                    usernamePassword(
                        credentialsId: 'aws-credentials',
                        usernameVariable: 'AWS_ACCESS_KEY',
                        passwordVariable: 'AWS_SECRET_KEY'
                    )
                ]) {
                    sh 'aws s3 sync build/ s3://my-bucket/'
                }
            }
        }
    }
}

Security Best Practices

Best Practices

Pipeline Development

Performance Optimization

Maintenance

Pro Tip: Use Jenkins Configuration as Code (JCasC) plugin to manage Jenkins configuration as code, making it reproducible and version-controlled.

Troubleshooting

Common Issues

Build Fails with Permission Denied

Solution:
  • Check file permissions in workspace
  • Verify Jenkins user has necessary permissions
  • Use appropriate credentials for external resources

Slow Build Performance

Solution:
  • Use distributed builds with agents
  • Enable parallel execution
  • Optimize dependency caching
  • Review plugin overhead

Out of Memory Errors

Solution:
  • Increase Java heap size: -Xmx4g
  • Clean up old builds and artifacts
  • Review plugin memory usage

Debugging Pipelines

// Enable debug output
pipeline {
    agent any

    options {
        timestamps()
        timeout(time: 1, unit: 'HOURS')
    }

    stages {
        stage('Debug') {
            steps {
                echo "Branch: ${env.BRANCH_NAME}"
                echo "Build Number: ${env.BUILD_NUMBER}"
                sh 'env | sort'  // Print all environment variables
            }
        }
    }
}

Additional Resources

Official Resources

Learning Resources

Community

Related Topics