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