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
2variables:
3 CI: 'true'
4
5# Main job
6CI:
7 interruptible: true
8 only:
9 - main
10 - merge_requests
11 script:
12 # This enables task distribution via Nx Cloud
13 # Run this command as early as possible, before dependencies are installed
14 # Learn more at https://nx.dev/ci/reference/nx-cloud-cli#npx-nxcloud-startcirun
15 # Connect your workspace by running "nx connect" and uncomment this line to enable task distribution
16 # - npx nx start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="build"
17
18 - npm ci --legacy-peer-deps
19 - NX_HEAD=$CI_COMMIT_SHA
20 - NX_BASE=${CI_MERGE_REQUEST_DIFF_BASE_SHA:-$CI_COMMIT_BEFORE_SHA}
21
22 # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud
23 # - npx nx-cloud record -- echo Hello World
24 - npx nx affected -t lint test build
25 # Nx Cloud recommends fixes for failures to help you get CI green faster. Learn more: https://nx.dev/ci/features/self-healing-ci
26
27 after_script:
28 - npx nx fix-ci
29