Streamlining Next.js Deployment to GitHub Pages with GitHub Actions: A Development Dashboard Insight

Deploying a Next.js application to GitHub Pages might seem straightforward, but unlike a simple HTML site, Next.js requires a bit more configuration. This is a common challenge for developers new to static site generation and automated deployments. Our community recently tackled this exact question, providing a comprehensive guide to get your Next.js project live on GitHub Pages using static export and GitHub Actions.

Developer setting up a CI/CD pipeline for web deployment
Developer setting up a CI/CD pipeline for web deployment

The Challenge: Next.js and Static Hosting

Next.js is a powerful React framework known for its server-side rendering (SSR) capabilities, which don't inherently align with the static-only nature of GitHub Pages. Simply pushing your Next.js code won't work because GitHub Pages expects pre-built static files (HTML, CSS, JS). The solution lies in telling Next.js to generate these static files (a process called static export) and then automating the build and deployment with GitHub Actions.

Development dashboard showing successful CI/CD workflow
Development dashboard showing successful CI/CD workflow

Step-by-Step Deployment Guide

Here’s a detailed walkthrough, combining the best practices shared by our community experts, Code-Crafter09 and andraokta:

1. Configure Next.js for Static Export

First, you need to modify your Next.js configuration to output static files. Open your next.config.js (or next.config.mjs) file at the root of your project and add the output: 'export' property. If you're using next/image, you'll also need to disable default image optimization for static export.

const nextC
  output: 'export',
  images: {
    unoptimized: true
  },
  // If your GitHub Pages URL includes a base path (e.g., 'your-username.github.io/your-repo-name'),
  // you'll need to configure basePath and assetPrefix.
  // For example, if your repo is 'my-nextjs-app' and your URL is 'https://username.github.io/my-nextjs-app',
  // basePath: '/my-nextjs-app',
  // assetPrefix: '/my-nextjs-app/',
};

module.exports = nextConfig;

Note: After this configuration, running npx next build will generate your static site in an out folder.

2. Update GitHub Repository Settings

Before setting up the workflow, tell GitHub Pages to expect deployments from GitHub Actions:

  1. Go to your repository on GitHub.
  2. Click on the Settings tab.
  3. On the left sidebar, click on Pages.
  4. Under Build and deployment, change the Source dropdown from "Deploy from a branch" to GitHub Actions.

3. Create the GitHub Actions Workflow

This automated script will build and deploy your static Next.js site. Create a new folder path .github/workflows/ in your project root, and inside it, create a file named deploy.yml (or similar).

name: Deploy Next.js site to Pages

on:
  push:
    branches: ["main"] # Ensure this matches your default branch name
  workflow_dispatch: # Allows you to run this workflow manually from the Actions tab

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
  contents: read
  pages: write
  id-token: write

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
concurrency:
  group: "pages"
  cancel-in-progress: false

jobs:
  # Build job
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: "20" # You can change this to match your local Node version
      - name: Install dependencies
        run: npm ci # Use 'yarn install' or 'pnpm install' if you don't use npm
      - name: Build with Next.js
        run: npx next build
      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: ./out # Next.js exports static files to the 'out' directory by default

  # Deployment job
  deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4

4. Push, Monitor, and Deploy

Commit your changes (next.config.js and .github/workflows/deploy.yml) and push them to your main branch. Then, navigate to the Actions tab in your GitHub repository. Here, you'll find a clear example of a development dashboard, showing the status of your workflow. Once the workflow successfully completes (turns green), return to your repository's Settings > Pages to find the live URL of your Next.js site.

This streamlined approach ensures your Next.js application is not only deployed efficiently but also automatically updated with every push, leveraging the power of GitHub's CI/CD capabilities for a seamless developer experience.

Track, Analyze and Optimize Your Software DeveEx!

Effortlessly implement gamification, pre-generated performance reviews and retrospective, work quality analytics, alerts on top of your code repository activity

 Install GitHub App to Start
devActivity Screenshot