Custom Distributed Task Execution on Circle CI
Using Nx Agents is the easiest way to distribute task execution, but it your organization may not be able to use hosted Nx Agents. With an enterprise license, you can set up distributed task execution on your own CI provider using the recipe below.
Run Custom Agents on Circle CI
Run agents directly on Circle CI with the workflow below:
1version: 2.1
2orbs:
3 nx: nrwl/nx@1.5.1
4jobs:
5 main:
6 docker:
7 - image: cimg/node:lts-browsers
8 environment:
9 NX_CLOUD_DISTRIBUTED_EXECUTION_AGENT_COUNT: 3 # expected number of agents
10 steps:
11 - checkout
12 - run: npm ci
13 - nx/set-shas
14
15 # Tell Nx Cloud to use DTE and stop agents when the e2e-ci tasks are done
16 - run: npx nx-cloud start-ci-run --distribute-on="manual" --stop-agents-after=e2e-ci
17 # Send logs to Nx Cloud for any CLI command
18 - run: npx nx-cloud record -- nx format:check
19 # Lint, test, build and run e2e on agent jobs for everything affected by a change
20 - run: npx nx affected --base=$NX_BASE --head=$NX_HEAD -t lint,test,build,e2e-ci --parallel=2 --configuration=ci
21 agent:
22 docker:
23 - image: cimg/node:lts-browsers
24 parameters:
25 ordinal:
26 type: integer
27 steps:
28 - checkout
29 - run: npm ci
30 # Wait for instructions from Nx Cloud
31 - run:
32 command: npx nx-cloud start-agent
33 no_output_timeout: 60m
34workflows:
35 build:
36 jobs:
37 - agent:
38 matrix:
39 parameters:
40 ordinal: [1, 2, 3]
41 - main
42
This configuration is setting up two types of jobs - a main job and three agent jobs.
The main job tells Nx Cloud to use DTE and then runs normal Nx commands as if this were a single pipeline set up. Once the commands are done, it notifies Nx Cloud to stop the agent jobs.
The agent jobs set up the repo and then wait for Nx Cloud to assign them tasks.
Two Types of ParallelizationThe ordinal: [1, 2, 3]
line and the --parallel
flag both parallelize tasks, but in different ways. The way this workflow is written, there will be 3 agents running tasks and each agent will try to run 2 tasks at once. If a particular CI run only has 2 tasks, only one agent will be used.