From fc701ca6e3520d0d96c4606d5634619a5197aced Mon Sep 17 00:00:00 2001
From: Christian Winter <c.winter@kit.edu>
Date: Mon, 30 Oct 2023 11:02:12 +0000
Subject: [PATCH] create pipeline steps for each container, that only execute
 if the relevant Dockerfile was changed

---
 .gitlab-ci.yml | 65 ++++++++++++++++++++++++++++++++++----------------
 1 file changed, 45 insertions(+), 20 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index afd424f..e87184c 100755
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1,27 +1,51 @@
 stages:
-  - build_and_push
+  - 🏗️ build_and_push
   - create_jupytermachine_version
 
 variables:
-  DOCKER_REGISTRY: "docker.io"
-  IMAGE_PREFIX: "jeppelt"  # Change this to your Docker Hub username
-  JUPYTERMACHINE_DOCKERFILE: "basic_jupytermachine/Dockerfile"
+  DOCKER_HUB_USER: "jeppelt"
 
-build_and_push:
-  stage: build_and_push
+.common_build:
   image: docker:latest
+  stage: 🏗️ build_and_push
+  rules:
+    - changes: 
+      - $IMAGE_NAME/Dockerfile
+  before_script:
+    - docker login -u $DOCKER_HUB_USER -p $DOCKER_TOKEN
   script:
-    - |
-      export IMAGES=( "basis" "tp" "python311"  "basis_jupytermachine") # "tp_herwig") # "tp_geant" "cuda_basis"
-      docker login -u jeppelt -p $DOCKER_TOKEN;
-      for image in "${IMAGES[@]}"; do
-        echo "$image";
-        cd "$image";
-        docker build -t "$IMAGE_PREFIX/etp_${image}" . --no-cache;
-        docker push "$IMAGE_PREFIX/etp_${image}";
-        cd ../;
-      done
+      - docker build -t "$DOCKER_HUB_USER/etp_$IMAGE_NAME" $IMAGE_NAME/Dockerfile --no-cache;
+      - docker push "$DOCKER_HUB_USER/etp_$IMAGE_NAME";
+
+basis:
+  variables:
+    IMAGE_NAME: "basis"
+  extends:
+    - .common_build
 
+tp:
+  variables:
+    IMAGE_NAME: "tp"
+  extends:
+    - .common_build
+
+python311:
+  variables:
+    IMAGE_NAME: "python311"
+  extends:
+    - .common_build
+
+basis_jupytermachine:
+  variables:
+    IMAGE_NAME: "basis_jupytermachine"
+  extends:
+    - .common_build
+
+tp_herwig:
+  variables:
+    IMAGE_NAME: "tp_herwig"
+  extends:
+    - .common_build
 
 create_jupytermachine_version:
   stage: create_jupytermachine_version
@@ -29,20 +53,21 @@ create_jupytermachine_version:
   script:
     - |
       IMAGES=( "basis" "tp" "python311") # "tp_herwig") #  "tp_geant" "cuda_basis"
-      docker login -u jeppelt -p $DOCKER_TOKEN
+      docker login -u $DOCKER_HUB_USER -p $DOCKER_TOKEN
       cd basis_jupytermachine
       for image in "${IMAGES[@]}"; do
+
         # Read the existing base image from the Dockerfile
         OLD_BASE_IMAGE=$(grep -E '^\s*FROM\s+.*' Dockerfile | awk '{print $2}');
         echo $OLD_BASE_IMAGE;
         # Replace "basic_jupytermachine" with the new base image name
-        NEW_BASE_IMAGE=$IMAGE_PREFIX/etp_$image;
+        NEW_BASE_IMAGE=$DOCKER_HUB_USER/etp_$image;
         echo $NEW_BASE_IMAGE;
         sed -i "s#$OLD_BASE_IMAGE#$NEW_BASE_IMAGE#" "Dockerfile"
         cat Dockerfile;
         # Build the second version of the container
-        docker build -t "$IMAGE_PREFIX/jupytermachine_${image}" -f "Dockerfile" . --no-cache;
-        docker push "$IMAGE_PREFIX/jupytermachine_${image}";
+        docker build -t "$DOCKER_HUB_USER/jupytermachine_${image}" -f "Dockerfile" . --no-cache;
+        docker push "$DOCKER_HUB_USER/jupytermachine_${image}";
         # Revert the Dockerfile to its original state for other iterations
         sed -i "s#$NEW_BASE_IMAGE#$OLD_BASE_IMAGE#" "Dockerfile"
       done
-- 
GitLab