Configuring CI Using GitHub Actions and Nx

The GitHub can track the last successful run on main branch and use this as a reference point for the BASE. The Nx Set SHAs provides convenient implementation of this functionality which you can drop into you existing CI config. To understand why knowing the last successful build is important for the affected command, check out the in-depth explanation at Actions's docs.

Below is an example of a GitHub setup for an Nx workspace only building and testing what is affected. For more details on how the action is used, head over to the official docs.

1name: CI
2on:
3  push:
4    branches:
5      - main
6  pull_request:
7
8jobs:
9  main:
10    runs-on: ubuntu-latest
11    steps:
12      - uses: actions/checkout@v2
13        with:
14          fetch-depth: 0
15      - uses: nrwl/nx-set-shas@v3
16      - run: npm ci
17
18      - run: npx nx workspace-lint
19      - run: npx nx format:check
20      - run: npx nx affected --target=lint --parallel=3
21      - run: npx nx affected --target=test --parallel=3 --ci --code-coverage
22      - run: npx nx affected --target=build --parallel=3
23

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

Distributed CI with Nx Cloud

In order to use distributed task execution, we need to start agents and set the NX_CLOUD_DISTRIBUTED_EXECUTION flag to true.

Read more about the Distributed CI setup with Nx Cloud.

1name: CI
2on:
3  push:
4    branches:
5      - main
6  pull_request:
7
8jobs:
9  main:
10    name: Nx Cloud - Main Job
11    uses: nrwl/ci/.github/workflows/nx-cloud-main.yml@v0.7
12    with:
13      number-of-agents: 3
14      parallel-commands: |
15        npx nx-cloud record -- npx nx workspace-lint
16        npx nx-cloud record -- npx nx format:check
17      parallel-commands-on-agents: |
18        npx nx affected --target=lint --parallel=3
19        npx nx affected --target=test --parallel=3 --ci --code-coverage
20        npx nx affected --target=build --parallel=3
21
22  agents:
23    name: Nx Cloud - Agents
24    uses: nrwl/ci/.github/workflows/nx-cloud-agents.yml@v0.7
25    with:
26      number-of-agents: 3
27

You can also use our ci-workflow generator to generate the workflow file.