Files
docker-images/.gitea/workflows/build-images.yml

136 lines
4.7 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). Leave empty to build all.'
required: false
default: ''
env:
REGISTRY: gitea.arnodo.fr
jobs:
build:
runs-on: docker
container:
image: docker:cli
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: Detect images to build
id: detect
run: |
echo "Event: ${{ gitea.event_name }}"
if [ "${{ gitea.event_name }}" = "workflow_dispatch" ]; then
if [ -n "${{ inputs.image }}" ]; then
# Manual trigger with specific image
echo "Manual trigger - building specific image: ${{ inputs.image }}"
IMAGES="${{ inputs.image }}"
else
# Manual trigger without input = build all
echo "Manual trigger - building all images"
IMAGES=""
for dir in images/*/; do
if [ -f "${dir}Dockerfile" ]; then
name=$(basename "$dir")
echo "Found image: $name"
IMAGES="${IMAGES}${name} "
fi
done
fi
else
# Push event = detect changes only
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' ' ')
fi
echo "Images to build: $IMAGES"
echo "images=$IMAGES" >> $GITHUB_OUTPUT
- name: Login to Registry
run: |
echo "${{ secrets.REGISTRY_TOKEN }}" | docker login ${{ env.REGISTRY }} -u ${{ gitea.actor }} --password-stdin
- name: Build and Push all images
run: |
IMAGES="${{ steps.detect.outputs.images }}"
if [ -z "$IMAGES" ]; then
echo "No images to build"
exit 0
fi
for IMAGE in $IMAGES; do
IMAGE_NAME="${{ env.REGISTRY }}/damien/${IMAGE}"
IMAGE_PATH="images/${IMAGE}"
# Validate image path exists
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
echo "========================================"
echo "Building ${IMAGE_NAME}"
echo "========================================"
# Check for version file (format: <NAME>_VERSION or VERSION)
VERSION=""
BUILD_ARGS=""
# Look for specific version file (e.g., NETBOX_VERSION, TERRAFORM_VERSION)
for version_file in "$IMAGE_PATH"/*_VERSION "$IMAGE_PATH"/VERSION; do
if [ -f "$version_file" ]; then
VERSION=$(cat "$version_file" | tr -d '[:space:]')
VERSION_NAME=$(basename "$version_file" | sed 's/_VERSION$//')
if [ "$VERSION_NAME" = "VERSION" ]; then
# Generic VERSION file
echo "📌 Found VERSION file: $VERSION"
else
# Specific version file (e.g., NETBOX_VERSION)
echo "📌 Found ${VERSION_NAME}_VERSION file: $VERSION"
BUILD_ARGS="--build-arg ${VERSION_NAME}_VERSION=${VERSION}"
fi
break
fi
done
# Build the image with --no-cache to ensure fresh build
if [ -n "$BUILD_ARGS" ]; then
echo "🔧 Building with args: $BUILD_ARGS"
docker build --no-cache $BUILD_ARGS -t "${IMAGE_NAME}:latest" "$IMAGE_PATH"
else
docker build --no-cache -t "${IMAGE_NAME}:latest" "$IMAGE_PATH"
fi
# Push latest tag
docker push "${IMAGE_NAME}:latest"
echo "✅ Pushed ${IMAGE_NAME}:latest"
# Also tag and push with version if available
if [ -n "$VERSION" ]; then
docker tag "${IMAGE_NAME}:latest" "${IMAGE_NAME}:${VERSION}"
docker push "${IMAGE_NAME}:${VERSION}"
echo "✅ Pushed ${IMAGE_NAME}:${VERSION}"
fi
done