Configuring CI

Circle CI

The following is an example of a Circle CI setup for an Nx workspace connected to Nx Cloud.

version: 2.1 jobs: agent: steps: - checkout - run: npm install - run: command: npx nx-cloud start-agent no_output_timeout: 60m main: environment: NX_CLOUD_DISTRIBUTED_EXECUTION: 'true' steps: - checkout - run: npm install - run: npx nx affected --base=HEAD~1 --target=build --parallel --max-parallel=3 - run: npx nx affected --base=HEAD~1 --target=test --parallel --max-parallel=2 - run: npx nx-cloud stop-all-agents pr: environment: NX_CLOUD_DISTRIBUTED_EXECUTION: 'true' steps: - checkout - run: npm install - run: npx nx affected --base=origin/main --target=build --parallel --max-parallel=3 - run: npx nx affected --base=origin/main --target=test --parallel --max-parallel=2 - run: npx nx-cloud stop-all-agents workflows: build: jobs: - agent: name: 'agent1' - agent: name: 'agent2' - agent: name: 'agent3' - main: filters: branches: only: main - pr: filters: branches: ignore: main

The pr and main jobs implement the CI workflow. The 3 agent jobs execute the tasks created by pr and main. Setting no_output_timeout is needed only if you have very slow tasks. Note, we aren't setting NX_BRANCH and NX_RUN_GROUP. The Nx Cloud runner is able to deduce them from the context provided by Nx Cloud.

GitHub Actions

The following is an example of a GitHub Actions setup for an Nx workspace connected to Nx Cloud.

name: CI on: push: branches: - main pull_request: env: NX_BRANCH: ${{ github.event.number || github.ref }} NX_RUN_GROUP: ${{ github.run_id }} NX_CLOUD_DISTRIBUTED_EXECUTION: true jobs: main: runs-on: ubuntu-latest if: ${{ github.event_name != 'pull_request' }} steps: - uses: actions/checkout@v2 name: Checkout [main] with: fetch-depth: 0 - uses: actions/setup-node@v1 - run: npm install - run: npx nx affected --base=HEAD~1 --target=build --parallel --max-parallel=3 - run: npx nx affected --base=HEAD~1 --target=test --parallel --max-parallel=2 - run: npx nx-cloud stop-all-agents pr: runs-on: ubuntu-latest if: ${{ github.event_name == 'pull_request' }} steps: - uses: actions/checkout@v2 with: ref: ${{ github.event.pull_request.head.ref }} fetch-depth: 0 - uses: actions/setup-node@v1 - run: npm install - run: npx nx affected --base=origin/main --target=build --parallel --max-parallel=3 - run: npx nx affected --base=origin/main --target=test --parallel --max-parallel=2 - run: npx nx-cloud stop-all-agents agent_1: runs-on: ubuntu-latest name: Agent 1 timeout-minutes: 60 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 - run: npm install - run: npx nx-cloud start-agent agent_2: runs-on: ubuntu-latest name: Agent 2 timeout-minutes: 60 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 - run: npm install - run: npx nx-cloud start-agent agent_3: runs-on: ubuntu-latest name: Agent 3 timeout-minutes: 60 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 - run: npm install - run: npx nx-cloud start-agent

The pr and main jobs implement the CI workflow. The 3 agent jobs execute the tasks created by pr and main. Setting timeout-minutes is needed only if you have very slow tasks.

Note that if you use an older version of @nrwl/nx-cloud, you need to add the following two env variables.

env: NX_BRANCH: ${{ github.event.number || github.ref }} NX_RUN_GROUP: ${{ github.run_id }}

Select an article to learn more about Nx Cloud