Configuring CI Using GitLab and Nx
Below is an example of a GitLab setup, building and testing only what is affected.
.gitlab-ci.yml
1image: node:20
2
3variables:
4  GIT_DEPTH: 0
5
6main:
7  interruptible: true
8  only:
9    - main
10    - merge_requests
11  cache:
12    key:
13      files:
14        - package-lock.json
15    paths:
16      - .npm/
17  script:
18    # Connect your workspace on <%= nxCloudHost %> and uncomment this to enable task distribution.
19    # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "e2e-ci" targets have been requested
20    # - npx nx-cloud start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="e2e-ci"
21
22    - npm ci --cache .npm --prefer-offline
23    - NX_HEAD=$CI_COMMIT_SHA
24    - NX_BASE=${CI_MERGE_REQUEST_DIFF_BASE_SHA:-$CI_COMMIT_BEFORE_SHA}
25
26    # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud
27    # This requires connecting your workspace to Nx Cloud. Run "nx connect" to get started w/ Nx Cloud
28    # - npx nx-cloud record -- nx format:check
29
30    # Without Nx Cloud, run format:check directly
31    - npx nx format:check --base=$NX_BASE --head=$NX_HEAD
32    - npx nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build e2e-ci
33