Skip to content

Jenkins

Overview

Jenkins is an open-source automation server used to build, test, and deploy software continuously. It enables teams to practice Continuous Integration and Continuous Deployment (CI/CD).


Core Concepts

Pipeline vs Freestyle Jobs

Aspect Freestyle Pipeline
Definition UI configuration Code-based (Jenkinsfile)
Version Control Manual Stored in Git
Reusability Limited High
Debugging Difficult Easy
Scalability Limited Excellent

Jenkins Architecture

┌─────────────────────────────────┐
│      Jenkins Master             │
│  - Job Orchestration            │
│  - Web UI                       │
│  - Plugin Management            │
└──────────────┬──────────────────┘
               │
     ┌─────────┼─────────┐
     │         │         │
   Agent1    Agent2    Agent3
  (Linux)   (Windows) (Docker)

Installation & Setup

Docker Installation

# Pull Jenkins image
docker pull jenkins/jenkins:lts

# Run Jenkins container
docker run -d -p 8080:8080 -p 50000:50000 \
  -v jenkins_home:/var/jenkins_home \
  --name jenkins \
  jenkins/jenkins:lts

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

Linux Installation

# Update package manager
sudo apt update

# Install Java
sudo apt install -y openjdk-11-jdk

# Add Jenkins repository
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'

# Install Jenkins
sudo apt update
sudo apt install -y jenkins

# Start Jenkins
sudo systemctl start jenkins
sudo systemctl enable jenkins

Pipeline Basics

Declarative Pipeline

pipeline {
    agent any

    triggers {
        githubPush()  // Trigger on GitHub push
    }

    environment {
        APP_NAME = 'MyApp'
        VERSION = '1.0.0'
        DOCKER_REGISTRY = 'docker.io'
    }

    options {
        buildDiscarder(logRotator(numToKeepStr: '10'))
        timeout(time: 1, unit: 'HOURS')
    }

    stages {
        stage('Checkout') {
            steps {
                echo 'Checking out code...'
                checkout scm
            }
        }

        stage('Build') {
            steps {
                echo 'Building application...'
                sh 'mvn clean package'
            }
        }

        stage('Test') {
            steps {
                echo 'Running tests...'
                sh 'mvn test'
                junit 'target/surefire-reports/*.xml'
            }
        }

        stage('SonarQube Analysis') {
            steps {
                echo 'Running code quality scan...'
                sh 'mvn sonar:sonar'
            }
        }

        stage('Build Docker Image') {
            steps {
                echo 'Building Docker image...'
                sh 'docker build -t ${DOCKER_REGISTRY}/${APP_NAME}:${VERSION} .'
            }
        }

        stage('Push to Registry') {
            steps {
                echo 'Pushing image to registry...'
                sh 'docker push ${DOCKER_REGISTRY}/${APP_NAME}:${VERSION}'
            }
        }

        stage('Deploy to Dev') {
            steps {
                echo 'Deploying to development...'
                sh 'kubectl set image deployment/myapp myapp=${DOCKER_REGISTRY}/${APP_NAME}:${VERSION} -n dev'
            }
        }

        stage('Deploy to Prod') {
            when {
                branch 'main'
            }
            steps {
                echo 'Deploying to production...'
                input 'Approve production deployment?'
                sh 'kubectl set image deployment/myapp myapp=${DOCKER_REGISTRY}/${APP_NAME}:${VERSION} -n prod'
            }
        }
    }

    post {
        always {
            echo 'Cleaning up...'
            cleanWs()
        }
        success {
            echo 'Pipeline succeeded!'
            // Send success notification
        }
        failure {
            echo 'Pipeline failed!'
            // Send failure notification
        }
    }
}

Scripted Pipeline

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

        stage('Build') {
            sh 'mvn clean package'
        }

        stage('Test') {
            sh 'mvn test'
        }

        stage('Deploy') {
            sh './deploy.sh'
        }
    }
    catch (Exception e) {
        echo "Build failed: ${e.message}"
        throw e
    }
}

Essential Plugins

Plugin Purpose Use Case
GitHub SCM integration Git repositories
Docker Container management Build & push images
Kubernetes K8s integration Deploy to Kubernetes
SonarQube Code quality Quality gates
Slack Notifications Team notifications
Email Email alerts Build notifications
JUnit Test reporting Test result parsing
Artifactory Artifact storage Artifact management

Jenkins Credentials

