Skip to content

feat: add PR preview deployments to GitHub Pages#16

Merged
reneaaron merged 4 commits into
getAlby:mainfrom
hermes-alby:feat/pr-preview-deploy
Apr 10, 2026
Merged

feat: add PR preview deployments to GitHub Pages#16
reneaaron merged 4 commits into
getAlby:mainfrom
hermes-alby:feat/pr-preview-deploy

Conversation

@hermes-alby
Copy link
Copy Markdown
Contributor

What

Every pull request now gets a live preview deployed to GitHub Pages at a URL like:

https://getalby.github.io/bitcoin-apps/pr-preview/pr-15/

Previews are automatically updated on each push and removed when the PR is closed.

How it works

  1. New workflow .github/workflows/pr-preview.yml — triggers on PR open/sync/close, builds the site with a subdirectory base path, and deploys to the gh-pages branch under pr-preview/pr-N/ using rossjrw/pr-preview-action

  2. Updated workflow .github/workflows/deploy-pages.yml — switched from the GitHub Pages API (deploy-pages action) to the gh-pages branch approach (peaceiris/actions-gh-pages) so both the main site and PR previews coexist on the same branch. Uses keep_dirs: pr-preview to avoid wiping active previews on main deploys.

  3. Updated vite.config.ts — reads VITE_BASE_URL env var to set <base href> correctly when deployed to a subdirectory (e.g. /pr-preview/pr-15/)

Setup required

After merging, GitHub Pages needs to be configured to serve from the gh-pages branch (root):

  1. Go to Settings → Pages
  2. Set Source to Deploy from a branch
  3. Select gh-pages branch, / (root) directory
  4. Save

The first deploy will create the gh-pages branch automatically.

Files changed

  • .github/workflows/pr-preview.yml — new PR preview workflow
  • .github/workflows/deploy-pages.yml — switch to gh-pages branch deployment
  • vite.config.ts — support VITE_BASE_URL for subdirectory deploys

Add a 'PR Preview' workflow that deploys every pull request to a live
preview URL on GitHub Pages (e.g. /pr-preview/pr-15/). Previews are
automatically updated on push and removed when the PR is closed.

Switch the main deploy workflow from the GitHub Pages API to the
gh-pages branch approach so both the main site and PR previews
coexist on the same branch. The main deploy preserves the pr-preview/
directory when pushing to avoid wiping active previews.

Also update vite.config.ts to support a VITE_BASE_URL env var for
correct asset paths when deployed to a subdirectory.
The GITHUB_TOKEN is read-only for PRs from forks, so the deploy
action gets a 403 when trying to push to gh-pages. Switch to
pull_request_target which runs in the base repo context with
write permissions. Explicitly checkout the PR head SHA to build
the correct code.
pull_request_target only works if the workflow file already exists on
the base branch — since this is a new workflow, GitHub can't discover
it with pull_request_target. Revert to pull_request trigger.

The 403 permission error requires a one-time repo settings change:
Settings → Actions → General → Workflow permissions → Read and write
GitHub always restricts GITHUB_TOKEN to read-only for the upstream
repo on fork PRs, even with 'Read and write' workflow permissions.
pull_request_target runs in the base repo context with write access.

Note: this PR itself won't trigger a preview because pull_request_target
reads the workflow file from the base branch — it won't exist on main
until after merging. All subsequent PRs will get previews automatically.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants