This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 matrix | |
MATRIX="[" | |
SEPARATOR="" | |
# Find and display all Dockerfiles | |
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 | |
# Remove leading ./ if present | |
dockerfile_clean=$(echo "$dockerfile" | sed 's|^./||') | |
image_name=$(echo "$dockerfile_clean" | sed 's/.*Dockerfile\.//') | |
context_dir=$(dirname "$dockerfile_clean") | |
if [ "$context_dir" = "." ]; then | |
context_path="deployment/images" | |
else | |
context_path="deployment/images/$context_dir" | |
fi | |
echo "β‘ Processing $dockerfile_clean" | |
echo " β’ Image name: $image_name" | |
echo " β’ Context directory: $context_dir" | |
echo " β’ Full context path: $context_path" | |
# Check for changes | |
CHANGED=false | |
echo " β’ Checking for changes in context..." | |
# First, check if the Dockerfile itself changed | |
if [[ "${{ steps.changed-files.outputs.all_changed_files }}" == *"deployment/images/$dockerfile_clean"* ]]; then | |
CHANGED=true | |
echo " β Dockerfile changed" | |
fi | |
# Then check context directory | |
if [ ! "$CHANGED" = true ] && [ "$context_dir" != "." ]; then | |
for changed_file in ${{ steps.changed-files.outputs.all_changed_files }}; do | |
if [[ $changed_file == $context_path/* ]]; then | |
CHANGED=true | |
echo " β Found change in context: $changed_file" | |
break | |
fi | |
done | |
fi | |
# If changed, add to matrix | |
if [ "$CHANGED" = true ]; then | |
echo " β Changes detected - adding to build matrix" | |
MATRIX="${MATRIX}${SEPARATOR}{\"image\": \"${image_name}\", \"dockerfile\": \"${dockerfile_clean}\", \"context\": \"${context_dir}\"}" | |
SEPARATOR="," | |
else | |
echo " βοΈ No changes detected - skipping" | |
fi | |
done | |
MATRIX="${MATRIX}]" | |
echo "π Final build matrix:" | |
echo "$MATRIX" | jq '.' | |
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" |