From cfe6910367f1e5b488ef18b214bcc0545d1a9e7c Mon Sep 17 00:00:00 2001 From: Griefed <griefed@griefed.de> Date: Sun, 30 Jan 2022 09:54:08 +0100 Subject: [PATCH] ci: Rework package versions job --- .gitlab-ci.yml | 91 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 33 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 4dcbba1..8a50793 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -70,6 +70,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 @@ -80,63 +81,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 '\ - apk info -v > /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 '\ - apk info -v > /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 '\ + apk info -v > /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 && \ + git --git-dir /tmp/$CI_PROJECT_PATH.git checkout -f main && \ - # 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 '\ + apk info -v > /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 main && \ + + # 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