fix: use gitea.event_name instead of github.event_name, simplify detection logic
This commit is contained in:
@@ -6,15 +6,11 @@ on:
|
|||||||
paths:
|
paths:
|
||||||
- 'images/**'
|
- 'images/**'
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
# Manual trigger builds all images
|
|
||||||
|
|
||||||
env:
|
env:
|
||||||
REGISTRY: gitea.arnodo.fr
|
REGISTRY: gitea.arnodo.fr
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
# ============================================================================
|
|
||||||
# Job 1 : Detect modified images
|
|
||||||
# ============================================================================
|
|
||||||
detect-changes:
|
detect-changes:
|
||||||
runs-on: docker
|
runs-on: docker
|
||||||
container:
|
container:
|
||||||
@@ -24,76 +20,40 @@ 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 python3
|
run: apk add --no-cache git
|
||||||
|
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
run: |
|
run: |
|
||||||
git clone --depth 2 https://gitea.arnodo.fr/${{ gitea.repository }}.git .
|
git clone --depth 2 https://gitea.arnodo.fr/${{ gitea.repository }}.git .
|
||||||
git checkout ${{ gitea.sha }}
|
git checkout ${{ gitea.sha }}
|
||||||
|
|
||||||
- name: Detect changed images
|
- name: Detect images to build
|
||||||
id: changes
|
id: changes
|
||||||
run: |
|
run: |
|
||||||
> valid_images.txt
|
echo "Event: ${{ gitea.event_name }}"
|
||||||
|
|
||||||
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
|
if [ "${{ gitea.event_name }}" = "workflow_dispatch" ]; then
|
||||||
echo "DEBUG: Manual trigger detected. Scanning all images..."
|
echo "Manual trigger - building all images"
|
||||||
|
# List all directories with Dockerfile
|
||||||
if [ -d "images" ]; then
|
IMAGES=$(find images -mindepth 1 -maxdepth 1 -type d -exec test -f {}/Dockerfile \; -print | xargs -n1 basename | tr '\n' ',' | sed 's/,$//')
|
||||||
# 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
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
else
|
else
|
||||||
echo "DEBUG: images/ directory not found."
|
echo "Push event - detecting changes"
|
||||||
|
IMAGES=$(git diff --name-only HEAD~1 HEAD -- images/ 2>/dev/null | cut -d'/' -f2 | sort -u | grep -v '^$' | tr '\n' ',' | sed 's/,$//')
|
||||||
fi
|
fi
|
||||||
|
|
||||||
else
|
echo "Detected images: $IMAGES"
|
||||||
echo "DEBUG: Push event detected. Analyzing git diff..."
|
|
||||||
|
|
||||||
CHANGED_FILES=$(git diff --name-only HEAD~1 HEAD -- images/ 2>/dev/null || true)
|
if [ -z "$IMAGES" ]; then
|
||||||
# Extract unique directories
|
|
||||||
CANDIDATES=$(echo "$CHANGED_FILES" | cut -d'/' -f2 | sort -u | grep -v '^$')
|
|
||||||
|
|
||||||
for CANDIDATE in $CANDIDATES; do
|
|
||||||
if [ -d "images/$CANDIDATE" ] && [ -f "images/$CANDIDATE/Dockerfile" ]; then
|
|
||||||
echo "DEBUG: Change detected in image: $CANDIDATE"
|
|
||||||
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
|
|
||||||
echo "No valid images detected."
|
|
||||||
echo "has_changes=false" >> $GITHUB_OUTPUT
|
echo "has_changes=false" >> $GITHUB_OUTPUT
|
||||||
echo "image_list=[]" >> $GITHUB_OUTPUT
|
echo "image_list=[]" >> $GITHUB_OUTPUT
|
||||||
else
|
else
|
||||||
# Use Python for reliable JSON generation
|
# Convert to JSON array
|
||||||
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))')
|
JSON=$(echo "$IMAGES" | tr ',' '\n' | sed 's/.*/"&"/' | tr '\n' ',' | sed 's/,$//' | sed 's/^/[/' | sed 's/$/]/')
|
||||||
|
echo "JSON: $JSON"
|
||||||
echo "DEBUG: Generated JSON: >$JSON<"
|
|
||||||
echo "image_list=$JSON" >> $GITHUB_OUTPUT
|
|
||||||
echo "has_changes=true" >> $GITHUB_OUTPUT
|
echo "has_changes=true" >> $GITHUB_OUTPUT
|
||||||
|
echo "image_list=$JSON" >> $GITHUB_OUTPUT
|
||||||
fi
|
fi
|
||||||
|
|
||||||
- name: Show detected changes
|
|
||||||
run: |
|
|
||||||
echo "Image List: ${{ steps.changes.outputs.image_list }}"
|
|
||||||
echo "Has changes: ${{ steps.changes.outputs.has_changes }}"
|
|
||||||
|
|
||||||
# ============================================================================
|
|
||||||
# Job 2 : Build and Push (Standard Docker)
|
|
||||||
# ============================================================================
|
|
||||||
build:
|
build:
|
||||||
needs: detect-changes
|
needs: detect-changes
|
||||||
if: needs.detect-changes.outputs.has_changes == 'true'
|
if: needs.detect-changes.outputs.has_changes == 'true'
|
||||||
@@ -105,44 +65,23 @@ jobs:
|
|||||||
image: ${{ fromJson(needs.detect-changes.outputs.image_list) }}
|
image: ${{ fromJson(needs.detect-changes.outputs.image_list) }}
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
steps:
|
steps:
|
||||||
- name: Install dependencies
|
- name: Install git and checkout
|
||||||
run: apk add --no-cache git
|
|
||||||
|
|
||||||
- name: Checkout repository
|
|
||||||
run: |
|
run: |
|
||||||
git clone --depth 2 https://gitea.arnodo.fr/${{ gitea.repository }}.git .
|
apk add --no-cache git
|
||||||
git checkout ${{ gitea.sha }}
|
git clone --depth 1 https://gitea.arnodo.fr/${{ gitea.repository }}.git .
|
||||||
|
|
||||||
- name: Login to Registry
|
- name: Login to Registry
|
||||||
run: |
|
run: |
|
||||||
echo "${{ secrets.REGISTRY_TOKEN }}" | docker login ${{ env.REGISTRY }} -u ${{ gitea.actor }} --password-stdin
|
echo "${{ secrets.REGISTRY_TOKEN }}" | docker login ${{ env.REGISTRY }} -u ${{ gitea.actor }} --password-stdin
|
||||||
|
|
||||||
- name: Build and Push
|
- name: Build and Push
|
||||||
env:
|
|
||||||
IMAGE_NAME: ${{ env.REGISTRY }}/damien/${{ matrix.image }}
|
|
||||||
IMAGE_PATH: images/${{ matrix.image }}
|
|
||||||
run: |
|
run: |
|
||||||
echo "Debug: Matrix Image is '${{ matrix.image }}'"
|
IMAGE_NAME="${{ env.REGISTRY }}/damien/${{ matrix.image }}"
|
||||||
|
IMAGE_PATH="images/${{ matrix.image }}"
|
||||||
|
|
||||||
if [ -z "${{ matrix.image }}" ]; then
|
echo "Building ${IMAGE_NAME}:latest from ${IMAGE_PATH}"
|
||||||
echo "Skipping empty image name"
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Building $IMAGE_NAME:latest from $IMAGE_PATH ..."
|
docker build -t "${IMAGE_NAME}:latest" "${IMAGE_PATH}"
|
||||||
|
docker push "${IMAGE_NAME}:latest"
|
||||||
|
|
||||||
if [ ! -d "$IMAGE_PATH" ]; then
|
echo "✅ Pushed ${IMAGE_NAME}:latest"
|
||||||
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"
|
|
||||||
|
|||||||
Reference in New Issue
Block a user