Configuring CI Using GitLab and Nx
Below is an example of an GitLab setup, building and testing only what is affected.
.gitlab-ci.yml
1image: node:18
2
3stages:
4 - lint
5 - test
6 - build
7
8.distributed:
9 interruptible: true
10 only:
11 - main
12 - merge_requests
13 cache:
14 key:
15 files:
16 - package-lock.json
17 paths:
18 - .npm/
19 before_script:
20 - npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" # this line enables distribution
21 - npm ci --cache .npm --prefer-offline
22 - NX_HEAD=$CI_COMMIT_SHA
23 - NX_BASE=${CI_MERGE_REQUEST_DIFF_BASE_SHA:-$CI_COMMIT_BEFORE_SHA}
24
25variables:
26 GIT_DEPTH: 0
27
28format-check:
29 stage: test
30 extends: .distributed
31 script:
32 - npx nx-cloud record -- nx format:check --base=$NX_BASE --head=$NX_HEAD
33
34lint:
35 stage: test
36 extends: .distributed
37 script:
38 - npx nx affected --base=$NX_BASE --head=$NX_HEAD -t lint --parallel=3
39
40test:
41 stage: test
42 extends: .distributed
43 script:
44 - npx nx affected --base=$NX_BASE --head=$NX_HEAD -t test --parallel=3
45
46build:
47 stage: build
48 extends: .distributed
49 script:
50 - npx nx affected --base=$NX_BASE --head=$NX_HEAD -t build --parallel=3
51