How to Deploy a Netlify Preview With the CLI in GitHub Actions
Run netlify deploy without --prod to publish a draft preview, and parse the JSON output for the deploy_url.
A Netlify deploy without --prod creates a draft (preview) deploy with its own URL. Pass --json and read deploy_url with jq so downstream steps can use the address.
Steps
- Add
NETLIFY_AUTH_TOKENandNETLIFY_SITE_IDas secrets. - Build your site, then run
netlify deploy --dir=<out> --json. - Parse
deploy_urlfrom the JSON withjqinto a step output.
Workflow
.github/workflows/preview.yml
jobs:
preview:
runs-on: ubuntu-latest
env:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
steps:
- uses: actions/checkout@v4
- run: npm ci && npm run build
- id: deploy
run: |
npm i -g netlify-cli
OUT=$(netlify deploy --dir=dist --json)
echo "url=$(echo "$OUT" | jq -r '.deploy_url')" >> "$GITHUB_OUTPUT"Gotchas
- Only
--prodpublishes to the main URL; omit it to keep the deploy as a preview. - The draft
deploy_urlis unique per deploy, so capture it each run rather than hardcoding it.
Related guides
How to Create a Vercel Preview Deployment Per PR in GitHub ActionsDeploy a Vercel preview for each pull request from GitHub Actions using the vercel CLI, capturing the generat…
How to Post the Preview URL as a PR Comment in GitHub ActionsComment a deployed preview URL on the pull request from GitHub Actions with actions/github-script, updating a…