From 528a24beb0541e504277a27dd9977028178699f4 Mon Sep 17 00:00:00 2001 From: darnodo Date: Sun, 7 Dec 2025 20:54:26 +0100 Subject: [PATCH] ci: improve image detection logic in build workflow Updates the `build-images` workflow to robustly detect and validate changes to image directories. Changes: - Implements strict validation to ensure detected image directories exist and contain a `Dockerfile` before adding them to the build matrix. - Normalizes logic for both manual inputs and automatic git-diff detection using a temporary file (`valid_images.txt`) to collect targets. - Adds specific checks to warn if a manually specified image is missing or invalid. - Prevents the build job from failing or running unnecessarily if an empty image name is passed to the matrix by adding an early exit condition. --- .gitea/workflows/build-images.yml | 56 +++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/.gitea/workflows/build-images.yml b/.gitea/workflows/build-images.yml index da9147c..9a3df85 100644 --- a/.gitea/workflows/build-images.yml +++ b/.gitea/workflows/build-images.yml @@ -37,30 +37,45 @@ jobs: - name: Detect changed images id: changes run: | + # Prepare output file for valid images + > valid_images.txt + if [ -n "${{ inputs.image }}" ]; then - # Clean input to remove potential whitespace + # Manual trigger with input IMAGE=$(echo "${{ inputs.image }}" | xargs) if [ -n "$IMAGE" ]; then - echo "matrix=[\"$IMAGE\"]" >> $GITHUB_OUTPUT - echo "has_changes=true" >> $GITHUB_OUTPUT - else - echo "has_changes=false" >> $GITHUB_OUTPUT - echo "matrix=[]" >> $GITHUB_OUTPUT + if [ -d "images/$IMAGE" ] && [ -f "images/$IMAGE/Dockerfile" ]; then + echo "$IMAGE" >> valid_images.txt + else + echo "Warning: Image '$IMAGE' not found or missing Dockerfile at images/$IMAGE/Dockerfile" + fi fi else - # Find changed directories in images/ - # We look for changes in images/ subdirectory between the previous commit and current commit - CHANGED=$(git diff --name-only HEAD~1 HEAD -- images/ 2>/dev/null | cut -d'/' -f2 | sort -u | grep -v '^$' || true) + # Automatic detection via git diff + CHANGED_FILES=$(git diff --name-only HEAD~1 HEAD -- images/ 2>/dev/null || true) - if [ -z "$CHANGED" ]; then - echo "has_changes=false" >> $GITHUB_OUTPUT - echo "matrix=[]" >> $GITHUB_OUTPUT - else - # Convert to JSON array, filtering out empty strings - JSON=$(echo "$CHANGED" | jq -R -s -c 'split("\n") | map(select(length > 0))') - echo "matrix=$JSON" >> $GITHUB_OUTPUT - echo "has_changes=true" >> $GITHUB_OUTPUT - fi + # Extract potential image directories (first level subdirs in images/) + # cut -d'/' -f2 takes the second component: images/COMPONENT/... + CANDIDATES=$(echo "$CHANGED_FILES" | cut -d'/' -f2 | sort -u | grep -v '^$') + + for CANDIDATE in $CANDIDATES; do + # Verify it's a directory with a Dockerfile + if [ -d "images/$CANDIDATE" ] && [ -f "images/$CANDIDATE/Dockerfile" ]; then + echo "$CANDIDATE" >> valid_images.txt + fi + done + fi + + if [ ! -s valid_images.txt ]; then + echo "No valid images detected." + echo "has_changes=false" >> $GITHUB_OUTPUT + echo "matrix=[]" >> $GITHUB_OUTPUT + else + # Convert list to JSON array using jq + JSON=$(cat valid_images.txt | jq -R -s -c 'split("\n") | map(select(length > 0))') + echo "Detected images: $JSON" + echo "matrix=$JSON" >> $GITHUB_OUTPUT + echo "has_changes=true" >> $GITHUB_OUTPUT fi - name: Show detected changes @@ -99,6 +114,11 @@ jobs: IMAGE_NAME: ${{ env.REGISTRY }}/damien/${{ matrix.image }} IMAGE_PATH: images/${{ matrix.image }} run: | + if [ -z "${{ matrix.image }}" ]; then + echo "Skipping empty image name" + exit 0 + fi + echo "Building $IMAGE_NAME:latest from $IMAGE_PATH ..." if [ ! -d "$IMAGE_PATH" ]; then