Skip to content

Workflow file for this run

name: B&P Dynamic Docker Images
on:
push:
branches:
- main
paths:
- ".github/workflows/dynamic-build.yml"
- "deployment/images/**"
pull_request:
branches:
- main
paths:
- ".github/workflows/dynamic-build.yml"
- "deployment/images/**"
jobs:
detect_changes:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Check out the repository
uses: actions/checkout@v3
with:
fetch-depth: 0 # Fetch all history for comparing changes
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v40
with:
files: deployment/images/**
- name: Display all changed files
run: |
echo "πŸ” Changed files in this push/PR:"
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
echo " β†’ $file"
done
- name: Set up build matrix
id: set-matrix
run: |
cd deployment/images
echo "πŸ“ Scanning directory: $(pwd)"
# Initialize an empty array for the matrix
MATRIX="["
SEPARATOR=""
# Find and display all Dockerfiles first
echo "🐳 Found Dockerfiles:"
find . -type f -name "Dockerfile.*" | while read -r dockerfile; do
echo " β†’ $dockerfile"
done
# Process each Dockerfile
for dockerfile in $(find . -type f -name "Dockerfile.*"); do
image_name=$(echo $dockerfile | sed 's/.*Dockerfile\.//')
context_dir=$(dirname $dockerfile)
echo "⚑ Processing $dockerfile"
echo " β€’ Image name: $image_name"
echo " β€’ Context directory: $context_dir"
# Check if Dockerfile or any files in its context directory changed
CHANGED=false
echo " β€’ Checking for changes in context..."
for changed_file in ${{ steps.changed-files.outputs.all_changed_files }}; do
if [[ $changed_file == deployment/images/$context_dir/* ]] || [[ $changed_file == $dockerfile ]]; then
CHANGED=true
echo " βœ“ Found change in: $changed_file"
break
fi
done
# If changed, add to matrix
if [ "$CHANGED" = true ]; then
echo " βœ… Changes detected - adding to build matrix"
MATRIX="${MATRIX}${SEPARATOR}{\"image\": \"${image_name}\", \"dockerfile\": \"${dockerfile}\", \"context\": \"${context_dir}\"}"
SEPARATOR=","
else
echo " ⏭️ No changes detected - skipping"
fi
done
MATRIX="${MATRIX}]"
# Display final matrix
echo "πŸ“Š Final build matrix:"
echo "$MATRIX" | jq '.'
# If matrix is empty (just []), set to empty list to skip builds
if [ "$MATRIX" = "[]" ]; then
echo "⚠️ No changes detected in any Docker contexts - skipping builds"
echo "matrix=[]" >> $GITHUB_OUTPUT
else
echo "πŸš€ Changes detected - proceeding with builds"
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 the 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 "Converting ${{ github.repository_owner }} to lowercase..."
echo "REPO_OWNER_LC=$(echo ${{ github.repository_owner }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
- name: Display build information
run: |
echo "πŸ—οΈ Building image: ${{ matrix.image }}"
echo " β€’ Dockerfile: ${{ matrix.dockerfile }}"
echo " β€’ Context: ${{ matrix.context }}"
echo " β€’ Full image name: ghcr.io/${{ env.REPO_OWNER_LC }}/${{ matrix.image }}:latest"
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: "./deployment/images/${{ matrix.context }}"
file: "./deployment/images/${{ matrix.dockerfile }}"
push: true
tags: ghcr.io/${{ env.REPO_OWNER_LC }}/${{ matrix.image }}:latest
- name: Build status
run: |
echo "βœ… Successfully built and pushed: ghcr.io/${{ env.REPO_OWNER_LC }}/${{ matrix.image }}:latest"