Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions .github/workflows/preview-deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Publish the site built by preview-pr.yaml to Cloudflare Pages. It runs from
# the default branch with the secrets, and never runs the PR code: it only
# uploads the files built by preview-pr.yaml.
name: preview-deploy

on:
workflow_run:
workflows: [preview-pr]
types: [completed]

permissions:
contents: read

jobs:
# PRs from forks wait for a manual approval of the "external" environment
authorize:
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
environment:
${{ github.event.workflow_run.head_repository.full_name != github.repository &&
'external' || 'internal' }}
runs-on: ubuntu-latest
steps:
- run: "true"

deploy:
needs: authorize
runs-on: ubuntu-latest
permissions:
actions: read
pull-requests: write
# Shows the preview link on the job, like the Pages deploy
environment:
name: preview
url: ${{ steps.preview-pages.outputs.deployment-url }}
env:
GH_TOKEN: ${{ github.token }}
steps:
- name: Download artifacts
uses: actions/download-artifact@v4
with:
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ github.token }}
# The artifacts come from the PR code: make sure the number is really
# the PR of the commit that was built
- name: Find the PR
id: pr
env:
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
run: |
number=$(cat pr-number/pr-number)
[[ "$number" =~ ^[0-9]+$ ]] || { echo "Invalid PR number"; exit 1; }
sha=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$number" --jq .head.sha)
[[ "$sha" == "$HEAD_SHA" ]] || { echo "PR #$number is not at $HEAD_SHA"; exit 1; }
echo "number=$number" >> "$GITHUB_OUTPUT"
- name: Extract site
run: mkdir dist && tar -xf github-pages/artifact.tar -C dist
- name: Publish to Cloudflare Pages
id: preview-pages
uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT }}
command: pages deploy dist --project-name=hack-gpon-preview --branch=preview
- name: Comment on the PR
env:
URL: ${{ steps.preview-pages.outputs.deployment-url }}
run: |
gh pr comment "${{ steps.pr.outputs.number }}" --repo "$GITHUB_REPOSITORY" \
--body "Preview of the website obtained from the PR: $URL"
45 changes: 9 additions & 36 deletions .github/workflows/preview-pr.yaml
Original file line number Diff line number Diff line change
@@ -1,53 +1,26 @@
# Build the site of a PR. It runs the PR code, so it has no secrets and a
# read-only token: preview-deploy.yaml publishes the result.
name: preview-pr

on:
pull_request_target:
pull_request:
types: [opened, reopened, synchronize]

permissions:
contents: read

jobs:
authorize:
environment:
${{ github.event_name == 'pull_request_target' &&
github.event.pull_request.head.repo.full_name != github.repository &&
'external' || 'internal' }}
runs-on: ubuntu-latest
steps:
- run: "true"

# The PR code is built without access to any secret
build:
needs: authorize
uses: ./.github/workflows/build.yml
with:
ref: ${{ github.event.pull_request.head.sha }}

deploy:
needs: build
# Tells preview-deploy.yaml which PR to comment on
pr-number:
runs-on: ubuntu-latest
permissions:
pull-requests: write
# Shows the preview link on the job and in the PR, like the Pages deploy
environment:
name: preview
url: ${{ steps.preview-pages.outputs.deployment-url }}
steps:
- name: Download artifact
uses: actions/download-artifact@v4
with:
name: github-pages
- name: Extract site
run: mkdir dist && tar -xf artifact.tar -C dist
- name: Publish to Cloudflare Pages
id: preview-pages
uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT }}
command: pages deploy dist --project-name=hack-gpon-preview --branch=preview
- uses: thollander/actions-comment-pull-request@v2
- run: echo "${{ github.event.pull_request.number }}" > pr-number
- uses: actions/upload-artifact@v4
with:
message: |
Preview of the website obtained from the PR: ${{ steps.preview-pages.outputs.deployment-url }}
name: pr-number
path: pr-number
Loading