diff options
| author | ryan77627 <ryan@rschanz.org> | 2026-06-10 17:44:30 -0400 |
|---|---|---|
| committer | ryan77627 <ryan@rschanz.org> | 2026-06-10 17:44:30 -0400 |
| commit | 6a16c765572c18cad512926e766dbd003ebbe247 (patch) | |
| tree | d5e22b652f5ce47a96b05333e0239c5904510b78 | |
| parent | 2d943efbcaf4817cc6e22dc1de29c3facd05dd93 (diff) | |
| -rw-r--r-- | Jenkinsfile | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/Jenkinsfile b/Jenkinsfile index a458d2a..f7f3f28 100644 --- a/Jenkinsfile +++ b/Jenkinsfile | |||
| @@ -1,10 +1,51 @@ | |||
| 1 | pipeline { | 1 | pipeline { |
| 2 | agent any | 2 | agent any |
| 3 | 3 | ||
| 4 | environment { | ||
| 5 | REGISTRY = 'registry.example.com/docker-images' | ||
| 6 | } | ||
| 7 | |||
| 4 | stages { | 8 | stages { |
| 5 | stage('Verify') { | 9 | stage('Detect changed images') { |
| 10 | steps { | ||
| 11 | script { | ||
| 12 | def base = env.CHANGE_TARGET ? "origin/${env.CHANGE_TARGET}" : "HEAD~1" | ||
| 13 | def changed = sh( | ||
| 14 | script: "git diff --name-only ${base} HEAD | cut -d/ -f1 | sort -u", | ||
| 15 | returnStdout: true | ||
| 16 | ).trim().split('\n') | ||
| 17 | |||
| 18 | // keep only dirs containing a Dockerfile | ||
| 19 | env.IMAGES_TO_BUILD = changed.findAll { | ||
| 20 | fileExists("${it}/Dockerfile") | ||
| 21 | }.join(',') | ||
| 22 | } | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | stage('Build & push') { | ||
| 27 | when { expression { env.IMAGES_TO_BUILD } } | ||
| 6 | steps { | 28 | steps { |
| 7 | echo 'Jenkins is successfully executing the Pipeline!' | 29 | script { |
| 30 | def shortSha = env.GIT_COMMIT.take(8) | ||
| 31 | env.IMAGES_TO_BUILD.split(',').each { img -> | ||
| 32 | def tag = "${REGISTRY}/${img}" | ||
| 33 | sh """ | ||
| 34 | docker build \ | ||
| 35 | --label org.opencontainers.image.source=${env.GIT_URL} \ | ||
| 36 | --label org.opencontainers.image.revision=${env.GIT_COMMIT} \ | ||
| 37 | -t ${tag}:${shortSha} \ | ||
| 38 | ${img}/ | ||
| 39 | """ | ||
| 40 | if (env.BRANCH_NAME == 'main') { | ||
| 41 | sh """ | ||
| 42 | docker tag ${tag}:${shortSha} ${tag}:latest | ||
| 43 | docker push ${tag}:${shortSha} | ||
| 44 | docker push ${tag}:latest | ||
| 45 | """ | ||
| 46 | } | ||
| 47 | } | ||
| 48 | } | ||
| 8 | } | 49 | } |
| 9 | } | 50 | } |
| 10 | } | 51 | } |
