Skip to main content

Jenkins Complete Guide:

 

Jenkins Complete Guide: From Installation to Advanced Pipelines

📅 Published: June 2026
⏱️ Estimated Reading Time: 28 minutes
🏷️ Tags: Jenkins, CI/CD, Automation, DevOps, Pipelines, Groovy


Introduction: What is Jenkins?

Jenkins is an open-source automation server that helps developers build, test, and deploy their software. It is the most widely used CI/CD tool, with a vast ecosystem of plugins and a strong community.

Think of Jenkins as the conductor of your software delivery orchestra. It coordinates all the moving parts: pulling code, running tests, building artifacts, and deploying applications. Jenkins makes these tasks consistent, repeatable, and automatic.

Why Jenkins remains popular:

  • Open source and free

  • 1,800+ plugins for almost any integration

  • Huge community and extensive documentation

  • Works with any language, any platform

  • Flexible pipeline definitions (Declarative and Scripted)


Part 1: Jenkins Architecture

Key Components

text
┌─────────────────────────────────────────────────────────────────┐
│                         Jenkins Master                           │
│  ┌─────────────────────────────────────────────────────────┐    │
│  │                     Web UI                              │    │
│  ├─────────────────────────────────────────────────────────┤    │
│  │                  Job Scheduler                          │    │
│  ├─────────────────────────────────────────────────────────┤    │
│  │               Plugin Manager                            │    │
│  ├─────────────────────────────────────────────────────────┤    │
│  │                  Build Queue                            │    │
│  └─────────────────────────────────────────────────────────┘    │
└────────────────────────────┬────────────────────────────────────┘
                             │
                             ▼
        ┌───────────────────────────────────────┐
        │              Agents                   │
        │  ┌───────────┐  ┌───────────┐        │
        │  │  Agent 1  │  │  Agent 2  │        │
        │  │ (Linux)   │  │ (Windows) │        │
        │  └───────────┘  └───────────┘        │
        └───────────────────────────────────────┘
ComponentDescription
Master/ControllerCentral management server. Hosts UI, schedules jobs, manages agents
AgentExecutes build jobs. Can run on different operating systems
Job/ProjectA configured build task with steps, triggers, and post-build actions
BuildA single execution of a job
WorkspaceDirectory where job source code and files are stored

Part 2: Installing Jenkins

Option 1: Docker (Quickest Setup)

bash
# Run Jenkins with persistent storage
docker run -d \
  --name jenkins \
  -p 8080:8080 \
  -p 50000:50000 \
  -v jenkins_home:/var/jenkins_home \
  jenkins/jenkins:lts

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

# Access Jenkins at http://localhost:8080

Option 2: Ubuntu/Debian

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

# Start Jenkins
sudo systemctl start jenkins
sudo systemctl enable jenkins

# Get initial admin password
sudo cat /var/lib/jenkins/secrets/initialAdminPassword

# Access Jenkins at http://localhost:8080

Option 3: Kubernetes

bash
# Install Jenkins using Helm
helm repo add jenkins https://charts.jenkins.io
helm repo update

helm install jenkins jenkins/jenkins \
  --set persistence.size=10Gi \
  --set serviceType=LoadBalancer

# Get admin password
kubectl exec --namespace default -it svc/jenkins -c jenkins -- \
  cat /var/jenkins_home/secrets/initialAdminPassword

Part 3: First-Time Setup

Step 1: Unlock Jenkins

  1. Get the initial admin password from logs

  2. Visit http://localhost:8080

  3. Enter the password

  4. Click Continue

Step 2: Install Plugins

Choose "Install suggested plugins" or select custom plugins.

Essential plugins for DevOps:

PluginPurpose
GitSource control integration
PipelineDefine builds as code
Docker PipelineDocker integration
KubernetesKubernetes integration
Blue OceanModern UI for pipelines
SonarQubeCode quality scanning
Slack/EmailNotifications
Credentials BindingSecure secrets

