Skip to content
Snippets Groups Projects
Verified Commit e6e9c59c authored by Griefed's avatar Griefed :joystick:
Browse files

Scheduled job for Docker container package updates should anyone need it

parent c0693aa9
No related branches found
No related tags found
No related merge requests found
...@@ -16,7 +16,7 @@ variables: ...@@ -16,7 +16,7 @@ variables:
SECRET_DETECTION_EXCLUDED_PATHS: "" SECRET_DETECTION_EXCLUDED_PATHS: ""
services: services:
- name: griefed/gitlab-ci-cd:2.0.8 - name: ghcr.io/griefed/gitlab-ci-cd:2.0.8
alias: docker alias: docker
workflow: workflow:
...@@ -444,3 +444,104 @@ Gradle Dependency-Checks: ...@@ -444,3 +444,104 @@ Gradle Dependency-Checks:
# expire_in: 1 week # expire_in: 1 week
# rules: # rules:
# - if: "$CI_SERVER_HOST =~ /git.griefed.de/" # - 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."
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment