From 6a16c765572c18cad512926e766dbd003ebbe247 Mon Sep 17 00:00:00 2001 From: ryan77627 Date: Wed, 10 Jun 2026 17:44:30 -0400 Subject: Update Jenkinsfile --- Jenkinsfile | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index a458d2a..f7f3f28 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,10 +1,51 @@ pipeline { agent any + environment { + REGISTRY = 'registry.example.com/docker-images' + } + stages { - stage('Verify') { + stage('Detect changed images') { + steps { + script { + def base = env.CHANGE_TARGET ? "origin/${env.CHANGE_TARGET}" : "HEAD~1" + def changed = sh( + script: "git diff --name-only ${base} HEAD | cut -d/ -f1 | sort -u", + returnStdout: true + ).trim().split('\n') + + // keep only dirs containing a Dockerfile + env.IMAGES_TO_BUILD = changed.findAll { + fileExists("${it}/Dockerfile") + }.join(',') + } + } + } + + stage('Build & push') { + when { expression { env.IMAGES_TO_BUILD } } steps { - echo 'Jenkins is successfully executing the Pipeline!' + script { + def shortSha = env.GIT_COMMIT.take(8) + env.IMAGES_TO_BUILD.split(',').each { img -> + def tag = "${REGISTRY}/${img}" + sh """ + docker build \ + --label org.opencontainers.image.source=${env.GIT_URL} \ + --label org.opencontainers.image.revision=${env.GIT_COMMIT} \ + -t ${tag}:${shortSha} \ + ${img}/ + """ + if (env.BRANCH_NAME == 'main') { + sh """ + docker tag ${tag}:${shortSha} ${tag}:latest + docker push ${tag}:${shortSha} + docker push ${tag}:latest + """ + } + } + } } } } -- cgit v1.3