Configuring CI Using GitLab and Nx
Nx is a smart, fast and extensible build system, and it works really well with monorepos. Monorepos provide a lot of advantages:
- Everything at that current commit works together. Changes can be verified across all affected parts of the organization.
- Easy to split code into composable modules
- Easier dependency management
- One toolchain setup
- Code editors and IDEs are "workspace" aware
- Consistent developer experience
- And more ...
But they come with their own technical challenges. The more code you add into your repository, the slower the CI gets.
Setting Gitlab CI/CD
Below is an example of a GitLab pipeline setup for an Nx workspace only building and testing what is affected.
1image: node:16-alpine
2stages:
3 - setup
4 - test
5
6install-dependencies:
7 stage: setup
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
21.distributed:
22 interruptible: true
23 only:
24 - main
25 - merge_requests
26 needs:
27 - install-dependencies
28 artifacts:
29 paths:
30 - node_modules/.cache/nx
31
32workspace-lint:
33 stage: test
34 extends: .distributed
35 script:
36 - npx nx workspace-lint
37
38format-check:
39 stage: test
40 extends: .distributed
41 script:
42 - npx nx format:check
43
44lint:
45 stage: test
46 extends: .distributed
47 script:
48 - npx nx affected --base=HEAD~1 --target=lint --parallel=3
49
50test:
51 stage: test
52 extends: .distributed
53 script:
54 - npx nx affected --base=HEAD~1 --target=test --parallel=3 --ci --code-coverage
55
56build:
57 stage: test
58 extends: .distributed
59 script:
60 - npx nx affected --base=HEAD~1 --target=build --parallel=3
The build
and test
jobs implement the CI workflow using .distributed
as template to keep
CI configuration file clearly.
Distributed CI with Nx Cloud
A computation cache is created on your local machine to make the developer experience faster. This allows you to not waste time re-building, re-testing, re-linting, or any number of other actions you might take on code that hasn't changed. Because the cache is stored locally, you are the only member of your team that can take advantage of these instant commands. You can manage and share this cache manually.
Nx Cloud allows this cache to be shared across your entire organization, meaning that any cacheable operation completed on your workspace only needs to be run once. Nx Cloud also allows you to distribute your CI across multiple machines to make sure the CI is fast even for very large repos.
Learn more about configuring your CI environment using Nx Cloud with Distributed Caching and Distributed Task Execution in the Nx Cloud docs.