From 3458b1c8a0b9c66cf6bdb2dcb948bf303f38b5b8 Mon Sep 17 00:00:00 2001 From: darnodo Date: Sun, 7 Dec 2025 20:58:54 +0100 Subject: [PATCH] ci: refine image detection logic in build workflow Updated the image detection step in the `.gitea/workflows/build-images.yml` workflow to better handle manual inputs and automatic change detection. Changes: - Reorganized the logic to handle manual inputs first; if a manual input is present, it validates the image, sets the output matrix immediately, and exits, bypassing git diff checks. - Added explicit error handling and output setting for invalid or empty manual inputs to prevent workflow failure confusion. - Grouped the Git diff detection logic into a distinct block that runs only if no manual input is provided. - Added a debug echo statement to the build job to verify the current matrix image being built. --- .gitea/workflows/build-images.yml | 52 +++++++++++++++++++------------ 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/.gitea/workflows/build-images.yml b/.gitea/workflows/build-images.yml index 9a3df85..17ccf64 100644 --- a/.gitea/workflows/build-images.yml +++ b/.gitea/workflows/build-images.yml @@ -37,34 +37,44 @@ jobs: - name: Detect changed images id: changes run: | - # Prepare output file for valid images - > valid_images.txt - + # 1. Handle Manual Input if [ -n "${{ inputs.image }}" ]; then - # Manual trigger with input + # Clean input IMAGE=$(echo "${{ inputs.image }}" | xargs) if [ -n "$IMAGE" ]; then if [ -d "images/$IMAGE" ] && [ -f "images/$IMAGE/Dockerfile" ]; then - echo "$IMAGE" >> valid_images.txt + echo "Manual image detected: $IMAGE" + echo "matrix=[\"$IMAGE\"]" >> $GITHUB_OUTPUT + echo "has_changes=true" >> $GITHUB_OUTPUT + exit 0 else - echo "Warning: Image '$IMAGE' not found or missing Dockerfile at images/$IMAGE/Dockerfile" + echo "Error: Image '$IMAGE' not found or missing Dockerfile at images/$IMAGE/Dockerfile" + echo "has_changes=false" >> $GITHUB_OUTPUT + echo "matrix=[]" >> $GITHUB_OUTPUT + exit 0 fi + else + echo "Input image name was empty." + echo "has_changes=false" >> $GITHUB_OUTPUT + echo "matrix=[]" >> $GITHUB_OUTPUT + exit 0 fi - else - # Automatic detection via git diff - CHANGED_FILES=$(git diff --name-only HEAD~1 HEAD -- images/ 2>/dev/null || true) - - # 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 + + # 2. Handle Automatic Detection (Git Diff) + > valid_images.txt + + CHANGED_FILES=$(git diff --name-only HEAD~1 HEAD -- images/ 2>/dev/null || true) + + # Extract potential image directories (first level subdirs in images/) + 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 if [ ! -s valid_images.txt ]; then echo "No valid images detected." @@ -114,6 +124,8 @@ jobs: IMAGE_NAME: ${{ env.REGISTRY }}/damien/${{ matrix.image }} IMAGE_PATH: images/${{ matrix.image }} run: | + echo "Debug: Matrix Image is '${{ matrix.image }}'" + if [ -z "${{ matrix.image }}" ]; then echo "Skipping empty image name" exit 0