Distribute Task Execution (Nx Agents)
Nx Agents let you distribute your CI across many machines with minimal configuration. It comes with features such as dynamically allocating agents based on the size of the PR, flaky task re-running, and intelligent task splitting and distribution. Keep reading to learn more.
Making a Distributed CI Pipeline Is Hard
The only way to speed up your CI pipeline while still running all the necessary tasks is to distribute those tasks across multiple machines. Unfortunately, doing distribution right is hard to set up and hard to maintain. These are just some concerns you have to account for:
- Choose how many machines to set up
- Set up each machine so that it is ready to execute tasks
- Ensure that tasks are run in the correct order
- Copy the output of certain tasks to the machines where those outputs are needed
- Shut down machines when there are no more tasks to run
- Shut down all the machines when the whole pipeline hits an error
- Make sure sensitive information is being handled securely on all machines
And each of these concerns will need to be reconsidered whenever the codebase changes. It would actually be best if they were reconsidered for every PR, because small PRs may not need as much distribution as large PRs.
Nx Agents Make Distributing Tasks Simple
Nx Agents take care of all these concerns with a small initial configuration that does not need to be modified as your codebase changes. Your CI pipeline sends your tasks to be run on agent machines that Nx Cloud creates for you. All you need to do is specify how many agents and the type of agent. Then, when the pipeline is finished, your initial CI pipeline will contain all the logs and artifacts as if the tasks all ran on your main CI machine - but completed in a fraction of the time.
For a more thorough explanation of how Nx Agents optimizes your CI pipeline, read this guide to parallelization and distribution in CI.
Enable Nx Agents
To enable task distribution with Nx Agents, there are two requirements:
- Enable version control system integration. The integrations currently available are GitHub, GitLab and Bitbucket. These integrations can be enabled from your Nx Cloud dashboard.
- Add a single line to your CI pipeline configuration.
Add the start-ci-run
command to your CI pipeline configuration after checking out the repository and before installing node_modules
:
1# After checkout repository
2- name: Start CI run
3 run: 'npx nx-cloud start-ci-run --distribute-on="8 linux-medium-js" --stop-agents-after="e2e-ci"'
4# Before install node_modules
5# Run any nx commands as if running on a single machine
6
The --distribute-on
flag instructs Nx Cloud to distribute tasks across 8 agents of type linux-medium-js
. linux-medium-js
is the name of the launch template that will be used to provision the agent. The default launch templates can be found here
Launch Templates
You can also define your own "launch templates" (here's an example from the Nx repo):
1launch-templates:
2 linux-medium:
3 resource-class: 'docker_linux_amd64/medium+'
4 init-steps:
5 - name: Pnpm Install
6 script: |
7 pnpm install --frozen-lockfile
8
9 - name: Install Cypress
10 script: pnpm exec cypress install
11
12 - name: Install Rust
13 - ...
14
Here are the available resource classes.
Related Features
Assign a different number of agents to a pipeline based on the size of the PR
Split large e2e tasks into separate tasks for each spec file
Re-run flaky tasks in CI whenever they fail
CI/CD Guides
Every organization manages their CI/CD pipelines differently, so the guides don't cover org-specific aspects of CI/CD (e.g., deployment). They mainly focus on configuring Nx correctly using Nx Agents and Nx Replay.
Note that only cacheable operations can be distributed because they have to be replayed on the main job.