Skip to content

Add dynamic workflow for service dockers #1

Add dynamic workflow for service dockers

Add dynamic workflow for service dockers #1

name: Dynamic Service Docker Builds
on:
push:
branches:
- main
paths-ignore:
- "deployment/**"
- "gef-portal-scraper/**"
- "**.md"
pull_request:
branches:
- main
paths-ignore:
- "deployment/**"
- "gef-portal-scraper/**"
- "**.md"
jobs:
detect_changes:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Check out repository
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v40
- name: Set up build matrix
id: set-matrix
run: |
# Initialize variables
MATRIX="["
SEPARATOR=""
# List of directories to exclude (space-separated)
EXCLUDED_DIRS="deployment resources gef-portal-scraper"
# Find all Dockerfiles in service directories
for dockerfile in $(find . -name "Dockerfile" -not -path "*/\.*"); do
# Get service directory (parent of Dockerfile)
service_dir=$(dirname "$dockerfile")
service_name=$(basename "$service_dir")
# Skip if directory is in excluded list
skip=false
for excluded in $EXCLUDED_DIRS; do
if [[ "$service_dir" == *"$excluded"* ]]; then
skip=true
echo "⏭️ Skipping excluded directory: $service_dir"
break
fi
done
[ "$skip" = true ] && continue
# Check for changes in service directory
CHANGED=false
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
if [[ $file == $service_dir/* ]]; then
CHANGED=true
echo "✨ Found changes in service: $service_name"
echo " Changed file: $file"
break
fi
done
# If changes detected, add to build matrix
if [ "$CHANGED" = true ]; then
echo "🔨 Adding $service_name to build matrix"
MATRIX="${MATRIX}${SEPARATOR}{\"service\": \"${service_name}\", \"context\": \"${service_dir#./}\"}"
SEPARATOR=","
else
echo "⏭️ No changes detected for $service_name - skipping"
fi
done
MATRIX="${MATRIX}]"
echo "📊 Final build matrix:"
echo "$MATRIX" | jq '.'
if [ "$MATRIX" = "[]" ]; then
echo "⚠️ No services to build"
echo "matrix=[]" >> $GITHUB_OUTPUT
else
echo "🚀 Services ready for build"
echo "matrix=$MATRIX" >> $GITHUB_OUTPUT
fi
build_and_push:
needs: detect_changes
if: ${{ needs.detect_changes.outputs.matrix != '[]' }}
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
strategy:
matrix:
include: ${{ fromJson(needs.detect_changes.outputs.matrix) }}
steps:
- name: Check out repository
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Define lowercase repository owner
id: repo
run: |
echo "REPO_OWNER_LC=$(echo ${{ github.repository_owner }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
- name: Display build information
run: |
echo "🏗️ Building service: ${{ matrix.service }}"
echo " • Context: ${{ matrix.context }}"
echo " • Image name: ghcr.io/${{ env.REPO_OWNER_LC }}/${{ matrix.service }}:latest"
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: ./${{ matrix.context }}
file: ./${{ matrix.context }}/Dockerfile
push: true
tags: ghcr.io/${{ env.REPO_OWNER_LC }}/${{ matrix.service }}:latest
- name: Build status
run: |
echo "✅ Successfully built and pushed: ghcr.io/${{ env.REPO_OWNER_LC }}/${{ matrix.service }}:latest"