This project demonstrates how to use a Jenkins Shared Library to build a reusable CI/CD pipeline for a Docker-based application.
Instead of writing pipelines in every project, we centralize logic using a shared library.
- Reuse pipeline code across projects
- Build Docker image from source code
- Push image to DockerHub
- Understand Jenkins Shared Library structure
GitHub Repo (App Code)
↓
Jenkins Pipeline
↓
Shared Library (Reusable Code)
↓
Docker Build & Push
jenkins-shared-library/
│
├── vars/
│ ├── ciPipeline.groovy
│ ├── dockerBuild.groovy
│ ├── dockerPush.groovy
│ └── gitCheckout.groovy
│
└── src/
└── com/devops/utils.groovy
sudo yum install git -y Go to:
Manage Jenkins → Configure System → Global Pipeline Libraries
Add:
-
Name:
mind-lib -
Default Version:
main -
SCM: Git
-
Repository URL:
https://github.com/jadalaramani/jenkins-shared-library.git
Go to:
Manage Jenkins → Credentials
Add:
- Kind: Username with Password
- ID:
dockerhub-cred - Username: your DockerHub username
- Password: your DockerHub password
def call(String repoUrl, String branch='main') {
git branch: branch, url: repoUrl
}def call(String imageName, String tag) {
sh "docker build -t ${imageName}:${tag} ."
}def call(String imageName, String tag, String credId) {
withCredentials([usernamePassword(
credentialsId: credId,
usernameVariable: 'DOCKER_USER',
passwordVariable: 'DOCKER_PASS'
)]) {
sh """
echo "$DOCKER_PASS" | docker login -u "$DOCKER_USER" --password-stdin
docker push ${imageName}:${tag}
"""
}
}def call(Map config) {
def image = config.imageName
pipeline {
agent any
environment {
TAG = "${BUILD_NUMBER}"
}
stages {
stage('Checkout') {
steps {
gitCheckout(config.repoUrl)
}
}
stage('Build Docker Image') {
steps {
sh "docker build -t ${image}:${TAG} ."
}
}
stage('Push Image') {
steps {
dockerPush(image, TAG, config.dockerCred)
}
}
stage('Deploy') {
steps {
echo "Deploying ${image}:${TAG}"
}
}
}
}
}@Library('mind-lib') _
ciPipeline(
repoUrl: 'https://github.com/jadalaramani/jenkins-shared-library.git',
imageName: 'ramanijadalla/mindcircuit17d',
dockerCred: 'dockerhub-cred'
)- Jenkins pulls source code
- Builds Docker image
- Tags image with build number
- Logs into DockerHub
- Pushes image
- Deploy stage runs
Install Git:
sudo yum install git -yUse:
usernamePassword (NOT string)
Avoid:
IMAGE_NAME = config.imageNameUse:
def image = config.imageName- Jenkins Shared Library structure
- Reusable pipeline design
- Secure credential handling
- Docker CI/CD workflow
Jadala Ramani DevOps Engineer