Types of Credentials

pipeline {
    agent any

    environment {
        // Use credentials plugin
        DOCKER_CREDS = credentials('docker-credentials')
        GIT_TOKEN = credentials('github-token')
    }

    stages {
        stage('Authenticate') {
            steps {
                sh '''
                    echo $DOCKER_CREDS_PSW | docker login -u $DOCKER_CREDS_USR --password-stdin
                '''
            }
        }
    }
}

Store Credentials

// Jenkins Credentials UI: Manage Jenkins → Manage Credentials

// Use in pipeline:
withCredentials([
    usernamePassword(
        credentialsId: 'docker-creds',
        usernameVariable: 'DOCKER_USER',
        passwordVariable: 'DOCKER_PASS'
    )
]) {
    sh 'echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin'
}

Integration Examples

GitHub Webhook Trigger

pipeline {
    agent any

    triggers {
        githubPush()
    }

    stages {
        stage('Pull Code') {
            steps {
                checkout([
                    $class: 'GitSCM',
                    branches: [[name: '*/main']],
                    userRemoteConfigs: [[
                        url: 'https://github.com/user/repo.git'
                    ]]
                ])
            }
        }
    }
}

Docker Integration

pipeline {
    agent any

    stages {
        stage('Build and Push') {
            steps {
                script {
                    docker.withRegistry('https://docker.io', 'docker-credentials') {
                        def app = docker.build("myapp:${BUILD_NUMBER}")
                        app.push("latest")
                    }
                }
            }
        }
    }
}

Kubernetes Deployment

pipeline {
    agent any

    stages {
        stage('Deploy') {
            steps {
                withKubeConfig([credentialsId: 'kubeconfig']) {
                    sh 'kubectl apply -f deployment.yaml'
                    sh 'kubectl rollout status deployment/myapp'
                }
            }
        }
    }
}

Job Configuration

Job Types

Type Use Case Benefit
Pipeline Complex workflows Code-based, version controlled
Freestyle Simple tasks Easy to configure
Multibranch Multiple branches Branch-specific builds
Folder Organization Logical grouping

Common Parameters

pipeline {
    agent any

    parameters {
        string(
            name: 'ENVIRONMENT',
            defaultValue: 'dev',
            description: 'Deployment environment'
        )
        choice(
            name: 'VERSION',
            choices: ['1.0.0', '1.0.1', '1.1.0'],
            description: 'Application version'
        )
        booleanParam(
            name: 'RUN_TESTS',
            defaultValue: true,
            description: 'Run tests'
        )
    }

    stages {
        stage('Deploy') {
            steps {
                echo "Deploying version ${params.VERSION} to ${params.ENVIRONMENT}"
                echo "Run tests: ${params.RUN_TESTS}"
            }
        }
    }
}

Monitoring & Logging

View Logs

# Jenkins logs location
sudo journalctl -u jenkins -f

# Docker Jenkins logs
docker logs -f jenkins

# Workspace logs
/var/lib/jenkins/jobs/JobName/builds/1/log

Health Check

# Check Jenkins status
curl http://localhost:8080/api/json

# Verify plugins
curl http://localhost:8080/pluginManager/api/json

# Check nodes
curl http://localhost:8080/computer/api/json

Best Practices

Practice Benefit Implementation
Use Jenkinsfile Version control Store in Git repo
Declarative syntax Readability Avoid scripted when possible
Secure credentials Security Use credential plugin
Parallel stages Performance Reduce build time
Build artifacts Reusability Archive outputs
Build history Troubleshooting Keep logs
Notifications Awareness Slack, email alerts
Test reports Quality Publish test results

Troubleshooting

Common Issues

# Jenkins won't start
sudo systemctl status jenkins
sudo tail -f /var/log/jenkins/jenkins.log

# Out of memory
# Increase JVM heap: JAVA_ARGS="-Xmx2g"

# Slow builds
# Check agent connectivity, add more agents

# Plugin conflicts
# Disable plugin, restart Jenkins

Summary Table: Pipeline Stages

Stage Purpose Command
Checkout Get source code checkout scm
Build Compile code mvn clean package
Test Run tests mvn test
Analysis Code quality mvn sonar:sonar
Publish Create artifact docker build
Deploy Dev Deploy to dev kubectl apply
Approval Manual gate input 'Approve?'
Deploy Prod Deploy to production kubectl apply

Resources