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
┌─────────────────────────────────────────────────────────────────┐
│ Jenkins Master │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ Web UI │ │
│ ├─────────────────────────────────────────────────────────┤ │
│ │ Job Scheduler │ │
│ ├─────────────────────────────────────────────────────────┤ │
│ │ Plugin Manager │ │
│ ├─────────────────────────────────────────────────────────┤ │
│ │ Build Queue │ │
│ └─────────────────────────────────────────────────────────┘ │
└────────────────────────────┬────────────────────────────────────┘
│
▼
┌───────────────────────────────────────┐
│ Agents │
│ ┌───────────┐ ┌───────────┐ │
│ │ Agent 1 │ │ Agent 2 │ │
│ │ (Linux) │ │ (Windows) │ │
│ └───────────┘ └───────────┘ │
└───────────────────────────────────────┘| Component | Description |
|---|---|
| Master/Controller | Central management server. Hosts UI, schedules jobs, manages agents |
| Agent | Executes build jobs. Can run on different operating systems |
| Job/Project | A configured build task with steps, triggers, and post-build actions |
| Build | A single execution of a job |
| Workspace | Directory where job source code and files are stored |
Part 2: Installing Jenkins
Option 1: Docker (Quickest Setup)
# 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
# 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
# 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
Get the initial admin password from logs
Visit
http://localhost:8080Enter the password
Click Continue
Step 2: Install Plugins
Choose "Install suggested plugins" or select custom plugins.
Essential plugins for DevOps:
| Plugin | Purpose |
|---|---|
| Git | Source control integration |
| Pipeline | Define builds as code |
| Docker Pipeline | Docker integration |
| Kubernetes | Kubernetes integration |
| Blue Ocean | Modern UI for pipelines |
| SonarQube | Code quality scanning |
| Slack/Email | Notifications |
| Credentials Binding | Secure secrets |
Step 3: Create Admin User
Create the first admin user for Jenkins.
Step 4: Configure Tools
Set up global tools:
Go to Manage Jenkins → Tools
Configure:
JDK installations
Git installations
Maven/Gradle configurations
Docker installations
Part 4: Jenkins Plugins
Plugin Management
# 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
| Category | Example Plugins |
|---|---|
| Source Control | Git, GitHub, GitLab, Bitbucket |
| Build Tools | Maven, Gradle, Ant, npm |
| Container | Docker, Kubernetes, OpenShift |
| Cloud | AWS, Azure, GCP |
| Testing | JUnit, TestNG, Selenium |
| Quality | SonarQube, Checkstyle, FindBugs |
| Notifications | Email, Slack, Teams, PagerDuty |
| Pipelines | Pipeline, Blue Ocean, Job DSL |
Part 5: Freestyle Jobs (Legacy but Common)
Creating a Freestyle Job
New Item → Enter name → Select Freestyle project
General → Description, discard old builds, concurrent builds
Source Code Management → Git, enter repository URL, credentials
Build Triggers → Poll SCM, GitHub hook, scheduled
Build → Add build step
Post-build Actions → Archive artifacts, publish test reports, send notifications
Build Triggers
| Trigger | Use Case |
|---|---|
| Poll SCM | Check for changes every X minutes |
| GitHub hook | Trigger on GitHub push |
| Schedule | Run at specific times (cron) |
| Build after other projects | Chain dependencies |
| Webhook | Trigger via API call |
Build Steps Examples
# 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
| Aspect | Freestyle | Pipeline |
|---|---|---|
| Defined as | UI configuration | Code (Jenkinsfile) |
| Version control | Not versioned | Versioned in Git |
| Complexity | Simple, limited | Complex, powerful |
| Reusability | Limited | High |
| Parallel stages | No | Yes |
| Recovery | Manual | Automatic |
Declarative Pipeline Syntax
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
| Directive | Purpose |
|---|---|
agent | Where to run the pipeline |
tools | Install tools automatically |
options | Pipeline configuration options |
environment | Set environment variables |
stages | Collection of build stages |
stage | Individual build step |
steps | Commands to execute |
when | Conditional execution |
parallel | Run stages in parallel |
post | Actions after pipeline completes |
Post-Build Conditions
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.
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
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
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
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
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):
Manage Jenkins → Nodes → New Node
Node name:
linux-agentType: Permanent Agent
Remote root directory:
/home/jenkins/agentLaunch method: Launch agents via SSH
Host:
linux-agent.example.comCredentials: SSH key
Docker Agent:
pipeline { agent { docker { image 'node:18-alpine' args '-v /cache:/cache' } } stages { stage('Build') { steps { sh 'npm install' sh 'npm test' } } } }
Kubernetes Agent:
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:
vars/
git.groovy
docker.groovy
deploy.groovy
src/
com/company/
BuildUtils.groovy
resources/vars/git.groovy:
def checkoutRepo(repoUrl, branch) { checkout([ $class: 'GitSCM', branches: [[name: branch]], userRemoteConfigs: [[url: repoUrl]] ]) }
vars/deploy.groovy:
def deployKubernetes(deploymentFile, namespace) { sh """ kubectl apply -f ${deploymentFile} -n ${namespace} kubectl rollout status deployment/${deploymentFile.replace('.yaml','')} -n ${namespace} """ }
Using Shared Libraries:
@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
Manage Jenkins → Credentials → System → Global credentials
Add Credentials
Types:
| Type | Use Case |
|---|---|
| Username with password | Basic auth, GitHub |
| SSH Username with private key | SSH access to servers |
| Secret text | API keys, tokens |
| Secret file | Certificate files |
| Docker Host Certificate | Docker registry auth |
Using Credentials in Pipeline
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
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
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
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
# 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
| Topic | Key Points |
|---|---|
| Installation | Docker, Linux, Kubernetes options |
| Plugins | 1,800+ plugins for integrations |
| Freestyle | Simple, UI-based jobs |
| Pipeline | Code-based, versioned, powerful |
| Declarative | Easier, structured syntax |
| Scripted | More flexible, complex |
| Agents | Distributed execution |
| Credentials | Secure secret management |
| Notifications | Email, 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
Post a Comment