ci(workflows): enhance debug logging and safety in image detection

Refactor the 'Detect changed images' step in the build-images workflow to improve robustness and debuggability.

Changes include:
- Pass `inputs.image` via an environment variable (`INPUT_IMAGE`) to avoid potential injection issues or syntax errors in shell scripts.
- Add comprehensive `DEBUG` echo statements to trace the logic flow for both manual and automatic detection paths.
- Use `printf` instead of `echo` for writing to `$GITHUB_OUTPUT` to ensure safer handling of string outputs.
- Remove redundant comments to clean up the script block.
This commit is contained in:
darnodo
2025-12-07 21:02:52 +01:00
parent 3458b1c8a0
commit 44d9527c95

View File

@@ -36,16 +36,22 @@ jobs:
- name: Detect changed images
id: changes
env:
INPUT_IMAGE: ${{ inputs.image }}
run: |
# 1. Handle Manual Input
if [ -n "${{ inputs.image }}" ]; then
if [ -n "$INPUT_IMAGE" ]; then
# Clean input
IMAGE=$(echo "${{ inputs.image }}" | xargs)
IMAGE=$(echo "$INPUT_IMAGE" | xargs)
echo "DEBUG: Processing manual input image: '$IMAGE'"
if [ -n "$IMAGE" ]; then
if [ -d "images/$IMAGE" ] && [ -f "images/$IMAGE/Dockerfile" ]; then
echo "Manual image detected: $IMAGE"
echo "matrix=[\"$IMAGE\"]" >> $GITHUB_OUTPUT
echo "DEBUG: Image directory and Dockerfile exist."
# Use printf for safer output
printf "matrix=[\"%s\"]\n" "$IMAGE" >> $GITHUB_OUTPUT
echo "has_changes=true" >> $GITHUB_OUTPUT
echo "DEBUG: Set matrix=['$IMAGE']"
exit 0
else
echo "Error: Image '$IMAGE' not found or missing Dockerfile at images/$IMAGE/Dockerfile"
@@ -54,7 +60,7 @@ jobs:
exit 0
fi
else
echo "Input image name was empty."
echo "DEBUG: Input image name was empty after trimming."
echo "has_changes=false" >> $GITHUB_OUTPUT
echo "matrix=[]" >> $GITHUB_OUTPUT
exit 0
@@ -62,15 +68,14 @@ jobs:
fi
# 2. Handle Automatic Detection (Git Diff)
echo "DEBUG: Running git diff detection..."
> 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