Deploying from a laptop works until the day it does not: someone deploys an unmerged branch, or forgets to build first. A small pipeline removes the manual step and gives every pull request its own preview URL. This is the setup for a static site on Firebase Hosting.

What the pipeline does

  • On every pull request: install, lint, build, and publish to a temporary preview channel. The URL is posted as a comment on the pull request.
  • On every push to main: the same checks, then deploy to the live site.

Credentials

The workflow needs permission to deploy. Create a service account for the Firebase project with the Hosting admin role, download its JSON key, and store the whole JSON as a repository secret named FIREBASE_SERVICE_ACCOUNT. Running firebase init hosting:github does these steps for you and writes starter workflow files.

The key never appears in the repository. GitHub injects it at run time and masks it in logs.

Preview on pull requests

# .github/workflows/preview.yml
name: Preview
on: pull_request

permissions:
  contents: read
  pull-requests: write
  checks: write

jobs:
  preview:
    if: github.event.pull_request.head.repo.full_name == github.repository
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
      - run: npm ci
      - run: npm run lint
      - run: npm run build
      - uses: FirebaseExtended/action-hosting-deploy@v0
        with:
          repoToken: ${{ secrets.GITHUB_TOKEN }}
          firebaseServiceAccount: ${{ secrets.FIREBASE_SERVICE_ACCOUNT }}
          projectId: my-project-id
          expires: 7d

Without a channelId, the action creates a preview channel named after the pull request and comments with its URL. expires removes the channel after a week.

The if line skips pull requests from forks. Forks do not receive secrets, so the deploy would fail, and you would not want unreviewed code deployed under your project anyway.

Production on main

# .github/workflows/deploy.yml
name: Deploy
on:
  push:
    branches: [main]

concurrency:
  group: production
  cancel-in-progress: false

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
      - run: npm ci
      - run: npm run lint
      - run: npm run build
      - uses: FirebaseExtended/action-hosting-deploy@v0
        with:
          repoToken: ${{ secrets.GITHUB_TOKEN }}
          firebaseServiceAccount: ${{ secrets.FIREBASE_SERVICE_ACCOUNT }}
          projectId: my-project-id
          channelId: live

channelId: live targets the real site. The concurrency block makes deploys run one at a time in order, so two quick merges cannot finish out of sequence.

Details worth getting right

  • npm ci installs exactly what the lockfile specifies and fails if it is out of date.
  • cache: npm on setup-node restores the download cache between runs.
  • Least privilege. The permissions block limits what the workflow token can do to what the job needs.
  • Protect main. Require the preview workflow to pass before merging, so nothing reaches production without a green build.
  • Rolling back is a click in the Firebase console: Hosting keeps previous releases and can restore any of them.

What you get

Reviewers open a real URL instead of pulling a branch. Production always matches main. And a deploy is the same sequence of commands every time, run by a machine that does not skip steps.