Skip to content
Open
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
61 changes: 58 additions & 3 deletions .github/workflows/firebase-hosting-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ name: Deploy to Firebase Hosting on PR

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
types: [opened, synchronize, reopened, closed]
types: [opened, synchronize, reopened]

Could skip the whole workflow when closing. Teardown is not supported by the official action, but found this where people shared their solution: FirebaseExtended/action-hosting-deploy#60

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If the expiration is for a preview specifically (not global), an alternative would be to have 30 days for all, and close sets it to 1 day so it cleans itself faster.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

oops, opus was hallucinating then.. :-P

branches:
- develop
- v3.x
Expand All @@ -17,16 +18,70 @@ permissions:

jobs:
build_and_preview:
if: '${{ github.event.pull_request.head.repo.full_name == github.repository }}'
if: >-
github.event.action != 'closed' &&
github.event.pull_request.head.repo.full_name == github.repository

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- run: npm ci && npm run predeploy && npm run typedoc
- uses: FirebaseExtended/action-hosting-deploy@v0
# A preview is only worth building when the deployed output can actually
# differ: the library, the demo it loads, or the deploy/docs config.
# Doc- and test-only PRs skip the whole build and deploy.
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
deployable:
- 'src/**'
- '!src/__tests__/**'
- 'demo/**'
- 'package.json'
- 'package-lock.json'
- 'rollup.config.mjs'
- 'firebase.json'
- 'typedoc.json'
- 'typedoc.css'
- '.github/workflows/firebase-hosting-pull-request.yml'
- if: steps.filter.outputs.deployable == 'true'
run: npm ci && npm run predeploy && npm run typedoc
- if: steps.filter.outputs.deployable == 'true'
uses: FirebaseExtended/action-hosting-deploy@v0
with:
repoToken: '${{ secrets.GITHUB_TOKEN }}'
expires: 30d
firebaseServiceAccount: '${{ secrets.FIREBASE_SERVICE_ACCOUNT_GCODE_PREVIEW }}'
projectId: gcode-preview

cleanup_preview:
if: >-
github.event.action == 'closed' &&
github.event.pull_request.head.repo.full_name == github.repository

runs-on: ubuntu-latest

steps:
# The deploy action has no teardown of its own
# (FirebaseExtended/action-hosting-deploy#60), so the channel is removed
# with the CLI. The action names its channel `pr<number>-<branch>` with
# the branch sanitized and truncated, so we match on the `pr<number>-`
# prefix rather than reproducing that truncation here.
- name: Delete the PR's preview channel
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
FIREBASE_SERVICE_ACCOUNT: ${{ secrets.FIREBASE_SERVICE_ACCOUNT_GCODE_PREVIEW }}
run: |
set -euo pipefail
echo "$FIREBASE_SERVICE_ACCOUNT" > "$RUNNER_TEMP/service-account.json"
export GOOGLE_APPLICATION_CREDENTIALS="$RUNNER_TEMP/service-account.json"
firebase() { npx --yes firebase-tools@14 "$@" --project gcode-preview; }
channel=$(firebase hosting:channel:list --json |
jq -r --arg prefix "pr${PR_NUMBER}-" \
'.result.channels[].name | split("/") | last | select(startswith($prefix))')
if [ -z "$channel" ]; then
echo "No preview channel for PR ${PR_NUMBER}; nothing to delete."
exit 0
fi
echo "Deleting preview channel $channel"
firebase hosting:channel:delete "$channel" --force
Loading