Skip to content
Latchkey

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_TOKEN and NETLIFY_SITE_ID as secrets.
  • Build your site, then run netlify deploy --dir=<out> --json.
  • Parse deploy_url from the JSON with jq into 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 --prod publishes to the main URL; omit it to keep the deploy as a preview.
  • The draft deploy_url is unique per deploy, so capture it each run rather than hardcoding it.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →