Speed up your CI by caching Nx generated metadata
Nx generates metadata files—referred to as workspace data—on initialization, which it uses to intelligently run commands and integrate with plugins. Large monorepos often contain numerous projects with complex relationships, making metadata generation more time-consuming.
Updating existing workspace data is faster than creating it from scratch. To avoid regenerating this data for each CI run, Nx Cloud supports workspace data caching. By caching workspace data from previous runs and reusing it in future runs, we can reduce the time needed to prepare the workspace.
How does it work?
When in a CI context, Nx Cloud will automatically cache the contents of your workspace data and upload it to our remote cache. This data will only be uploaded when you run CI on your workspaces default branch.
To leverage your cached workspace data, you can use the following command while authenticated with read-write
access:
1npx nx-cloud get-workspace-data
2
This will download your cached workspace data to your workspace.
What does this look like in practice?
The following is an example of a CI configuration that uses the get-workspace-data
command to download the workspace data:
1name: Nx Cloud - CI
2on:
3 push:
4 branches:
5 - main
6 pull_request:
7 branches:
8 - main
9jobs:
10 build:
11 runs-on: ubuntu-latest
12 steps:
13 - uses: actions/checkout@v3
14 - uses: actions/setup-node@v3
15 with:
16 node-version: 18
17 - run: npx nx-cloud@latest start-ci-run
18
19 # Invoke this command before your first nx command
20 - run: npx nx-cloud get-workspace-data
21 - run: npx nx affected
22