Skip to content

Commit

Permalink
Update PR Preview Workflow to use reusable components.
Browse files Browse the repository at this point in the history
  • Loading branch information
Southpaw1496 committed Aug 31, 2024
1 parent ef841d1 commit b5d0c17
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 100 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/build-pr-preview.yml
Original file line number Diff line number Diff line change
@@ -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/
48 changes: 0 additions & 48 deletions .github/workflows/pr-preview.yaml

This file was deleted.

77 changes: 25 additions & 52 deletions .github/workflows/publish-pr-preview.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
5 changes: 5 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down

1 comment on commit b5d0c17

@Cozy-GitHub
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See preview on Cloudflare Pages: https://7b5f8bf6.quiltmc-org.pages.dev

Please sign in to comment.