Deploy Next.js on Google Cloud with GitHub Actions

▲ Next.js 🌐 Google Cloud Platform ⚙️ GitHub Actions

Configuration Files

Production-ready configuration files with detailed comments and best practices. Each file works together as a complete deployment solution.
name: Deploy Next.js to Google Cloud Storage + CDN

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Build Next.js (static export)
        run: npm run build
        env:
          NODE_ENV: production

      - name: Authenticate to Google Cloud
        uses: google-github-actions/auth@v2
        with:
          credentials_json: ${{ secrets.GCP_SA_KEY }}

      - name: Set up Cloud SDK
        uses: google-github-actions/setup-gcloud@v2

      - name: Upload to Cloud Storage
        run: |
          gsutil -m rsync -r -d out/ gs://${{ secrets.GCS_BUCKET }}

      - name: Invalidate CDN cache
        run: |
          gcloud compute url-maps invalidate-cdn-cache ${{ secrets.URL_MAP_NAME }} --path "/*"

Prerequisites

  • Google Cloud Platform account
  • GCS bucket created
  • Cloud CDN configured
  • Service account with proper permissions
  • Next.js configured for static export

Deployment Steps

  • Create GCS bucket with website configuration
  • Set up Cloud CDN with load balancer
  • Create service account with Storage Admin role
  • Download service account JSON key
  • Add GCP_SA_KEY to GitHub Secrets
  • Configure Next.js for static export
  • Deploy via GitHub Actions

Additional Notes

  • GCP offers sustained use discounts
  • Cloud CDN has global presence
  • Pay only for what you use
  • Integrates well with other GCP services