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.
151 lines
5.1 KiB
YAML
151 lines
5.1 KiB
YAML
name: Build and Push Docker Images
|
|
|
|
on:
|
|
push:
|
|
branches: [main]
|
|
paths:
|
|
- 'images/**'
|
|
workflow_dispatch:
|
|
inputs:
|
|
image:
|
|
description: 'Image to build (e.g., terraform-ci)'
|
|
required: false
|
|
|
|
env:
|
|
REGISTRY: gitea.arnodo.fr
|
|
|
|
jobs:
|
|
# ============================================================================
|
|
# Job 1 : Detect modified images
|
|
# ============================================================================
|
|
detect-changes:
|
|
runs-on: docker
|
|
container:
|
|
image: alpine:3.20
|
|
outputs:
|
|
matrix: ${{ steps.changes.outputs.matrix }}
|
|
has_changes: ${{ steps.changes.outputs.has_changes }}
|
|
steps:
|
|
- name: Install dependencies
|
|
run: apk add --no-cache git jq
|
|
|
|
- name: Checkout repository
|
|
run: |
|
|
git clone --depth 2 https://gitea.arnodo.fr/${{ gitea.repository }}.git .
|
|
git checkout ${{ gitea.sha }}
|
|
|
|
- name: Detect changed images
|
|
id: changes
|
|
run: |
|
|
# 1. Handle Manual Input
|
|
if [ -n "${{ inputs.image }}" ]; then
|
|
# Clean input
|
|
IMAGE=$(echo "${{ inputs.image }}" | xargs)
|
|
if [ -n "$IMAGE" ]; then
|
|
if [ -d "images/$IMAGE" ] && [ -f "images/$IMAGE/Dockerfile" ]; then
|
|
echo "Manual image detected: $IMAGE"
|
|
echo "matrix=[\"$IMAGE\"]" >> $GITHUB_OUTPUT
|
|
echo "has_changes=true" >> $GITHUB_OUTPUT
|
|
exit 0
|
|
else
|
|
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
|
|
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."
|
|
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
|
|
run: |
|
|
echo "Matrix: ${{ steps.changes.outputs.matrix }}"
|
|
echo "Has changes: ${{ steps.changes.outputs.has_changes }}"
|
|
|
|
# ============================================================================
|
|
# Job 2 : Build and Push (Standard Docker)
|
|
# ============================================================================
|
|
build:
|
|
needs: detect-changes
|
|
if: needs.detect-changes.outputs.has_changes == 'true'
|
|
runs-on: docker
|
|
container:
|
|
image: docker:cli
|
|
strategy:
|
|
matrix:
|
|
image: ${{ fromJson(needs.detect-changes.outputs.matrix) }}
|
|
fail-fast: false
|
|
steps:
|
|
- name: Install dependencies
|
|
run: apk add --no-cache git
|
|
|
|
- name: Checkout repository
|
|
run: |
|
|
git clone --depth 2 https://gitea.arnodo.fr/${{ gitea.repository }}.git .
|
|
git checkout ${{ gitea.sha }}
|
|
|
|
- name: Login to Registry
|
|
run: |
|
|
echo "${{ secrets.REGISTRY_TOKEN }}" | docker login ${{ env.REGISTRY }} -u ${{ gitea.actor }} --password-stdin
|
|
|
|
- name: Build and Push
|
|
env:
|
|
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
|
|
fi
|
|
|
|
echo "Building $IMAGE_NAME:latest from $IMAGE_PATH ..."
|
|
|
|
if [ ! -d "$IMAGE_PATH" ]; then
|
|
echo "Error: Directory $IMAGE_PATH does not exist."
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! -f "$IMAGE_PATH/Dockerfile" ]; then
|
|
echo "Error: Dockerfile not found in $IMAGE_PATH."
|
|
exit 1
|
|
fi
|
|
|
|
# Use standard docker build
|
|
docker build -t "$IMAGE_NAME:latest" "$IMAGE_PATH"
|
|
docker push "$IMAGE_NAME:latest"
|
|
|
|
echo "✅ Pushed $IMAGE_NAME:latest"
|