From cd8a67a18e1409452728306de594000b2eafe292 Mon Sep 17 00:00:00 2001 From: TrebledJ <39648915+TrebledJ@users.noreply.github.com> Date: Sat, 8 Jun 2024 15:58:22 +0800 Subject: [PATCH] feat(workflows): revamp workflows with artifacts instead of a `site` branch Using artifacts for build output allows them to be more easily managed, compared to checking out an entire repository. --- .../{automerge.yml => pr.automerge.yml} | 52 ++++++------- .../workflows/{links.yml => site.links.yml} | 8 +- .github/workflows/site.validate.yml | 44 +++++++++++ .../workflows/{build.yml => src.build.yml} | 74 +++++++++++-------- .github/workflows/{lint.yml => src.lint.yml} | 0 .github/workflows/{test.yml => src.test.yml} | 0 .github/workflows/validate-html.yml | 27 ------- 7 files changed, 116 insertions(+), 89 deletions(-) rename .github/workflows/{automerge.yml => pr.automerge.yml} (83%) rename .github/workflows/{links.yml => site.links.yml} (92%) create mode 100644 .github/workflows/site.validate.yml rename .github/workflows/{build.yml => src.build.yml} (55%) rename .github/workflows/{lint.yml => src.lint.yml} (100%) rename .github/workflows/{test.yml => src.test.yml} (100%) delete mode 100644 .github/workflows/validate-html.yml diff --git a/.github/workflows/automerge.yml b/.github/workflows/pr.automerge.yml similarity index 83% rename from .github/workflows/automerge.yml rename to .github/workflows/pr.automerge.yml index 585878529..c0d8b81b4 100644 --- a/.github/workflows/automerge.yml +++ b/.github/workflows/pr.automerge.yml @@ -4,16 +4,9 @@ name: Dependabot Reviewer on: pull_request_target # on: workflow_dispatch -# on: -# workflow_run: -# workflows: [Dry Run Build] -# branches: -# - 'dependabot/**' -# types: -# - completed env: - OLD_BUILD_FOLDER: old-build + MASTER_REF: master permissions: pull-requests: write @@ -27,55 +20,58 @@ jobs: run: | echo "## Debug" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY + echo "- ref: ${{ github.ref }}" >> $GITHUB_STEP_SUMMARY echo "- user.login: ${{ github.event.pull_request.user.login }}" >> $GITHUB_STEP_SUMMARY echo "- merged: ${{ github.event.pull_request.merged }}" >> $GITHUB_STEP_SUMMARY echo "- merged_at: ${{ github.event.pull_request.merged_at }}" >> $GITHUB_STEP_SUMMARY echo "- mergeable: ${{ github.event.pull_request.mergeable }}" >> $GITHUB_STEP_SUMMARY echo "- mergeable_state: ${{ github.event.pull_request.mergeable_state }}" >> $GITHUB_STEP_SUMMARY - pre-diff: - name: Pre-Diff Checkout - runs-on: ubuntu-latest - steps: - - name: Checkout Base Site - uses: actions/checkout@v4 - with: - ref: site - path: ${{ env.OLD_BUILD_FOLDER }} - - - name: Sanity Check - run: | - echo "cwd: `pwd`" && echo "Workspace: $GITHUB_WORKSPACE" && ls -Al $GITHUB_WORKSPACE + sanity: + if: github.event.pull_request.user.login == 'dependabot[bot]' build: + needs: sanity uses: ./.github/workflows/build.yml diff: name: Diff Builds - needs: [pre-diff, build] + needs: build runs-on: ubuntu-latest steps: - name: Sanity Check run: | echo "cwd: $(pwd)" && echo "Workspace: $GITHUB_WORKSPACE" && ls -Al $GITHUB_WORKSPACE - echo "\ndiff: $(which diff)" + echo "diff: $(which diff)" + + - name: Download Master Site + uses: actions/download-artifact@v4 + with: + name: site-${{ env.MASTER_REF }} + path: master/ + + - name: Download PR Site + uses: actions/download-artifact@v4 + with: + name: ${{ needs.build.outputs.artifact }} + path: pr/ - name: Diff Folders id: diff continue-on-error: true - run: diff --unified --recursive ${{ env.OLD_BUILD_FOLDER }}/_site/ $GITHUB_WORKSPACE/_site/ >.diff - - - name: Epic Fail + run: diff --unified --recursive master/ pr/ >.diff + + - name: Diff Found if: steps.diff.outcome != 'success' run: | - echo "## Diff Output" >> $GITHUB_STEP_SUMMARY + echo '## Diff Output' >> $GITHUB_STEP_SUMMARY echo '```diff' >> $GITHUB_STEP_SUMMARY cat .diff >> $GITHUB_STEP_SUMMARY echo '```' >> $GITHUB_STEP_SUMMARY exit 1 - test: + needs: sanity uses: ./.github/workflows/test.yml review-dependabot-pr: diff --git a/.github/workflows/links.yml b/.github/workflows/site.links.yml similarity index 92% rename from .github/workflows/links.yml rename to .github/workflows/site.links.yml index 71f6f4160..56795ff49 100644 --- a/.github/workflows/links.yml +++ b/.github/workflows/site.links.yml @@ -13,13 +13,17 @@ on: schedule: - cron: "00 11 * * 2,5" # HKT 7PM Tuesday/Friday +env: + REF: master + jobs: check: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: actions/download-artifact@v4 with: - ref: site + name: site-${{ env.REF }} + path: _site/ # - name: Restore Lychee Cache # uses: actions/cache@v3 diff --git a/.github/workflows/site.validate.yml b/.github/workflows/site.validate.yml new file mode 100644 index 000000000..9b644e61a --- /dev/null +++ b/.github/workflows/site.validate.yml @@ -0,0 +1,44 @@ +# Workflow to validate site files. +name: Validate + +on: + workflow_dispatch: + + # Run after a site build completed. + workflow_run: + workflows: [Build] + types: [completed] + branches: [master] + +env: + REF: master + +jobs: + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/download-artifact@v4 + with: + name: site-${{ env.REF }} + path: _site/ + + - name: Setup Node + uses: actions/setup-node@v4 + with: + node-version: 20 + cache: npm + + - name: Restore npm Cache + id: cache-npm + uses: actions/cache@v4 + with: + path: node_modules + key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }} + + - name: Install Dependencies + if: steps.cache-npm.outputs.cache-hit != 'true' + run: npm ci + + - name: Validate HTML + run: npm run validate-html diff --git a/.github/workflows/build.yml b/.github/workflows/src.build.yml similarity index 55% rename from .github/workflows/build.yml rename to .github/workflows/src.build.yml index 923a10377..a60295fae 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/src.build.yml @@ -1,8 +1,5 @@ - -# Build (without deploying) an Eleventy site for CI testing. -# Useful for testing PRs and branches. -# See deploy.yml for a build + deploy to GitHub Pages workflow. -name: Dry Run Build +# Build an Eleventy site and upload build artifacts. +name: Build on: push: @@ -21,17 +18,26 @@ on: - '!scripts/**' - '!ust/**' + # Regularly build (to keep the main site artifact alive). + schedule: + - cron: "00 10 1 * *" # HKT 6PM, first day of each month + # Allows this workflow to be called from other workflows. workflow_call: + outputs: + artifact: + description: The artifact name of the uploaded site files. + value: ${{ jobs.build.outputs.artifact }} # Allows you to run this workflow manually from the Actions tab. workflow_dispatch: jobs: - # Build job build: name: Build runs-on: ubuntu-latest + outputs: + artifact: ${{ steps.upload.outputs.artifact }} steps: - name: Get Ref id: get-ref @@ -53,51 +59,55 @@ jobs: - name: Sanity Check run: | + echo '## ref' >> $GITHUB_STEP_SUMMARY + echo '```' >> $GITHUB_STEP_SUMMARY + echo "ref: ${{ github.ref }}" >> $GITHUB_STEP_SUMMARY + echo "ref_name: ${{ github.ref_name }}" >> $GITHUB_STEP_SUMMARY + echo "ref_type: ${{ github.ref_type }}" >> $GITHUB_STEP_SUMMARY + echo '```' >> $GITHUB_STEP_SUMMARY + echo '## `git status`' >> $GITHUB_STEP_SUMMARY echo '```' >> $GITHUB_STEP_SUMMARY git status >> $GITHUB_STEP_SUMMARY echo '```' >> $GITHUB_STEP_SUMMARY - - name: Detect Package Manager - id: detect-package-manager - run: | - if [ -f "${{ github.workspace }}/yarn.lock" ]; then - echo "manager=yarn" >> $GITHUB_OUTPUT - echo "command=install" >> $GITHUB_OUTPUT - exit 0 - elif [ -f "${{ github.workspace }}/package.json" ]; then - echo "manager=npm" >> $GITHUB_OUTPUT - echo "command=ci" >> $GITHUB_OUTPUT - exit 0 - else - echo "Unable to determine packager manager" >> $GITHUB_STEP_SUMMARY - exit 1 - fi - - name: Setup Node uses: actions/setup-node@v4 with: node-version: 20 - cache: ${{ steps.detect-package-manager.outputs.manager }} + cache: npm - - name: Restore cache + - name: Restore 11ty Cache uses: actions/cache@v4 with: - path: | - .cache - .eleventy/cache + path: .cache # Generate a new cache whenever packages or source files change. - key: ${{ runner.os }}-eleventy-${{ hashFiles('**/package-lock.json', '**/yarn.lock') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }} + key: ${{ runner.os }}-eleventy-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }} # If source files changed but packages didn't, rebuild from a prior cache. - restore-keys: | - ${{ runner.os }}-eleventy-${{ hashFiles('**/package-lock.json', '**/yarn.lock') }}- + restore-keys: ${{ runner.os }}-eleventy-${{ hashFiles('**/package-lock.json') }}- + + - name: Restore npm Cache + id: cache-npm + uses: actions/cache@v4 + with: + path: node_modules + key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }} - name: Install Dependencies - run: ${{ steps.detect-package-manager.outputs.manager }} ${{ steps.detect-package-manager.outputs.command }} + if: steps.cache-npm.outputs.cache-hit != 'true' + run: npm ci - name: Build run: | echo "## Build Output" >> $GITHUB_STEP_SUMMARY echo '```' >> $GITHUB_STEP_SUMMARY - ${{ steps.detect-package-manager.outputs.manager }} run prod >> $GITHUB_STEP_SUMMARY + npm run prod >> $GITHUB_STEP_SUMMARY echo '```' >> $GITHUB_STEP_SUMMARY + + - uses: actions/upload-artifact@v4 + with: + name: site-${{ github.ref }} + path: _site/ + + - id: upload + run: echo 'site-${{ github.ref }}' >> $GITHUB_OUTPUT diff --git a/.github/workflows/lint.yml b/.github/workflows/src.lint.yml similarity index 100% rename from .github/workflows/lint.yml rename to .github/workflows/src.lint.yml diff --git a/.github/workflows/test.yml b/.github/workflows/src.test.yml similarity index 100% rename from .github/workflows/test.yml rename to .github/workflows/src.test.yml diff --git a/.github/workflows/validate-html.yml b/.github/workflows/validate-html.yml deleted file mode 100644 index 2c612ec74..000000000 --- a/.github/workflows/validate-html.yml +++ /dev/null @@ -1,27 +0,0 @@ -# Workflow to validate HTML. - -name: Validate HTML - -on: - workflow_dispatch: - push: - branches: site - paths: - - '_site/**' - -jobs: - check: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - with: - ref: site - - - name: Set up Node.js - uses: actions/setup-node@v4 - with: - node-version: 20 - cache: npm - - - name: Run validate-html - run: npm run validate-html