From f32b3c973cb19331af93bfefeedcc335da33b087 Mon Sep 17 00:00:00 2001
From: Griefed <griefed@griefed.de>
Date: Sun, 30 Jan 2022 09:53:13 +0100
Subject: [PATCH] ci: Rework package versions job

---
 .gitlab-ci.yml | 95 +++++++++++++++++++++++++++++++++-----------------
 1 file changed, 63 insertions(+), 32 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 5c20b2c..e5c3e2a 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -24,6 +24,7 @@ test docker:
   script:
     - echo "**** Build amd64 ****"
     - docker build
+      --no-cache
       --tag "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:amd64-develop"
       --tag "ghcr.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:amd64-develop"
       --build-arg FOCAL_ARCH=amd64
@@ -36,6 +37,7 @@ test docker:
     - docker push "ghcr.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:amd64-develop"
     - echo "**** Build armhf ****"
     - docker build
+      --no-cache
       --tag "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:armhf-develop"
       --tag "ghcr.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:armhf-develop"
       --build-arg FOCAL_ARCH=armhf
@@ -48,6 +50,7 @@ test docker:
     - docker push "ghcr.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:armhf-develop"
     - echo "**** Build arm64 ****"
     - docker build
+      --no-cache
       --tag "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:arm64-develop"
       --tag "ghcr.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:arm64-develop"
       --build-arg FOCAL_ARCH=arm64
@@ -95,6 +98,7 @@ build:
   script:
     - echo "**** Build amd64 ****"
     - docker build
+      --no-cache
       --tag "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:amd64-latest"
       --tag "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:amd64-$CI_COMMIT_TAG"
       --tag "ghcr.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:amd64-latest"
@@ -111,6 +115,7 @@ build:
     - docker push "ghcr.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:amd64-$CI_COMMIT_TAG"
     - echo "**** Build armhf ****"
     - docker build
+      --no-cache
       --tag "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:armhf-latest"
       --tag "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:armhf-$CI_COMMIT_TAG"
       --tag "ghcr.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:armhf-latest"
@@ -127,6 +132,7 @@ build:
     - docker push "ghcr.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:armhf-$CI_COMMIT_TAG"
     - echo "**** Build arm64 ****"
     - docker build
+      --no-cache
       --tag "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:arm64-latest"
       --tag "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:arm64-$CI_COMMIT_TAG"
       --tag "ghcr.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:arm64-latest"
@@ -167,6 +173,7 @@ Check Packages:on-schedule:
     - schedules
   before_script:
     - |-
+      echo "Preparing package versions comparison."
       # Check and, if necessary, update git user and mail
       if [[ "$(git config --list | grep user.name)" != "user.name=$GIT_USER" ]];then
         git config --global user.name $GIT_USER
@@ -177,63 +184,87 @@ Check Packages:on-schedule:
       
       # Clean system of potentially interrupting images
       docker image rm -f $DOCKERHUB_USER/$DOCKERHUB_REPO:latest
-      docker image rm -f localbuild
+      docker image rm -f $DOCKERHUB_REPO
       rm -rf /tmp/$CI_PROJECT_PATH
-      rm -f /tmp/package_versions*
       mkdir -p /tmp/$CI_PROJECT_PATH
+      echo "Preparations complete."
   script:
     - |-
+      echo "Comparing package versions."
       # Clone the repository
       git clone $CI_PROJECT_URL.git /tmp/$CI_PROJECT_PATH && \
       cd /tmp/$CI_PROJECT_PATH && \
       
-      # Build local image for new package versions list
-      docker build --tag localbuild . && \
+      if [ ! -s "package-versions.txt" ];then
       
