CI/CDTemplate3 min readUpdated September 2026

A Next.js CI/CD Pipeline: Stages, Checks and Deploy Gates

A solid Next.js CI/CD pipeline runs these stages in order: clean install, lint, type check, tests, production build, a preview deployment with a smoke test, then a gated promotion to production. Fast checks go first so a failing commit is rejected in minutes.

The outline below works on GitHub Actions or GitLab CI, since the stages are the same and only the syntax differs. It's an outline to adapt, not a file to paste.

Vendors Covered in this Article

Disclosure: We may earn a commission if you buy through some links on this page. It doesn't change what we recommend.

What stages should the pipeline have, and in what order?

Order stages by speed and by how cheaply they fail:

  1. Install: a clean install from the lockfile, with the dependency cache restored.
  2. Lint and format check: seconds to run, catches the most trivial problems.
  3. Type check: run the TypeScript compiler without emitting output.
  4. Unit and component tests: run in parallel with the type check if your runner has capacity.
  5. Build: the production build. Failures here often reveal server and client boundary mistakes that dev mode hides.
  6. Preview deploy: publish the build to a unique preview URL for the pull request.
  7. End-to-end smoke test: run a few critical flows against the preview URL, such as sign-in and the main page render.
  8. Promote: on merge to the main branch, deploy to production, then run a post-deploy check.

Steps 2 to 4 can run at the same time. Don't start preview deployments until the cheap checks pass, or you'll waste build minutes on commits that were already broken.

How do you make it fast?

Slow pipelines get bypassed, so treat speed as a feature. The main levers:

  • Cache the package manager's download cache, keyed on the lockfile hash.
  • Cache the Next.js build cache between runs, so unchanged pages don't rebuild.
  • Run independent jobs in parallel and fail fast on the first error.
  • Cancel superseded runs on the same branch when a new commit arrives.
  • Quarantine flaky tests: move a test that fails intermittently out of the blocking path, file a ticket and fix it within days, since one flaky test teaches everyone to re-run instead of investigate.
  • Split slow end-to-end tests into a smaller pull request suite and a fuller nightly one.

Say your pipeline takes 14 minutes and engineers batch changes to avoid waiting. Cutting it to 6 minutes changes how often people integrate, which matters more than the minutes saved. DORA's 2024 report puts its highest-performing cluster on demand for deploy frequency, meaning no more than 1 day between deploys1.

How should you handle environment variables and secrets?

Next.js treats variables prefixed for the browser differently from server-only ones, and that difference is a common leak. Follow these rules:

  • Anything in a browser-exposed variable is public. Never put a secret key there.
  • Store secrets in the CI platform's encrypted secret store, scoped to the environments that need them.
  • Use separate values for preview and production, and never let a preview build talk to the production database.
  • Fail the build if a required variable is missing, using a small validation module at startup.
  • Give forks and untrusted pull requests no access to secrets.

Also decide which variables are read at build time and which at run time, since a value baked into a build can't change without a rebuild.

Which gates protect production?

Set branch protection so the main branch requires passing checks and at least one review, and disallow direct pushes. For production, choose an approach that matches your risk. A small team with good tests can deploy every merge automatically. A team with regulated changes may add a manual approval on the production job.

Whatever you pick, add a post-deploy smoke check and an automatic alert on error rate. Keep the previous build available for a one-step rollback, and rehearse it once. Change failure rate is the number to watch: in DORA's 2024 clusters, the highest-performing group reported a change failure rate of about 5%2. Pair the pipeline with a written release checklist covering what to verify around each deploy.

GitHub Actions or GitLab CI: which should you write this in?

Use whichever platform already hosts your code. The stages, caching and secret handling above are identical. Choose GitHub Actions if you're on GitHub and want its marketplace of reusable steps, and GitLab CI if you're on GitLab and want pipelines, environments and the container registry in one place. Don't migrate your source host just for CI. If you're weighing the two anyway, the CI platform comparison goes through the tradeoffs.

Executive Capability Standard

What Good Looks Like

Every pull request runs lint, type check, tests and a build in under ten minutes, and merges to main deploy through a gated, reversible path.

Building The Capability (5-Stage Skill Ladder)

1. Learn:Learn the difference between build-time and run-time variables and how Next.js separates server and browser code.
2. Do Manually:Write down your current build and release steps, time each one and find the slowest.
3. Delegate:Assign one engineer to own the pipeline configuration, caching and flaky test triage.
4. Automate:Add caching, parallel jobs, preview deployments with smoke tests and automatic post-deploy checks.
5. Buy:Use hosted runners or a managed CI platform rather than running your own build servers.

How to Get Started

Disclosure: We may earn a commission if you buy through some links on this page. It doesn't change what we recommend.

GitHub Actions

Fits when your repositories are on GitHub and you want workflows defined next to the code.

Visit GitHub Actions→
GitLab CI

Fits when your code is on GitLab and you want pipelines, environments and a registry in one system.

Visit GitLab CI→

Frequently Asked Questions

How long should a Next.js CI pipeline take?

Aim for under ten minutes for the pull request checks. If it runs longer, engineers start batching changes. Cache dependencies and the build output, run jobs in parallel, and move slow end-to-end tests to a nightly run.

Should you run tests before or after the build?

Run fast checks first: lint, type check and unit tests. Then build. The build is slower, and running it after cheap checks means obviously broken commits fail early without spending build time.

Do you need preview deployments?

They're worth it for most teams. A preview URL lets reviewers and testers see the change and lets you run smoke tests before merge. Just keep preview environments isolated from production data.

How do you roll back a bad Next.js deploy?

Keep the last known good build and redeploy it. Practice the rollback ahead of time, and make sure database migrations are backward compatible so the old build can still run against the current schema.

Sources

Where we quote a benchmark, we show its source. Other figures in this guide are estimates or general guidance, so check them against your own numbers.

  1. Deployment frequency by DORA performance cluster (max days between deploys). DORA Accelerate State of DevOps 2024 (Google Cloud), cluster table via Octopus Deploy analysis, 2024.
  2. Change failure rate by DORA performance cluster. DORA Accelerate State of DevOps 2024 (Google Cloud), cluster table via Octopus Deploy analysis, 2024.

Related Guides