Step 3: Create Admin User

Create the first admin user for Jenkins.

Step 4: Configure Tools

Set up global tools:

  • Go to Manage JenkinsTools

  • Configure:

    • JDK installations

    • Git installations

    • Maven/Gradle configurations

    • Docker installations


Part 4: Jenkins Plugins

Plugin Management

bash
# Install plugin via CLI
jenkins-plugin-cli --plugins git pipeline-aws

# Update plugins
jenkins-plugin-cli --update

# List installed plugins
ls /var/lib/jenkins/plugins/

Common Plugin Categories

CategoryExample Plugins
Source ControlGit, GitHub, GitLab, Bitbucket
Build ToolsMaven, Gradle, Ant, npm
ContainerDocker, Kubernetes, OpenShift
CloudAWS, Azure, GCP
TestingJUnit, TestNG, Selenium
QualitySonarQube, Checkstyle, FindBugs
NotificationsEmail, Slack, Teams, PagerDuty
PipelinesPipeline, Blue Ocean, Job DSL

Part 5: Freestyle Jobs (Legacy but Common)

Creating a Freestyle Job

  1. New Item → Enter name → Select Freestyle project

  2. General → Description, discard old builds, concurrent builds

  3. Source Code Management → Git, enter repository URL, credentials

  4. Build Triggers → Poll SCM, GitHub hook, scheduled

  5. Build → Add build step

  6. Post-build Actions → Archive artifacts, publish test reports, send notifications

Build Triggers

TriggerUse Case
Poll SCMCheck for changes every X minutes
GitHub hookTrigger on GitHub push
ScheduleRun at specific times (cron)
Build after other projectsChain dependencies
WebhookTrigger via API call

Build Steps Examples

bash
# Execute shell step
npm install
npm test
npm run build

# Execute Windows batch step
npm install
npm test
npm run build

# Invoke Gradle step
gradle clean build

# Invoke Maven step
mvn clean install

Part 6: Jenkins Pipeline (Modern Standard)

Pipeline vs Freestyle

AspectFreestylePipeline
Defined asUI configurationCode (Jenkinsfile)
Version controlNot versionedVersioned in Git
ComplexitySimple, limitedComplex, powerful
ReusabilityLimitedHigh
Parallel stagesNoYes
RecoveryManualAutomatic

Declarative Pipeline Syntax

groovy
pipeline {
    // Global settings
    agent any
    
    tools {
        maven 'Maven-3.6'
        jdk 'JDK-11'
    }
    
    options {
        timeout(time: 1, unit: 'HOURS')
        buildDiscarder(logRotator(numToKeepStr: '10'))
    }
    
    environment {
        // Environment variables
        APP_NAME = 'my-app'
        DOCKER_IMAGE = 'myapp:latest'
    }
    
    stages {
        // Stage 1: Checkout
        stage('Checkout') {
            steps {
                checkout scm
            }
        }
        
        // Stage 2: Build
        stage('Build') {
            steps {
                sh 'mvn clean compile'
            }
        }
        
        // Stage 3: Test
        stage('Test') {
            steps {
                sh 'mvn test'
            }
            post {
                always {
                    junit 'target/surefire-reports/*.xml'
                }
            }
        }
        
        // Stage 4: Package
        stage('Package') {
            steps {
                sh 'mvn package'
            }
        }
        
        // Stage 5: Docker Build
        stage('Docker Build') {
            steps {
                script {
                    docker.build("${DOCKER_IMAGE}")
                }
            }
        }
        
        // Stage 6: Deploy
        stage('Deploy') {
            when {
                branch 'main'
            }
            steps {
                sh 'kubectl apply -f k8s/deployment.yaml'
            }
        }
    }
    
    post {
        success {
            emailext (
                subject: "Build Passed: ${env.JOB_NAME}",
                body: "Build ${env.BUILD_NUMBER} passed",
                to: 'team@example.com'
            )
        }
        failure {
            emailext (
                subject: "Build Failed: ${env.JOB_NAME}",
                body: "Build ${env.BUILD_NUMBER} failed",
                to: 'team@example.com'
            )
        }
    }
}

