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.
This commit is contained in:
darnodo
2025-12-07 21:20:07 +01:00
parent a6f7c9799f
commit 9e9006439b

View File

@@ -6,7 +6,7 @@ on:
paths: paths:
- 'images/**' - 'images/**'
workflow_dispatch: workflow_dispatch:
# No inputs needed as we build all images on manual trigger # Manual trigger builds all images
env: env:
REGISTRY: gitea.arnodo.fr REGISTRY: gitea.arnodo.fr
@@ -24,7 +24,7 @@ jobs:
has_changes: ${{ steps.changes.outputs.has_changes }} has_changes: ${{ steps.changes.outputs.has_changes }}
steps: steps:
- name: Install dependencies - name: Install dependencies
run: apk add --no-cache git jq run: apk add --no-cache git python3
- name: Checkout repository - name: Checkout repository
run: | run: |
@@ -40,10 +40,9 @@ jobs:
echo "DEBUG: Manual trigger detected. Scanning all images..." echo "DEBUG: Manual trigger detected. Scanning all images..."
if [ -d "images" ]; then if [ -d "images" ]; then
# Loop through all items in images/ directory # Use find for reliable directory listing
for DIR in images/*; do find images -mindepth 1 -maxdepth 1 -type d | while read DIR; do
# Check if it's a directory and has a Dockerfile if [ -f "$DIR/Dockerfile" ]; then
if [ -d "$DIR" ] && [ -f "$DIR/Dockerfile" ]; then
BASENAME=$(basename "$DIR") BASENAME=$(basename "$DIR")
echo "DEBUG: Found image: $BASENAME" echo "DEBUG: Found image: $BASENAME"
echo "$BASENAME" >> valid_images.txt echo "$BASENAME" >> valid_images.txt
@@ -57,6 +56,7 @@ jobs:
echo "DEBUG: Push event detected. Analyzing git diff..." echo "DEBUG: Push event detected. Analyzing git diff..."
CHANGED_FILES=$(git diff --name-only HEAD~1 HEAD -- images/ 2>/dev/null || true) 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 '^$') CANDIDATES=$(echo "$CHANGED_FILES" | cut -d'/' -f2 | sort -u | grep -v '^$')
for CANDIDATE in $CANDIDATES; do for CANDIDATE in $CANDIDATES; do
@@ -65,6 +65,12 @@ jobs:
echo "$CANDIDATE" >> valid_images.txt echo "$CANDIDATE" >> valid_images.txt
fi fi
done 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 fi
if [ ! -s valid_images.txt ]; then if [ ! -s valid_images.txt ]; then
@@ -72,9 +78,10 @@ jobs:
echo "has_changes=false" >> $GITHUB_OUTPUT echo "has_changes=false" >> $GITHUB_OUTPUT
echo "image_list=[]" >> $GITHUB_OUTPUT echo "image_list=[]" >> $GITHUB_OUTPUT
else else
# Convert list to JSON array using jq # Use Python for reliable JSON generation
JSON=$(cat valid_images.txt | jq -R -s -c 'split("\n") | map(select(length > 0))') 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 "Detected images: $JSON"
echo "DEBUG: Generated JSON: >$JSON<"
echo "image_list=$JSON" >> $GITHUB_OUTPUT echo "image_list=$JSON" >> $GITHUB_OUTPUT
echo "has_changes=true" >> $GITHUB_OUTPUT echo "has_changes=true" >> $GITHUB_OUTPUT
fi fi