Skip to content

Commit

Permalink
Add CI workflow to check for missing image version bumps
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnesbitt committed Jul 9, 2024
1 parent 3062ad1 commit aa9e111
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
49 changes: 49 additions & 0 deletions .github/scripts/check_docker_image_versions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env bash

echoerr() { printf "%s\n" "$*" >&2; }

WORKFLOW_FILE=./.github/workflows/custom_docker_builds.yml

# Set to 1 if any of the checks fail
FAILED=0

# What gets fed into the $image var is defined at the end of the loop
while read image; do
DOCKER_IMAGE_DIR=$(echo $image | jq '."docker-image"' -r | sed 's/^\.\///')
IMAGE_TAG=$(echo $image | jq '."image-tags"' -r)

# Skip if the directory was not modified at all
if ! git diff --name-only | grep $DOCKER_IMAGE_DIR > /dev/null; then
continue
fi

# Is the found tag in the added lines of the diff? If so, don't error just yet.
# If not, error, as that means the tag we're looking at is the old tag
if ! git diff -- $WORKFLOW_FILE | grep "^+[^+].\+$IMAGE_TAG" > /dev/null; then
FAILED=1
echoerr "ERROR: Directory '$DOCKER_IMAGE_DIR' modified, but image tag $IMAGE_TAG not incremented!"
continue
fi

# Find the old tag from the diff and search for it. If it exists, error, as that means it hasn't been bumped
BASE_IMAGE_TAG=$(echo $IMAGE_TAG | cut -d ":" -f1)
BASE_IMAGE_TAG_PATTERN=$(echo $BASE_IMAGE_TAG | sed 's/[.\/]/\\&/g')
OLD_TAG=$(git diff -- $WORKFLOW_FILE | sed -nr s"/^-[^-].+($BASE_IMAGE_TAG_PATTERN)/\1/p")

NEW_TAG_VERSION=$(echo $IMAGE_TAG | cut -d ":" -f2)
OLD_TAG_VERSION=$(echo $OLD_TAG | cut -d ":" -f2)

# Search for this old tag. If found error, as we should only find the new tag
if git grep $OLD_TAG > /dev/null; then
FAILED=1
echoerr "ERROR: Image $BASE_IMAGE_TAG incremented to $NEW_TAG_VERSION, found remaining occurances of $OLD_TAG_VERSION!"
fi

# This is where the input to the while loop variable $image comes in. This is called a "here string" and
# circumvents the issue with subshells setting global variables.
# https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Here-Strings
done <<< $(cat $WORKFLOW_FILE | yq ".jobs.build.strategy.matrix.include" -o json | jq -c ".[]")

if [ "$FAILED" -eq "1" ]; then
exit 1
fi
15 changes: 15 additions & 0 deletions .github/workflows/check_docker_image_versions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Check Docker Image Versions

on:
pull_request:

jobs:
check-image:
if: ${{ !contains(github.event.pull_request.labels.*.name, 'no-image-bump') }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Check for modified directories that need an image bump
run: ./.github/scripts/check_docker_image_versions.sh

0 comments on commit aa9e111

Please sign in to comment.