Pipeline Directives

DirectivePurpose
agentWhere to run the pipeline
toolsInstall tools automatically
optionsPipeline configuration options
environmentSet environment variables
stagesCollection of build stages
stageIndividual build step
stepsCommands to execute
whenConditional execution
parallelRun stages in parallel
postActions after pipeline completes

Post-Build Conditions

groovy
post {
    always {
        // Always run
        cleanWs()
    }
    success {
        // On successful build
        archiveArtifacts artifacts: 'target/*.jar'
    }
    failure {
        // On failed build
        notifyBuildFailure()
    }
    unstable {
        // On unstable build
        mail to: 'dev@example.com', subject: "Build unstable"
    }
    aborted {
        // On aborted build
        echo "Build was aborted"
    }
}

Part 7: Scripted Pipeline (Advanced)

Scripted pipeline provides more flexibility but is less readable.

groovy
node('linux') {
    stage('Checkout') {
        checkout scm
    }
    
    stage('Build') {
        sh 'mvn clean compile'
    }
    
    stage('Test') {
        sh 'mvn test'
    }
    
    stage('Package') {
        sh 'mvn package'
    }
    
    stage('Docker Build') {
        docker.build("myapp:${env.BUILD_ID}")
    }
    
    stage('Deploy') {
        if (env.BRANCH_NAME == 'main') {
            sh 'kubectl apply -f k8s/deployment.yaml'
        }
    }
    
    // Try-catch for error handling
    try {
        sh 'mvn deploy'
    } catch (Exception e) {
        echo "Deployment failed: ${e.message}"
        currentBuild.result = 'FAILURE'
        throw e
    } finally {
        cleanWs()
    }
}

Part 8: Jenkinsfile Examples

Node.js Application

groovy
pipeline {
    agent any
    
    environment {
        NODE_VERSION = '18'
    }
    
    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }
        
        stage('Install') {
            steps {
                sh 'npm ci'
            }
        }
        
        stage('Lint') {
            steps {
                sh 'npm run lint'
            }
        }
        
        stage('Test') {
            steps {
                sh 'npm test'
            }
            post {
                always {
                    junit 'test-results.xml'
                }
            }
        }
        
        stage('Build') {
            steps {
                sh 'npm run build'
            }
            post {
                success {
                    archiveArtifacts artifacts: 'build/**'
                }
            }
        }
        
        stage('Docker Build') {
            steps {
                script {
                    docker.build("myapp:${env.BUILD_ID}")
                }
            }
        }
        
        stage('Deploy') {
            when {
                branch 'main'
            }
            steps {
                script {
                    docker.image("myapp:${env.BUILD_ID}").push()
                }
                sh 'kubectl rollout restart deployment/myapp'
            }
        }
    }
}

Python Application

groovy
pipeline {
    agent any
    
    environment {
        PYTHON_VERSION = '3.9'
    }
    
    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }
        
        stage('Install Dependencies') {
            steps {
                sh 'pip install -r requirements.txt'
                sh 'pip install pytest pytest-cov flake8'
            }
        }
        
        stage('Lint') {
            steps {
                sh 'flake8 src/'
            }
        }
        
        stage('Test') {
            steps {
                sh 'pytest --cov=src --junitxml=test-results.xml'
            }
            post {
                always {
                    junit 'test-results.xml'
                }
            }
        }
        
        stage('Build') {
            steps {
                sh 'python setup.py sdist bdist_wheel'
            }
            post {
                success {
                    archiveArtifacts artifacts: 'dist/*.whl'
                }
            }
        }
        
        stage('Deploy') {
            when {
                branch 'main'
            }
            steps {
                sh 'twine upload --repository pypi dist/*'
            }
        }
    }
}

Terraform Pipeline

