diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index fa5bc0fb56a4500690d83b95290bc8d0063b1ee6..9b349b5bfac2540fbdf867c1051ba151141b01a0 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -16,7 +16,7 @@ variables: SECRET_DETECTION_EXCLUDED_PATHS: "" services: - - name: griefed/gitlab-ci-cd:2.0.8 + - name: ghcr.io/griefed/gitlab-ci-cd:2.0.8 alias: docker workflow: @@ -444,3 +444,104 @@ Gradle Dependency-Checks: # expire_in: 1 week # rules: # - if: "$CI_SERVER_HOST =~ /git.griefed.de/" + +Check Packages:on-schedule: + only: + - 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 + fi + if [[ "$(git config --list | grep user.email)" != "user.email=$GIT_MAIL" ]];then + git config --global user.email $GIT_MAIL + fi + + # Clean system of potentially interrupting images + docker image rm -f $DOCKERHUB_USER/$DOCKERHUB_REPO:latest + docker image rm -f $DOCKERHUB_REPO + rm -rf /tmp/$CI_PROJECT_PATH + 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 && \ + + if [ ! -s "package_versions.txt" ];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 checkout -f $CI_DEFAULT_BRANCH && \ + + wait && \ + + # Add and commit new file to repository + git add package_versions.txt && \ + 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 "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 checkout -f $CI_DEFAULT_BRANCH && \ + + # 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 in Docker container.' && \ + + # 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 $DOCKERHUB_REPO + rm -rf /tmp/$CI_PROJECT_PATH + echo "Done."