Example GitLab CI/CD pipeline for Next.js
This is a ready-to-use GitLab CI configuration for a typical Next.js project. The YAML block below is left unchanged — it shows a basic workflow with an installation step, a build step and a parallel job. Read the short notes after the example to understand each section and adapt it to your project.
yaml
1image: node:latest2
3stages:4 - install5 - build6 - parallel7
8cache:9 key: ${CI_COMMIT_REF_SLUG}10 paths:11 - node_modules/12
13install-deps:14 stage: install15 script:16 - npm install17
18build-site:19 stage: build20 script:21 - npm run build22 dependencies:23 - install-deps24 artifacts:25 paths:26 - .next/27 - out/ # include if you're using `next export`28 expire_in: 1 hour29
30parallel-job:31 stage: parallel32 script:33 - echo "I'm running at the same time as build-site!"Notes:
- image: The base Node image used for jobs; change the tag to match your Node.js version.
- stages: Defines the pipeline stages order (install → build → parallel).
- cache: Speeds up npm installs by persisting node_modules between runs.
- install-deps: Installs project dependencies (runs npm install).
- build-site: Runs the Next.js build (npm run build), saves .next/ and out/ as artifacts for later jobs.
- parallel-job: Example of a job that runs in the same pipeline stage as others (demonstrates parallelism).
Adapt the scripts, artifacts and cache to fit your repository layout and CI goals. Keep the YAML block above unchanged when copying into .gitlab-ci.yml.