groovy
pipeline {
    agent any
    
    environment {
        AWS_DEFAULT_REGION = 'us-west-2'
    }
    
    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }
        
        stage('Terraform Init') {
            steps {
                sh 'terraform init'
            }
        }
        
        stage('Terraform Plan') {
            steps {
                sh 'terraform plan -no-color'
            }
            post {
                always {
                    archiveArtifacts artifacts: 'terraform.tfplan'
                }
            }
        }
        
        stage('Terraform Apply') {
            when {
                branch 'main'
            }
            steps {
                input message: 'Apply Terraform?', ok: 'Yes'
                sh 'terraform apply -auto-approve'
            }
        }
    }
}

Part 9: Parallel Execution

groovy
pipeline {
    agent none
    
    stages {
        stage('Build and Test') {
            parallel {
                stage('Build') {
                    agent { label 'linux' }
                    steps {
                        sh 'mvn clean compile'
                    }
                }
                
                stage('Unit Tests') {
                    agent { label 'linux' }
                    steps {
                        sh 'mvn test'
                    }
                }
                
                stage('Integration Tests') {
                    agent { label 'linux' }
                    steps {
                        sh 'mvn verify -P integration'
                    }
                }
                
                stage('Security Scan') {
                    agent { label 'linux' }
                    steps {
                        sh 'snyk test'
                    }
                }
            }
        }
        
        stage('Deploy') {
            steps {
                echo 'Deploying...'
            }
        }
    }
}

Part 10: Jenkins Agents

Configuring Agents

Linux Agent (SSH):

  1. Manage Jenkins → Nodes → New Node

  2. Node name: linux-agent

  3. Type: Permanent Agent

  4. Remote root directory: /home/jenkins/agent

  5. Launch method: Launch agents via SSH

  6. Host: linux-agent.example.com

  7. Credentials: SSH key

Docker Agent:

groovy
pipeline {
    agent {
        docker {
            image 'node:18-alpine'
            args '-v /cache:/cache'
        }
    }
    stages {
        stage('Build') {
            steps {
                sh 'npm install'
                sh 'npm test'
            }
        }
    }
}

Kubernetes Agent:

groovy
pipeline {
    agent {
        kubernetes {
            cloud 'kubernetes'
            yaml """
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: jnlp
    image: jenkins/inbound-agent:latest
    imagePullPolicy: IfNotPresent
  - name: docker
    image: docker:latest
    command: ['cat']
    tty: true
  - name: kubectl
    image: bitnami/kubectl:latest
    command: ['cat']
    tty: true
"""
        }
    }
}

Part 11: Advanced Pipeline Patterns

Shared Libraries

Directory Structure:

text
vars/
  git.groovy
  docker.groovy
  deploy.groovy
src/
  com/company/
    BuildUtils.groovy
resources/

vars/git.groovy:

groovy
def checkoutRepo(repoUrl, branch) {
    checkout([
        $class: 'GitSCM',
        branches: [[name: branch]],
        userRemoteConfigs: [[url: repoUrl]]
    ])
}

vars/deploy.groovy:

groovy
def deployKubernetes(deploymentFile, namespace) {
    sh """
        kubectl apply -f ${deploymentFile} -n ${namespace}
        kubectl rollout status deployment/${deploymentFile.replace('.yaml','')} -n ${namespace}
    """
}

Using Shared Libraries:

groovy
@Library('my-shared-library') _

pipeline {
    agent any
    
    stages {
        stage('Checkout') {
            steps {
                git.checkoutRepo('https://github.com/myorg/myapp.git', 'main')
            }
        }
        
        stage('Deploy') {
            steps {
                deploy.deployKubernetes('k8s/deployment.yaml', 'production')
            }
        }
    }
}

Part 12: Credentials Management

Adding Credentials

  1. Manage Jenkins → Credentials → System → Global credentials

  2. Add Credentials

Types:

