Skip to content

Commit

Permalink
Update Jenkins_Docker
Browse files Browse the repository at this point in the history
  • Loading branch information
em-tpt-bbandi authored Jun 21, 2024
1 parent 6052f83 commit 2b29083
Showing 1 changed file with 40 additions and 30 deletions.
70 changes: 40 additions & 30 deletions Jenkins_Docker
Original file line number Diff line number Diff line change
@@ -1,59 +1,69 @@
pipeline {
agent any

parameters {
string(name: 'DOCKER_HUB_CREDENTIALS_ID', defaultValue: '', description: 'Jenkins credentials ID for Docker Hub')
string(name: 'DOCKER_IMAGE_NAME', defaultValue: '', description: 'Docker image name (e.g., username/repository)')
string(name: 'DOCKER_IMAGE_TAG', defaultValue: 'latest', description: 'Docker image tag')
string(name: 'BRANCH_NAME', defaultValue: 'main', description: 'The branch to build')
string(name: 'DOCKERHUB_CREDENTIALS', defaultValue: 'dockerhub-credentials', description: 'Jenkins credentials ID for Docker Hub')
string(name: 'DOCKERHUB_REPO', defaultValue: 'your-dockerhub-repo', description: 'Docker Hub repository name')
}

environment {
DOCKER_HUB_CREDENTIALS_ID = "${params.DOCKER_HUB_CREDENTIALS_ID}"
DOCKER_IMAGE_NAME = "${params.DOCKER_IMAGE_NAME}"
DOCKER_IMAGE_TAG = "${params.DOCKER_IMAGE_TAG}"
DOCKERHUB_CREDENTIALS = "${params.DOCKERHUB_CREDENTIALS}"
DOCKERHUB_REPO = "${params.DOCKERHUB_REPO}"
BRANCH_NAME = "${params.BRANCH_NAME}"
}

stages {
stage('Checkout') {
steps {
// Checkout the repository
checkout scm
script {
// Checkout the specified branch
git branch: "${BRANCH_NAME}", url: 'https://github.com/your-repo.git'
}
}
}

stage('Build Docker Image') {
steps {
script {
// Build the Docker image
def imageName = "${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}"
sh "docker build -t ${imageName} ."
// Build Docker image with the tag as the branch name
sh "docker build -t ${DOCKERHUB_REPO}:${BRANCH_NAME} ."
}
}
}
stage('Push Docker Image') {

stage('Login to Docker Hub') {
steps {
script {
// Login to Docker Hub
withCredentials([usernamePassword(credentialsId: DOCKER_HUB_CREDENTIALS_ID, usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {
sh 'echo $DOCKER_PASSWORD | docker login -u $DOCKER_USERNAME --password-stdin'
withCredentials([usernamePassword(credentialsId: DOCKERHUB_CREDENTIALS, usernameVariable: 'DOCKERHUB_USERNAME', passwordVariable: 'DOCKERHUB_PASSWORD')]) {
sh "echo $DOCKERHUB_PASSWORD | docker login -u $DOCKERHUB_USERNAME --password-stdin"
}

// Push the Docker image
def imageName = "${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}"
sh "docker push ${imageName}"

// Logout from Docker Hub
sh 'docker logout'
}
}
}

stage('Push Docker Image') {
steps {
script {
// Push the Docker image to Docker Hub
sh "docker push ${DOCKERHUB_REPO}:${BRANCH_NAME}"
}
}
}
}

post {
always {
// Clean up the Docker environment
sh 'docker system prune -f'
script {
// Clean up Docker images to save space
sh "docker rmi ${DOCKERHUB_REPO}:${BRANCH_NAME} || true"
}
}
success {
echo 'Docker image has been successfully pushed to Docker Hub!'
}
failure {
echo 'Failed to push Docker image to Docker Hub.'
}
}
}

0 comments on commit 2b29083

Please sign in to comment.