Next.js GitHub Actions Workflow Template
Last updated
This is a working .github/workflows/ci.yml for a Next.js app on npm. It caches npm downloads and the .next/cache build cache, then runs lint, a type check, tests and a production build. Edit the script names to match your package.json.
The workflow
name: CI
on:
push:
branches: [main]
pull_request:
permissions:
contents: read
concurrency:
group: ci-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
ci:
runs-on: ubuntu-latest
timeout-minutes: 20
env:
NEXT_TELEMETRY_DISABLED: "1"
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- name: Cache Next.js build cache
uses: actions/cache@v4
with:
path: .next/cache
key: nextjs-${{ runner.os }}-${{ hashFiles('package-lock.json') }}-${{ hashFiles('**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx', '!node_modules/**', '!.next/**') }}
restore-keys: |
nextjs-${{ runner.os }}-${{ hashFiles('package-lock.json') }}-
nextjs-${{ runner.os }}-
- run: npm ci
- run: npm run lint
- run: npx tsc --noEmit
- run: npm test --if-present
- run: npm run build
What the non-obvious parts do
cache: npmcaches npm's download folder, keyed on the lockfile. See caching npm.- The
.next/cachestep is the one most templates miss. Next.js stores compiled output and image optimisation results there. Without a restored cache every build starts cold, and Next.js prints a warning about it. The key combines the lockfile hash and a hash of source files, so a code change produces a new entry, whilerestore-keysfalls back to the most recent older one for an incremental build. npx tsc --noEmittype checks without writing files.next buildalso type checks, but running it first gives a faster, clearer failure.NEXT_TELEMETRY_DISABLEDswitches off anonymous telemetry in CI.concurrencycancels older runs on the same pull request. Main is never cancelled.timeout-minutescaps a stuck build.
Variants
Environment variables at build time
Variables prefixed with NEXT_PUBLIC_ are inlined at build. Pass them with env: on the build step and keep real secrets out of the client bundle.
End-to-end tests with Playwright
Add npx playwright install --with-deps chromium and npx playwright test. Shard it with a matrix; see test sharding and slow Playwright tests.
Monorepo with Turborepo
Run npx turbo run lint test build with TURBO_TOKEN and TURBO_TEAM set for a remote build cache. More in Nx and Turborepo CI.
Deploy job
Add a second job with needs: ci and if: github.ref == 'refs/heads/main'. Vercel users can skip this and let Vercel's Git integration deploy.
macOS
Rarely useful for a web app. Stay on Linux.
Run it on faster machines
Next.js builds are CPU hungry because SWC and type checking use several cores. Change one line:
runs-on: [self-hosted, Linux, compiler-ci]
That moves the job to an 8 vCPU machine from compiler.dev, with a fallback to your current runner. The full tuning list is in speed up Next.js on GitHub Actions.
FAQ
Why does my Next.js build warn about no cache?
The .next/cache folder was empty. Add the cache step above and the warning goes away on the next run that restores it.
Should I cache node_modules for Next.js?
No. Cache npm's downloads and let npm ci rebuild node_modules.
Can I use pnpm or Yarn?
Yes. Change cache: and the install command, and update the lockfile name in the key. See pnpm caching.
Do I need Docker for CI?
No. Build in the job. Use Docker only if you deploy a container; see Docker layer caching.
Related
- Speed Up Next.js in GitHub ActionsSpeed up Next.js builds in GitHub Actions with .next/cache, package manager caching, split lint and test jobs, and Playwright sharding. Working YAML.
- .NET GitHub Actions Workflow TemplateCopy-paste GitHub Actions CI for .NET with NuGet caching, restore, build and test steps, a SQL Server service option, concurrency and permissions.
- Django GitHub Actions Workflow TemplateCopy-paste GitHub Actions CI for Django with a Postgres service container, pip caching, migrations check and tests, plus concurrency and permissions.
- Docker Build GitHub Actions Workflow TemplateCopy-paste GitHub Actions workflow to build Docker images with Buildx, GitHub cache layers and a push to GHCR on main, with minimal permissions.