From b5d0c17d36c3ed486b0441b61a6cc6c613bbd9cb Mon Sep 17 00:00:00 2001 From: Southpaw Date: Sat, 31 Aug 2024 20:36:58 +0100 Subject: [PATCH] Update PR Preview Workflow to use reusable components. --- .github/workflows/build-pr-preview.yml | 18 ++++++ .github/workflows/pr-preview.yaml | 48 -------------- .github/workflows/publish-pr-preview.yaml | 77 ++++++++--------------- .github/workflows/publish.yml | 5 ++ 4 files changed, 48 insertions(+), 100 deletions(-) create mode 100644 .github/workflows/build-pr-preview.yml delete mode 100644 .github/workflows/pr-preview.yaml diff --git a/.github/workflows/build-pr-preview.yml b/.github/workflows/build-pr-preview.yml new file mode 100644 index 00000000..40d64003 --- /dev/null +++ b/.github/workflows/build-pr-preview.yml @@ -0,0 +1,18 @@ +on: [pull_request] +name: Build PR preview + +jobs: + build: + uses: ./.github/workflows/build-website.yml + publish-pr-number: + runs-on: ubuntu-latest + steps: + - name: Write PR Number to file + env: + PR_NUMBER: ${{ github.event.number }} + run: echo $PR_NUMBER > ./pr/pr_number + - name: Upload PR Number as artifact + uses: actions/upload-artifact@v4 + with: + name: pr_number + path: pr/ \ No newline at end of file diff --git a/.github/workflows/pr-preview.yaml b/.github/workflows/pr-preview.yaml deleted file mode 100644 index f55115a3..00000000 --- a/.github/workflows/pr-preview.yaml +++ /dev/null @@ -1,48 +0,0 @@ -on: [pull_request] -name: PR preview - -jobs: - build: - name: Generate PR preview - runs-on: ubuntu-latest - permissions: - contents: read - - steps: - - uses: actions/checkout@v4 - with: - fetch-depth: 0 - filter: 'blob:none' - - - name: Setup PNPM and install dependencies - uses: pnpm/action-setup@v4 - with: - version: latest - run_install: | - - args: [--frozen-lockfile] - - - name: Build the project - run: pnpm run build - - - name: Upload HTML artifact - uses: actions/upload-artifact@v4 - with: - name: website-html - path: dist - retention-days: 1 - - - name: Upload Functions artifact - uses: actions/upload-artifact@v4 - with: - name: website-functions - path: functions - retention-days: 1 - - - name: Upload package artifact - uses: actions/upload-artifact@v4 - with: - name: website-package - path: | - package.json - pnpm-lock.yaml - retention-days: 1 diff --git a/.github/workflows/publish-pr-preview.yaml b/.github/workflows/publish-pr-preview.yaml index 4d6b178a..458d69a1 100644 --- a/.github/workflows/publish-pr-preview.yaml +++ b/.github/workflows/publish-pr-preview.yaml @@ -1,62 +1,35 @@ on: workflow_run: - workflows: ["PR preview"] + workflows: ["Build PR preview"] types: [completed] name: Publish PR preview jobs: - publish: - name: Publish PR preview + get-pr-number: + # We need the PR number to generate the branch URL for Cloudflare Pages, but GitHub doesn't seem to have a way to derive the PR number (or receive any input at all) from the triggering workflow, so we use artifacts instead. + name: Get PR Number runs-on: ubuntu-latest + outputs: + pr-number: ${{ steps.output-pr-number.pr-number }} + permissions: + actions: read if: ${{ github.event.workflow_run.conclusion == 'success' }} - steps: - - name: Download HTML artifact - uses: dawidd6/action-download-artifact@09f2f74827fd3a8607589e5ad7f9398816f540fe # v3.1.4 - with: - name: website-html - path: dist - run_id: ${{ github.event.workflow_run.id }} - - - name: Download Functions artifact - uses: dawidd6/action-download-artifact@09f2f74827fd3a8607589e5ad7f9398816f540fe # v3.1.4 - with: - name: website-functions - path: functions - run_id: ${{ github.event.workflow_run.id }} - - - name: Download package artifact - uses: dawidd6/action-download-artifact@09f2f74827fd3a8607589e5ad7f9398816f540fe # v3.1.4 - with: - name: website-package - run_id: ${{ github.event.workflow_run.id }} - - - name: Setup node - uses: actions/setup-node@v4 - with: - node-version-file: package.json - - - name: Setup PNPM and install dependencies - uses: pnpm/action-setup@v4 - with: - version: latest - run_install: | - - args: [--frozen-lockfile] - - - # TODO this action is deprecated! - - name: Gather PR details - uses: potiuk/get-workflow-origin@e2dae063368361e4cd1f510e8785cd73bca9352e # v1_5 (v1_6 is broken!) - id: pr-details - with: - token: ${{ secrets.GITHUB_TOKEN }} - sourceRunId: ${{ github.event.workflow_run.id }} - - - name: Publish to Pages - uses: cloudflare/pages-action@1 + - name: Download Artifact from triggering workflow + env: + RUN_ID: ${{ github.event.workflow_run.id }} + uses: actions/download-artifact@v4 with: - apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} - accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} - projectName: quiltmc-org - directory: dist - branch: preview-${{ steps.pr-details.outputs.pullRequestNumber }} + name: pr_number + path: pr + run-id: ${{ env.RUN_ID }} + - name: Output PR Number + id: output-pr-number + run: echo "pr-number=$(cat ./pr/pr_number)" >> "$GITHUB_OUTPUT" + + publish-preview: + uses: ./.github/workflows/publish.yml + needs: get-pr-number + with: + branch: preview-${{ needs.get-pr-number.pr-number }} + run-id: ${{ github.event.workflow_run.id }} diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 7399c4c6..17fd50c5 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -5,6 +5,9 @@ on: required: false default: ${{ github.ref_name }} type: string + run-id: + required: false + type: string outputs: deployment-url: description: The URL of the deployed site. @@ -21,6 +24,7 @@ jobs: env: CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + GITHUB_RUN_ID: ${{ inputs.run-id }} steps: - name: Download Website HTML uses: actions/download-artifact@v4 @@ -32,6 +36,7 @@ jobs: with: name: functions path: functions + run-id: ${{ env.GITHUB_RUN_ID }} # The following hack is required because Cloudflare can't compile the workeers in the functions/ directory seperately from deploying, so it needs the packages they use in order to compile them at deployment time, but installing *everything* in package.json would be a waste. The build and publish steps can't go in the same job, because PR Previews have to build the site without publishing it (since they can't access secrets). Hopefully we can remove the need for this in the future by switching to Astro Actions, which should be compiled with the rest of the site. - name: Install packages required by functions