Configuring CI Using GitLab and Nx
Below is an example of a GitLab pipeline setup for an Nx workspace only building and testing what is affected.
1image: node:16
2
3stages:
4 - test
5 - build
6
7.distributed:
8 interruptible: true
9 only:
10 - main
11 - merge_requests
12 cache:
13 key:
14 files:
15 - package-lock.json
16 paths:
17 - .npm/
18 before_script:
19 - npm ci --cache .npm --prefer-offline
20 - NX_HEAD=$CI_COMMIT_SHA
21 - NX_BASE=${CI_MERGE_REQUEST_DIFF_BASE_SHA:-$CI_COMMIT_BEFORE_SHA}
22 artifacts:
23 paths:
24 - node_modules/.cache/nx
25
26workspace-lint:
27 stage: test
28 extends: .distributed
29 script:
30 - npx nx workspace-lint --base=$NX_BASE --head=$NX_HEAD
31
32format-check:
33 stage: test
34 extends: .distributed
35 script:
36 - npx nx format:check --base=$NX_BASE --head=$NX_HEAD
37
38lint:
39 stage: test
40 extends: .distributed
41 script:
42 - npx nx affected --base=$NX_BASE --head=$NX_HEAD --target=lint --parallel=3
43
44test:
45 stage: test
46 extends: .distributed
47 script:
48 - npx nx affected --base=$NX_BASE --head=$NX_HEAD --target=test --parallel=3 --ci --code-coverage
49
50build:
51 stage: build
52 extends: .distributed
53 script:
54 - npx nx affected --base=$NX_BASE --head=$NX_HEAD --target=build --parallel=3
55
The build
and test
jobs implement the CI workflow using .distributed
as template to keep CI configuration file more readable.
Distributed CI with Nx Cloud
In order to use distributed task execution, we need to start agents and set the NX_CLOUD_DISTRIBUTED_EXECUTION
flag to true
.
Read more about the Distributed CI setup with Nx Cloud.
1image: node:18
2variables:
3 CI: 'true'
4 NX_CLOUD_DISTRIBUTED_EXECUTION: 'true'
5
6# Creating template for DTE agents
7.dte-agent:
8 interruptible: true
9 cache:
10 key:
11 files:
12 - yarn.lock
13 paths:
14 - '.yarn-cache/'
15 script:
16 - yarn install --cache-folder .yarn-cache --prefer-offline --frozen-lockfile
17 - yarn nx-cloud start-agent
18 artifacts:
19 expire_in: 5 days
20 paths:
21 - dist
22
23# Creating template for a job running DTE (orchestrator)
24.base-pipeline:
25 interruptible: true
26 only:
27 - main
28 - merge_requests
29 cache:
30 key:
31 files:
32 - yarn.lock
33 paths:
34 - '.yarn-cache/'
35 before_script:
36 - yarn install --cache-folder .yarn-cache --prefer-offline --frozen-lockfile
37 - NX_HEAD=$CI_COMMIT_SHA
38 - NX_BASE=${CI_MERGE_REQUEST_DIFF_BASE_SHA:-$CI_COMMIT_BEFORE_SHA}
39 artifacts:
40 expire_in: 5 days
41 paths:
42 - node_modules/.cache/nx
43
44# Main job running DTE
45nx-dte:
46 stage: affected
47 extends: .base-pipeline
48 script:
49 - yarn nx-cloud start-ci-run
50 - yarn nx-cloud record -- yarn nx workspace-lint --base=$NX_BASE --head=$NX_HEAD
51 - yarn nx-cloud record -- yarn nx format:check --base=$NX_BASE --head=$NX_HEAD
52 - yarn nx affected --base=$NX_BASE --head=$NX_HEAD --target=lint --parallel=3
53 - yarn nx affected --base=$NX_BASE --head=$NX_HEAD --target=test --parallel=3 --ci --code-coverage
54 - yarn nx affected --base=$NX_BASE --head=$NX_HEAD --target=e2e --parallel=3 --ci --code-coverage
55 - yarn nx affected --base=$NX_BASE --head=$NX_HEAD --target=build --parallel=3
56 - yarn nx-cloud stop-all-agents
57
58# Create as many agents as you want
59nx-dte-agent1:
60 extends: .dte-agent
61 stage: affected
62nx-dte-agent2:
63 extends: .dte-agent
64 stage: affected
65nx-dte-agent3:
66 extends: .dte-agent
67 stage: affected
68