From e387bf47a2a61cebc12ebe635ac7441be0c65b5f Mon Sep 17 00:00:00 2001 From: Enderson Maia Date: Mon, 4 Sep 2023 15:37:02 +0100 Subject: [PATCH] ci: limit build to changed templates --- .github/workflows/build.yaml | 69 +++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 16 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 3d97b8a..0c02754 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -8,23 +8,60 @@ permissions: contents: read concurrency: ${{ github.workflow }}-${{ github.ref }} jobs: - build: - name: sunodo build + changedfiles: + runs-on: ubuntu-latest + outputs: + changedTemplates: ${{ steps.changes.outputs.CHANGED_TEMPLATES }} + allTemplate: ${{ steps.changes.outputs.ALL_TEMPLATES }} + steps: + - name: Checkout repository + uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Get template names + id: changes + run: | + echo "CHANGED_TEMPLATES=$(git diff --name-only --diff-filter=ACMRT ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | awk -F/ '/^[a-z]*\// {print $1}' | sort -u | jq -Rnc '[inputs]')" >> "$GITHUB_OUTPUT" + echo "ALL_TEMPLATES=$(find ./ -name Dockerfile | awk -F/ '/^[a-z]*\// {print $1}' | sort -u | jq -Rnc '[inputs]')" >> "$GITHUB_OUTPUT" + + build-on-main: + needs: changedfiles + if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' && (needs.changedfiles.outputs.allTemplates != '' && toJson(fromjson(needs.changedfiles.outputs.allTemplates)) != '[]') }} + name: sunodo build ${{ matrix.allTemplates }} + runs-on: ubuntu-latest + strategy: + matrix: + allTemplates: ${{ fromJson(needs.changedfiles.outputs.allTemplates) }} + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: current + + - name: Install Sunodo + run: npm install -g @sunodo/cli + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Check system requirements + run: sunodo doctor + + - name: Build from main + run: sunodo build + working-directory: ${{ matrix.ALL_TEMPLATES }} + + build-on-pr: + needs: changedfiles + if: ${{ github.event_name == 'pull_request' && (needs.changedfiles.outputs.changedTemplates != '' && toJson(fromjson(needs.changedfiles.outputs.changedTemplates)) != '[]') }} + name: sunodo build ${{ matrix.changedTempl&& needs.changedfiles.outputs.allTemplatesates }} runs-on: ubuntu-latest strategy: matrix: - template: - [ - cpp, - cpp-low-level, - go, - javascript, - lua, - python, - ruby, - rust, - typescript, - ] + changedTemplates: ${{ fromJson(needs.changedfiles.outputs.changedTemplates) }} steps: - name: Checkout uses: actions/checkout@v3 @@ -43,6 +80,6 @@ jobs: - name: Check system requirements run: sunodo doctor - - name: Build + - name: Build from PR run: sunodo build - working-directory: ${{ matrix.template }} + working-directory: ${{ matrix.changedTemplates }}