From e7f9d864604a73ccd1df5ed2b25f2ecfdbfbc9bd Mon Sep 17 00:00:00 2001 From: Peter Law Date: Sat, 31 Aug 2024 14:09:43 +0100 Subject: [PATCH] Add support for diffing the output when the build system changes This is copied over from the website repo and aims to simplify validation of build system changes, mostly dependency updates. --- .github/workflows/render.yml | 138 +++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) diff --git a/.github/workflows/render.yml b/.github/workflows/render.yml index f2e6293..e6f60c8 100644 --- a/.github/workflows/render.yml +++ b/.github/workflows/render.yml @@ -37,6 +37,144 @@ jobs: with: path: out/html + changes: + runs-on: ubuntu-latest + if: github.ref != 'refs/heads/main' + outputs: + build-system: ${{ steps.filter.outputs.build-system }} + steps: + - name: Check if build system changed + uses: dorny/paths-filter@v3 + id: filter + with: + filters: | + build-system: + - .github/**/* + - package.json + - yarn.lock + - requirements.txt + - scripts/render* + - scripts/setup.sh + + build-main: + needs: + - changes + if: needs.changes.outputs.build-system == 'true' + env: + # ruby/setup-ruby@v1 does not offer a way to change the cached gems path. + # See https://github.com/ruby/setup-ruby/issues/291 + GLOBAL_GEMS: 1 + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + ref: main + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version-file: '.python-version' + + - name: Install dependencies + run: bash ./scripts/setup.sh + + - uses: actions/setup-node@v3 + with: + node-version-file: '.node-version' + + - name: Install + run: yarn install + + - name: Build newsletters + run: bash ./scripts/render-all.sh + + - name: Upload baseline + uses: actions/upload-pages-artifact@v2 + with: + name: build-from-main + path: out/html + + diff: + runs-on: ubuntu-latest + needs: + - build + - build-main + permissions: + pull-requests: write + steps: + - name: Download artifact from this branch's build + uses: actions/download-artifact@v3 + with: + name: github-pages + + - name: Unpack local build + run: | + mkdir local + pushd local + tar --extract --file ../artifact.tar + popd + mv artifact.tar local.tar + + - name: Download artifact from main branch build + uses: actions/download-artifact@v3 + with: + name: build-from-main + + - name: Unpack main build + run: | + mkdir main + pushd main + tar --extract --file ../artifact.tar + popd + mv artifact.tar main.tar + + - name: Diff + id: diff + run: | + set +e + diff -ru main local > ./result.diff + result=$? + set -euo pipefail + + delimiter="gha-delim-$RANDOM-$RANDOM-gha-delim" + { + echo "diff<<${delimiter}" + cat ./result.diff + echo "${delimiter}" + } >> "$GITHUB_OUTPUT" + + cat ./result.diff + + if [[ $result -ne 0 ]] + then + echo has-changes=true >> "$GITHUB_OUTPUT" + else + echo has-changes=false >> "$GITHUB_OUTPUT" + fi + + - name: Build comment + id: build-comment + if: always() + run: | + { + if [[ "${{ steps.diff.outputs.has-changes }}" = "true" ]] + then + echo "🔀 Build diff:" + echo '```diff' + cat ./result.diff + echo '```' + else + echo "Build has no changes." + fi + } > ./comment.md + + - name: Post diff to PR + uses: thollander/actions-comment-pull-request@v2 + if: always() + with: + comment_tag: build-diff + filePath: ./comment.md + deploy: name: Deploy to GitHub Pages runs-on: ubuntu-latest