From 1a3dc7f9cc13623daf4fa85ce2079f581d6db33e Mon Sep 17 00:00:00 2001 From: Ryan Drew Date: Wed, 31 Jul 2024 16:31:42 -0600 Subject: [PATCH] ci: Modify build workflow to only build images on changes Signed-off-by: Ryan Drew --- .github/images.json | 16 +++++++++++ .github/workflows/build.yaml | 51 ++++++++++++++++++++++++++++++------ 2 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 .github/images.json diff --git a/.github/images.json b/.github/images.json new file mode 100644 index 00000000..a8e5f1fa --- /dev/null +++ b/.github/images.json @@ -0,0 +1,16 @@ + +{ + "include": [ + { + "name": "cmapisrv-mock", + "context": "cmapisrv-mock", + "dockerfile": "./cmapisrv-mock/Dockerfile" + }, + { + "name": "egw-scale-utils", + "context": "egw-scale-utils", + "dockerfile": "./egw-scale-utils/Dockerfile" + } + ] +} + diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 19d1f101..d067c828 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -7,19 +7,54 @@ on: - main jobs: + generate-matrix: + name: Create build matrix + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.generate-matrix.outputs.matrix }} + + steps: + - name: Checkout Source Code + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + with: + persist-credentials: false + + - name: Check code changes + uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2 + if: ${{ github.event_name == 'pull_request' }} + id: changes + with: + # These filter names must be synced with the `name` attribute for + # each entry in images.json. + filters: | + cmapisrv-mock: + - 'cmapisrv-mock/**' + - '.github/workflows/build.yaml' + egw-scale-utils: + - 'egw-scale-utils/**' + - '.github/workflows/build.yaml' + + - name: Generate matrix + id: generate-matrix + run: | + if [[ "${{ github.event_name }}" == "push" ]]; then + echo "matrix=$(jq -c . ./.github/images.json)" >> $GITHUB_OUTPUT + exit 0 + fi + + jq -c '{"include": [ .include[] | select(any(.name == ${{ steps.changes.outputs.changes }}[]; .)) ]}' ./.github/images.json > /tmp/matrix.json + echo "Generated matrix" + cat /tmp/matrix.json + + echo "matrix=$(jq -c . /tmp/matrix.json)" >> $GITHUB_OUTPUT + build-and-push: name: Build and Push runs-on: ubuntu-latest + needs: generate-matrix strategy: - matrix: - include: - - name: cmapisrv-mock - context: cmapisrv-mock - dockerfile: ./cmapisrv-mock/Dockerfile - - name: egw-scale-utils - context: egw-scale-utils - dockerfile: ./egw-scale-utils/Dockerfile + matrix: ${{ fromJSON(needs.generate-matrix.outputs.matrix) }} steps: - name: Set up Docker Buildx