Set up automatic pull request preview deploys to Netlify
did:plc:zviscnpwyvj6y32agi5davn5 opened this Sep 10, 2025 0 comments
did:plc:zviscnpwyvj6y32agi5davn5 opened Sep 10, 2025
The way this is set up on GitHub right now is via Netlify's native integration, so we'll need to roll our own. Here's a sample pipeline that ChatGPT conjured:
# .tangled/workflows/deploy-preview.yml
when:
- event: ["pull_request"]
# 'branch' here is the PR's *base* branch filter (target)
branch: ["main"]
dependencies:
nixpkgs:
- nodejs
- git
steps:
- name: Install
command: |
npm ci
- name: Build (Astro)
command: |
npm run build
- name: Compute preview alias
# Create a stable alias like pr-<branch>-<shortsha>, sanitize slashes
command: |
BRANCH="$(git rev-parse --abbrev-ref HEAD | tr '/ _' '---')"
SHA="$(git rev-parse --short HEAD)"
echo "ALIAS=pr-${BRANCH}-${SHA}" >> $SPINDLE_ENV_FILE
- name: Deploy preview (draft)
environment:
# Pull the alias from the previous step
ALIAS: ${{ env.ALIAS }}
command: |
echo "Using alias: ${ALIAS}"
npx netlify-cli deploy \
--dir="dist" \
--alias="${ALIAS}" \
--message="PR preview ${ALIAS}" \
--site="$NETLIFY_SITE_ID" \
--auth="$NETLIFY_AUTH_TOKEN"No activity yet.