Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
---
name: Preview
on:
pull_request:
types: [opened, synchronize, reopened, labeled, unlabeled, closed]
# A label other than deploy-preview does nothing here. It gets a group of its own, so it cannot
# cancel a preview build that is running.
concurrency:
group: >-
preview-${{ github.event.number }}${{ (github.event.action == 'labeled' || github.event.action == 'unlabeled')
&& github.event.label.name != 'deploy-preview' && format('-{0}', github.run_id) || '' }}
cancel-in-progress: true
jobs:
gate:
# A preview publishes this PR's own HTML and JS on homeracker.org, same-origin with the live
# site, so a fork must never reach that path. The repository's fork-approval setting only
# holds back first-time contributors and is not a sufficient gate on its own.
if: >-
github.event.pull_request.head.repo.full_name == github.repository
&& github.event.action != 'closed'
&& github.event.action != 'unlabeled'
&& (github.event.action != 'labeled' || github.event.label.name == 'deploy-preview')
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: read
outputs:
build: ${{ steps.decide.outputs.build }}
steps:
- name: Decide whether this PR gets a preview
id: decide
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
PR: ${{ github.event.number }}
LABELLED: ${{ contains(github.event.pull_request.labels.*.name, 'deploy-preview') }}
run: |
set -euo pipefail
if gh pr diff "$PR" --repo "$REPO" --name-only | grep -qE '^(site|configurator)/'; then
echo "build=true" >> "$GITHUB_OUTPUT"
else
echo "build=$LABELLED" >> "$GITHUB_OUTPUT"
fi
build:
needs: gate
if: needs.gate.outputs.build == 'true'
runs-on: ubuntu-latest
permissions:
contents: read
actions: write
steps:
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Build site
uses: ./.github/actions/build-site
with:
base: /preview/pr-${{ github.event.number }}/
- name: Upload preview
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: preview-pr-${{ github.event.number }}
path: site/dist
retention-days: 14
# Pages serves one artifact for the whole domain, so publishing a preview means
# redeploying the site with every live preview collected into it. The dispatch carries no
# inputs: it runs main's workflow, which finds this artifact by name on its own.
- name: Trigger the Pages deployment
env:
GH_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
gh workflow run pages.yml \
--repo "${{ github.repository }}" \
--ref main
# Closing a PR or dropping its label leaves its preview published until something redeploys.
# pages.yml re-evaluates every artifact on each run, so a deployment is all the pruning takes.
#
# A merge is excluded: it pushes to main, which deploys anyway. Dispatching here as well would
# land seconds later and cancel that push run under the shared `pages` concurrency group,
# marking the merge commit as failed even though the site deployed fine.
prune:
if: >-
github.event.pull_request.head.repo.full_name == github.repository
&& ((github.event.action == 'closed' && !github.event.pull_request.merged)
|| (github.event.action == 'unlabeled' && github.event.label.name == 'deploy-preview'))
runs-on: ubuntu-latest
permissions:
actions: write
steps:
- name: Redeploy only if this PR had a preview
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
PR: ${{ github.event.number }}
run: |
set -euo pipefail
found=$(gh api --paginate "repos/$REPO/actions/artifacts" \
--jq "[.artifacts[] | select(.expired == false) | select(.name == \"preview-pr-$PR\")] | length")
if [ "$found" -eq 0 ]; then
echo "no preview artifact for pr-$PR, nothing to prune"
exit 0
fi
gh workflow run pages.yml --repo "$REPO" --ref main
...
e6ec7bab9This file is pinned to the selected release. Download the original to open it in your design tool.