Skip to content

Commit

Permalink
Only rebuild, tag, and push the images that actually changed.
Browse files Browse the repository at this point in the history
  • Loading branch information
scottwittenburg committed Sep 14, 2023
1 parent ad7abf3 commit b2b5dc5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/custom_docker_builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,22 @@ on:
- main
paths:
- images/**
- .github/workflows/custom_docker_builds.yml
- scripts/get-changed-files.sh

jobs:
getchanges:
runs-on: ubuntu-latest
outputs:
changelist: ${{ steps.changes.outputs.diff }}
steps:
- name: Checkout
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
with:
fetch-depth: 0
- name: Get changed files
id: changes
run: ./scripts/get-changed-files.sh
publish:
runs-on: ubuntu-latest
strategy:
Expand Down Expand Up @@ -50,11 +64,18 @@ jobs:
- name: Checkout
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3

- name: Matrix element to relative path
run: |
REL_PATH=$(realpath --relative-to $(git rev-parse --show-toplevel) ${{ matrix.docker-image }})
echo "REL_PATH=$REL_PATH" >> $GITHUB_ENV
- name: Set up QEMU
uses: docker/setup-qemu-action@2b82ce82d56a2a04d2637cd93a637ae1b359c0a7 # v2.2.0
if: contains(needs.getchanges.outputs.changelist, env.REL_PATH)

- name: Log in to the Container registry
uses: docker/login-action@465a07811f14bebb1938fbed4728c6a1ff8901fc # v2.2.0
if: contains(needs.getchanges.outputs.changelist, env.REL_PATH)
with:
registry: ghcr.io
username: ${{ github.actor }}
Expand All @@ -63,6 +84,7 @@ jobs:
- name: Build and push ${{ matrix.docker-image }}
id: docker-build-push
uses: docker/build-push-action@2eb1c1961a95fc15694676618e422e8ba1d63825 # v4.1.1
if: contains(needs.getchanges.outputs.changelist, env.REL_PATH)
with:
context: ${{ matrix.docker-image }}
file: ${{ matrix.docker-image }}/Dockerfile
Expand All @@ -71,4 +93,5 @@ jobs:
platforms: linux/amd64,linux/arm64

- name: Image digest
if: contains(needs.getchanges.outputs.changelist, env.REL_PATH)
run: echo ${{ steps.docker-build-push.outputs.digest }}
29 changes: 29 additions & 0 deletions scripts/get-changed-files.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

case "${GITHUB_EVENT_NAME}"
in
pull_request)
BASE_SHA=$(jq -r .pull_request.base.sha ${GITHUB_EVENT_PATH})
HEAD_SHA=$(jq -r .pull_request.head.sha ${GITHUB_EVENT_PATH})
;;
push)
BASE_SHA=$(jq -r .before ${GITHUB_EVENT_PATH})
HEAD_SHA=$(jq -r .after ${GITHUB_EVENT_PATH})
;;
*)
echo "Unable to get changed files from '${GITHUB_EVENT_NAME}' event"
exit 1
esac

echo "Event: ${GITHUB_EVENT_NAME}"
echo "Base: ${BASE_SHA}"
echo "Head: ${HEAD_SHA}"
echo ""

git fetch origin ${BASE_SHA}
git fetch origin ${HEAD_SHA}

echo ""
echo "::group::All changed files"
git diff --name-only ${BASE_SHA}...${HEAD_SHA} | xargs
echo "diff=$(git diff --name-only ${BASE_SHA}...${HEAD_SHA} | xargs dirname | xargs)" >> $GITHUB_OUTPUT

0 comments on commit b2b5dc5

Please sign in to comment.