TypeUse Case
Username with passwordBasic auth, GitHub
SSH Username with private keySSH access to servers
Secret textAPI keys, tokens
Secret fileCertificate files
Docker Host CertificateDocker registry auth

Using Credentials in Pipeline

groovy
pipeline {
    environment {
        // Reference credentials
        GITHUB_TOKEN = credentials('github-token')
        AWS_CREDS = credentials('aws-credentials')
        DOCKER_CREDS = credentials('docker-hub')
    }
    
    stages {
        stage('Use Credentials') {
            steps {
                // Environment variables are available
                sh "aws configure set aws_access_key_id ${AWS_CREDS_USR}"
                sh "aws configure set aws_secret_access_key ${AWS_CREDS_PSW}"
                
                // Docker login with credentials
                sh """
                    echo ${DOCKER_CREDS_PSW} | docker login -u ${DOCKER_CREDS_USR} --password-stdin
                """
            }
        }
        
        stage('With Credentials') {
            steps {
                withCredentials([
                    string(credentialsId: 'api-key', variable: 'API_KEY'),
                    file(credentialsId: 'cert-file', variable: 'CERT_FILE')
                ]) {
                    sh 'python deploy.py --api-key $API_KEY --cert $CERT_FILE'
                }
            }
        }
    }
}

Part 13: Notifications

Email Notifications

groovy
post {
    success {
        emailext (
            to: 'team@example.com',
            subject: "${env.JOB_NAME} - Build ${env.BUILD_NUMBER} - SUCCESS",
            body: """
                Build: ${env.BUILD_URL}
                Branch: ${env.BRANCH_NAME}
                Duration: ${currentBuild.duration}
            """
        )
    }
    failure {
        emailext (
            to: 'team@example.com, oncall@example.com',
            subject: "${env.JOB_NAME} - Build ${env.BUILD_NUMBER} - FAILED",
            body: """
                Build: ${env.BUILD_URL}
                Branch: ${env.BRANCH_NAME}
                Console: ${env.BUILD_URL}console
                Failed stage: ${currentBuild.failureStage}
            """
        )
    }
}

Slack Notifications

groovy
post {
    success {
        slackSend(
            color: 'good',
            message: "Pipeline ${env.JOB_NAME} - Build ${env.BUILD_NUMBER} succeeded"
        )
    }
    failure {
        slackSend(
            color: 'danger',
            message: "Pipeline ${env.JOB_NAME} - Build ${env.BUILD_NUMBER} failed"
        )
    }
}

Part 14: Best Practices

Jenkinsfile Best Practices

Version your Jenkinsfile – Store in Git with your code

Keep pipelines simple – Avoid complex Groovy logic

Use environment variables – For configuration

Fail fast – Run fast tests first

Use parallel stages – Speed up execution

Clean workspace – Start fresh each build

Use shared libraries – Don't repeat yourself

Implement input for critical steps – Require manual approval

groovy
stage('Deploy to Production') {
    when { branch 'main' }
    input {
        message 'Deploy to production?'
        submitter 'admin,lead'
    }
    steps {
        sh 'deploy.sh'
    }
}

Security Best Practices

  • Never store credentials in Jenkinsfiles

  • Use Credentials Binding plugin for secrets

  • Limit agent access to specific jobs

  • Regularly update Jenkins and plugins

  • Use matrix-based security

  • Enable security realm and authorization


Jenkins Commands Cheat Sheet

bash
# Start/Stop Jenkins
sudo systemctl start jenkins
sudo systemctl stop jenkins
sudo systemctl restart jenkins

# Check logs
sudo journalctl -u jenkins -f

# Jenkins CLI
java -jar jenkins-cli.jar -s http://localhost:8080 help
java -jar jenkins-cli.jar -s http://localhost:8080 list-jobs
java -jar jenkins-cli.jar -s http://localhost:8080 build my-job

# Reload configuration
java -jar jenkins-cli.jar -s http://localhost:8080 reload-configuration

