From 9e9006439b4e113e0190f0b3438cabb57ee515e7 Mon Sep 17 00:00:00 2001 From: darnodo Date: Sun, 7 Dec 2025 21:20:07 +0100 Subject: [PATCH] ci: refine image build matrix generation logic and tools Updates the build-images workflow to improve reliability and reduce dependency on external tools like `jq`. - Replaced `jq` with `python3` for generating the JSON matrix, removing the need to install a separate binary and allowing for more standard standard library usage. - Switched from a shell glob loop (`for DIR in images/*`) to `find` for more robust directory discovery when detecting manual triggers. - Added explanatory comments regarding fallback behavior for push events. - Added debug logging for the generated JSON output. --- .gitea/workflows/build-images.yml | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/.gitea/workflows/build-images.yml b/.gitea/workflows/build-images.yml index 08777e9..619b861 100644 --- a/.gitea/workflows/build-images.yml +++ b/.gitea/workflows/build-images.yml @@ -6,7 +6,7 @@ on: paths: - 'images/**' workflow_dispatch: - # No inputs needed as we build all images on manual trigger + # Manual trigger builds all images env: REGISTRY: gitea.arnodo.fr @@ -24,7 +24,7 @@ jobs: has_changes: ${{ steps.changes.outputs.has_changes }} steps: - name: Install dependencies - run: apk add --no-cache git jq + run: apk add --no-cache git python3 - name: Checkout repository run: | @@ -40,10 +40,9 @@ jobs: echo "DEBUG: Manual trigger detected. Scanning all images..." if [ -d "images" ]; then - # Loop through all items in images/ directory - for DIR in images/*; do - # Check if it's a directory and has a Dockerfile - if [ -d "$DIR" ] && [ -f "$DIR/Dockerfile" ]; then + # Use find for reliable directory listing + find images -mindepth 1 -maxdepth 1 -type d | while read DIR; do + if [ -f "$DIR/Dockerfile" ]; then BASENAME=$(basename "$DIR") echo "DEBUG: Found image: $BASENAME" echo "$BASENAME" >> valid_images.txt @@ -57,6 +56,7 @@ jobs: echo "DEBUG: Push event detected. Analyzing git diff..." CHANGED_FILES=$(git diff --name-only HEAD~1 HEAD -- images/ 2>/dev/null || true) + # Extract unique directories CANDIDATES=$(echo "$CHANGED_FILES" | cut -d'/' -f2 | sort -u | grep -v '^$') for CANDIDATE in $CANDIDATES; do @@ -65,6 +65,12 @@ jobs: echo "$CANDIDATE" >> valid_images.txt fi done + + # Fallback: If push event but no valid images found (e.g. change in root file but paths filter matched?), + # we generally skip. But if the user wants "Simple: No diff -> Build All", we could enable it. + # However, typically for push, we only want changes. + # The user's comment "If no modification is detected ... build all" likely referred to the manual trigger context. + # So we keep push strictly to changes. fi if [ ! -s valid_images.txt ]; then @@ -72,9 +78,10 @@ jobs: echo "has_changes=false" >> $GITHUB_OUTPUT echo "image_list=[]" >> $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" + # Use Python for reliable JSON generation + JSON=$(cat valid_images.txt | python3 -c 'import json, sys; lines = [l.strip() for l in sys.stdin if l.strip()]; print(json.dumps(lines))') + + echo "DEBUG: Generated JSON: >$JSON<" echo "image_list=$JSON" >> $GITHUB_OUTPUT echo "has_changes=true" >> $GITHUB_OUTPUT fi