From 439d0654a5ba3c3eb2721e1e272ea13c83fe3a07 Mon Sep 17 00:00:00 2001 From: iancumatei67 <127155074+iancumatei67@users.noreply.github.com> Date: Fri, 2 Feb 2024 14:04:55 +0200 Subject: [PATCH] Update Jenkinsfile for MR --- Jenkinsfile | 52 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 8069e80af..592619894 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -36,7 +36,7 @@ pipeline { } } - stage('Build Docker Image') { + stage('Build Docker Image (Main)') { when { branch 'main' } @@ -49,7 +49,8 @@ pipeline { } } } - stage('Push Docker Image') { + + stage('Push Docker Image (Main)') { when { branch 'main' } @@ -62,8 +63,47 @@ pipeline { } } } - - - } -} + stage('Build Docker Image (MR)') { + when { + expression { + // Execute this stage only for merge requests + return env.CHANGE_ID != null + } + } + steps { + script { + def gitCommitShort = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim() + appMR = docker.build("${DOCKER_REPO_MR}:${gitCommitShort}") + appMR.inside { + sh 'echo $(curl localhost:8080)' + } + } + } + } + + stage('Push Docker Image (MR)') { + when { + expression { + // Execute this stage only for merge requests + return env.CHANGE_ID != null + } + } + steps { + script { + def gitCommitShort = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim() + docker.withRegistry('https://registry.hub.docker.com', 'docker_hub_login') { + appMR.push("${DOCKER_REPO_MR}:${gitCommitShort}") + appMR.push("latest") + } + } + } + } + } + + post { + always { + cleanWs() // Clean workspace after the pipeline execution + } + } +}