add more logging #3
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 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" |