Jenkins Real-Time Examples and Solutions
Scenario 1: Reusable Pipelines with Shared Libraries
Problem: You have 50 microservices, and each has a Jenkinsfile. They all look 90% identical (checkout, build, test, push). Updating the pipeline logic requires editing 50 files.
Solution: Use Jenkins Shared Libraries.
* Setup: Create a Git repo with a vars/ directory. Create a file standardPipeline.groovy.
* Code:
groovy
// vars/standardPipeline.groovy
def call(Map config) {
pipeline {
agent any
stages {
stage('Build') {
steps {
sh "mvn clean package"
}
}
// ... other stages
}
}
}
* Usage: In each microservice's Jenkinsfile:
groovy
@Library('my-shared-lib') _
standardPipeline(repo: 'my-repo')
* Benefit: Change logic in one place, update everywhere.
Scenario 2: Isolated Builds with Docker Agents
Problem: One project needs Node.js 14, another needs Node.js 18. Installing both on the Jenkins controller or static agents causes conflicts.
Solution: Use Docker agents in the pipeline.
* Code:
groovy
pipeline {
agent none
stages {
stage('Build Frontend') {
agent {
docker { image 'node:14-alpine' }
}
steps {
sh 'npm install'
}
}
stage('Build Backend') {
agent {
docker { image 'maven:3.8-openjdk-11' }
}
steps {
sh 'mvn package'
}
}
}
}
* Benefit: Each stage runs in a clean, isolated container with the exact tools it needs.
Scenario 3: Triggering Downstream Jobs
Problem: After the "Backend API" job finishes successfully, you want to automatically trigger the "Integration Tests" job.
Solution: Use the build step.
* Code:
groovy
stage('Trigger Tests') {
steps {
build job: 'integration-tests', parameters: [string(name: 'ENV', value: 'staging')]
}
}