# Install plugins
java -jar jenkins-cli.jar -s http://localhost:8080 install-plugin git pipeline

# Restart safely
java -jar jenkins-cli.jar -s http://localhost:8080 safe-restart

# Clear build queue
java -jar jenkins-cli.jar -s http://localhost:8080 clear-queue

Summary

TopicKey Points
InstallationDocker, Linux, Kubernetes options
Plugins1,800+ plugins for integrations
FreestyleSimple, UI-based jobs
PipelineCode-based, versioned, powerful
DeclarativeEasier, structured syntax
ScriptedMore flexible, complex
AgentsDistributed execution
CredentialsSecure secret management
NotificationsEmail, Slack, Teams

Jenkins remains a cornerstone of CI/CD pipelines. Start with a simple pipeline and gradually add complexity as your team's needs grow.


Learn More

Practice Jenkins with hands-on exercises in our interactive labs:
https://devops.trainwithsky.com

Comments

Popular posts from this blog

🌐 Holographic Communications & 6G: The Future of Immersive Connectivity

  🌐 Holographic Communications & 6G: The Future of Immersive Connectivity 🚀 Introduction As the world moves towards 6G , a revolutionary technology is set to redefine digital interactions: Holographic Communications . Imagine real-time, 3D holographic video calls, immersive remote collaboration, and lifelike virtual experiences —all powered by ultra-fast, ultra-low-latency 6G networks . This topic explores Holographic Communications , its impact on various industries, key enabling technologies, and how 6G will bring this futuristic concept to reality . Shape Your Future with AI & Infinite Knowledge...!! Want to Generate Text-to-Voice, Images & Videos? http://www.ai.skyinfinitetech.com Read In-Depth Tech & Self-Improvement Blogs http://www.skyinfinitetech.com Watch Life-Changing Videos on YouTube https://www.youtube.com/@SkyInfinite-Learning Transform Your Skills, Business & Productivity – Join Us Today! 🔍 1. What is Holographic Communication? Hologr...

How to Use SKY TTS: The Complete, Step-by-Step Guide for 2025

 What is SKY TTS? SKY TTS  is a free, next-generation  AI audio creation platform  that brings together high-quality  Text-to-Speech ,  Speech-to-Text , and a full suite of professional  audio editing tools  in one seamless experience. Our vision is simple — to make advanced audio technology  free, accessible, and effortless  for everyone. From creators and educators to podcasters, developers, and businesses, SKY TTS helps users produce  studio-grade voice content  without expensive software or technical skills. With support for  70+ languages, natural voices, audio enhancement, waveform generation, and batch automation , SKY TTS has become a trusted all-in-one toolkit for modern digital audio workflows. Why Choose SKY TTS? Instant Conversion:  Enjoy rapid text-to-speech generation, even with large documents. Advanced Voice Settings:   Adjust speed, pitch, and style for a personalized listening experience. Multi-...

📊 Monitoring & Logging in Kubernetes – Tools like Prometheus, Grafana, and Fluentd

  Monitoring & Logging in Kubernetes – Tools like Prometheus, Grafana, and Fluentd Monitoring and logging are essential for maintaining a healthy and well-performing Kubernetes cluster. In this guide, we’ll cover why monitoring is important, key monitoring tools like Prometheus and Grafana, and logging tools like Fluentd to help you gain visibility into your cluster’s performance and logs. Shape Your Future with AI & Infinite Knowledge...!! Want to Generate Text-to-Voice, Images & Videos? http://www.ai.skyinfinitetech.com Read In-Depth Tech & Self-Improvement Blogs http://www.skyinfinitetech.com Watch Life-Changing Videos on YouTube https://www.youtube.com/@SkyInfinite-Learning Transform Your Skills, Business & Productivity – Join Us Today! 🚀 Introduction In today’s fast-paced cloud-native environment, Kubernetes has emerged as the de-facto container orchestration platform. But deploying and managing applications in Kubernetes is just half the ba...