Cizz22 1 year ago
parent 53dfb4f5f1
commit 62563442c3

17
Jenkinsfile vendored

@ -38,21 +38,21 @@ pipeline {
script { script {
// Build with commit hash tag // Build with commit hash tag
def commitHash = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim() def commitHash = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
sh ''' sh """
docker build -t ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:latest . docker build -t ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:latest .
docker tag ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:latest ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:${commitHash} docker tag ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:latest ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:${commitHash}
''' """
} }
} }
} }
stage('Push to Docker Hub') { stage('Push to Docker Hub') {
steps { steps {
sh ''' sh """
# Push both tags # Push both tags
docker push ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:${commitHash} docker push ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:${commitHash}
docker push ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:latest docker push ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:latest
''' """
} }
} }
} }
@ -65,17 +65,18 @@ pipeline {
// Clean up local images // Clean up local images
script { script {
try { try {
sh ''' sh """
docker rmi ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:${GIT_COMMIT_HASH} # Push both tags
docker rmi ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:${commitHash}
docker rmi ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:latest docker rmi ${DOCKER_HUB_USERNAME}/${IMAGE_NAME}:latest
''' """
} catch (err) { } catch (err) {
echo "Failed to clean up images: ${err}" echo "Failed to clean up images: ${err}"
} }
} }
} }
success { success {
echo "Successfully built and pushed Docker image with tags: latest and ${GIT_COMMIT_HASH}" echo "Successfully built and pushed Docker image with tags: latest and ${commitHash}"
} }
failure { failure {
echo 'Failed to build/push Docker image!' echo 'Failed to build/push Docker image!'

Loading…
Cancel
Save