summaryrefslogtreecommitdiff
path: root/Jenkinsfile
diff options
context:
space:
mode:
Diffstat (limited to 'Jenkinsfile')
-rw-r--r--Jenkinsfile45
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 @@
1pipeline { 1pipeline {
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 }