Update Jenkinsfile

Signed-off-by: Mihailo <mmarcetic@griddynamcis.com>
This commit is contained in:
MMarceticGrid 2025-03-06 16:04:20 +01:00 committed by Mihailo
parent 684e04e703
commit 0dce1f7d2b

16
Jenkinsfile vendored
View file

@ -5,7 +5,6 @@ pipeline {
// Define environment variables // Define environment variables
DOCKER_REGISTRY = "docker.io" DOCKER_REGISTRY = "docker.io"
DOCKER_IMAGE = "mmarcetic/main" DOCKER_IMAGE = "mmarcetic/main"
DOCKER_CREDENTIALS = "Docker_hub"
} }
stages { stages {
@ -20,7 +19,8 @@ pipeline {
steps { steps {
script { script {
// Build the Docker image // Build the Docker image
sh 'docker build -t ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest .' def gitCommit = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim()
sh "docker build -t ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${gitCommit} ."
} }
} }
} }
@ -28,10 +28,10 @@ pipeline {
stage('Push Docker Image') { stage('Push Docker Image') {
steps { steps {
script { script {
//Login to the Docker repository def gitCommit = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim()
docker.withRegistry('https://${DOCKER_REGISTRY}', "${DOCKER_CREDENTIALS}") { withCredentials([usernamePassword(credentialsId: "docker-login", usernameVariable: "DOCKER_USER", passwordVariable: "DOCKER_PASSWORD")]) {
// Push the Docker image to the registry sh "docker login -u ${DOCKER_USER} -p ${DOCKER_PASSWORD}"
sh 'docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest' sh "docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${gitCommit}"
} }
} }
} }
@ -39,10 +39,6 @@ pipeline {
} }
post { post {
always {
// Clean up Docker images after the job is done
sh 'docker rmi ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest || true'
}
success { success {
echo 'Docker image built and pushed successfully.' echo 'Docker image built and pushed successfully.'
} }