-      # Get packages from newly build local image
-      docker run --rm --entrypoint /bin/sh -v /tmp:/tmp localbuild -c '\
-        apt list -qq --installed > /tmp/package_versions_new.txt && \
-        sort -o /tmp/package_versions_new.txt  /tmp/package_versions_new.txt && \
-        chmod 777 /tmp/package_versions_new.txt' && \
-      
-      # Gather package information from latest build
-      docker run --rm --entrypoint /bin/sh -v /tmp:/tmp $DOCKERHUB_USER/$DOCKERHUB_REPO:latest -c '\
-        apt list -qq --installed > /tmp/package_versions_old.txt && \
-        sort -o /tmp/package_versions_old.txt  /tmp/package_versions_old.txt && \
-        chmod 777 /tmp/package_versions_old.txt' && \
-
-      # Get checksum of old packages
-      OLD_CHECKSUM=$(md5sum /tmp/package_versions_old.txt | cut -f1 -d" ") && \
-      
-      # Get checksum of new packages
-      NEW_CHECKSUM=$(md5sum /tmp/package_versions_new.txt | cut -f1 -d" ") && \
-      
-      # If new checksum is not the same as old checksum, we have new versions
-      if [ "${OLD_CHECKSUM}" != "${NEW_CHECKSUM}" ]; then
+        # Gather package information from latest build
+        docker run --rm --entrypoint /bin/sh -v /tmp/$CI_PROJECT_PATH:/tmp $DOCKERHUB_USER/$DOCKERHUB_REPO:latest -c '\
+          apt list -qq --installed > /tmp/package_versions.txt && \
+          sort -o /tmp/package_versions.txt  /tmp/package_versions.txt && \
+          chmod 777 /tmp/package_versions.txt' && \
       
         # Checkout our branch
         git --git-dir /tmp/$CI_PROJECT_PATH.git checkout -f master && \
       
-        # Copy the new package versions list to repository
-        cp -f /tmp/package_versions_new.txt /tmp/$CI_PROJECT_PATH/package_versions.txt && \
-        
         wait && \
       
         # Add and commit new file to repository
         git add package_versions.txt && \
-        git commit -m 'build: Update installed packages.' && \
+        git commit -m 'chore: Add list of package versions.' && \
       
         # Push the changes to the remote
         git push "https://$GIT_USER:$GITLAB_TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH.git" --all && \
       
         # Nice
-        echo "Packages updated."
-      else
-        echo "No package updates available."
+        echo "package_versions.txt added."        
+      
+      elif [ -s "package-versions.txt" ];then
+      
+        # Build local image for new package versions list
+        docker build --no-cache --tag $DOCKERHUB_REPO . && \
+        
+        # Get packages from newly build local image
+        docker run --rm --entrypoint /bin/sh -v /tmp/$CI_PROJECT_PATH:/tmp $DOCKERHUB_REPO -c '\
+          apt list -qq --installed > /tmp/package_versions_new.txt && \
+          sort -o /tmp/package_versions_new.txt  /tmp/package_versions_new.txt && \
+          chmod 777 /tmp/package_versions_new.txt' && \
+      
+        # Get checksum of old packages
+        OLD_CHECKSUM=$(md5sum package_versions.txt | cut -f1 -d" ") && \
+        
+        # Get checksum of new packages
+        NEW_CHECKSUM=$(md5sum package_versions_new.txt | cut -f1 -d" ") && \
+      
+        # If new checksum is not the same as old checksum, we have new versions
+        if [ "${OLD_CHECKSUM}" != "${NEW_CHECKSUM}" ]; then
+        
+          # Checkout our branch
+          git --git-dir /tmp/$CI_PROJECT_PATH.git checkout -f master && \
+        
+          # Copy the new package versions list to repository
+          mv -f package_versions_new.txt package_versions.txt && \
+        
+          wait && \
+        
+          # Add and commit new file to repository
+          git add package_versions.txt && \
+          git commit -m 'build: Update installed packages.' && \
+        
+          # Push the changes to the remote
+          git push "https://$GIT_USER:$GITLAB_TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH.git" --all && \
+        
+          # Nice
+          echo "Packages updated."
+        else
+          echo "No package updates available."
+        fi
+      
       fi
+      echo "Comparison complete."
   after_script:
     - |-
+      echo "Cleaning up."
       docker image rm -f $DOCKERHUB_USER/$DOCKERHUB_REPO:latest
-      docker image rm -f localbuild
+      docker image rm -f $DOCKERHUB_REPO
       rm -rf /tmp/$CI_PROJECT_PATH
       echo "Done."
-- 
GitLab