Skip to content

Setting Up CI for Nx Cloud

Learn how to set up Nx Cloud for your workspace with your preferred CI platform. Get started connecting to Nx Cloud by running nx connect.

Each platform has specific configurations and optimizations that help you build and test only what is affected, retrieve previous successful builds, and optimize CI performance.

Need a starting point? Generate a new workflow file with the following command:

nx g ci-workflow --ci=github

Below is an example of a GitHub Actions setup, building, and testing only what is affected.

.github/workflows/ci.yml
name: CI
on:
push:
branches:
- main
pull_request:
permissions:
actions: read
contents: read
jobs:
main:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
filter: tree:0
fetch-depth: 0
# This enables task distribution via Nx Cloud
# Run this command as early as possible, before dependencies are installed
# Learn more at https://nx.dev/ci/reference/nx-cloud-cli#npx-nxcloud-startcirun
# Connect your workspace by running "nx connect" and uncomment this line to enable task distribution
# - run: npx nx start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="build"
# Cache node_modules
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- run: npm ci --legacy-peer-deps
- uses: nrwl/nx-set-shas@v4
# Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud
# - run: npx nx-cloud record -- echo Hello World
- run: npx nx affected -t lint test build
# Nx Cloud recommends fixes for failures to help you get CI green faster. Learn more: https://nx.dev/ci/features/self-healing-ci
- run: npx nx fix-ci
if: always()

Get the Commit of the Last Successful Build

Section titled “Get the Commit of the Last Successful Build”

The GitHub can track the last successful run on the main branch and use this as a reference point for the BASE. The nrwl/nx-set-shas provides a convenient implementation of this functionality, which you can drop into your existing CI workflow.

To understand why knowing the last successful build is important for the affected command, check out the in-depth explanation in Actions's docs.

All CI platforms support task distribution via Nx Cloud. To enable it:

  1. Connect your workspace by running nx connect
  2. Uncomment the npx nx start-ci-run command in your CI configuration
  3. Configure the distribution settings based on your needs

Learn more at Nx Cloud CI Reference.

Nx Cloud can recommend fixes for failures to help you get CI green faster. The nx fix-ci command is included in all platform configurations and runs automatically after failures.

Learn more about Self-Healing CI features.

You can record any command's logs to Nx Cloud by prepending it with nx-cloud record --. This helps with debugging and monitoring your CI pipeline.

Example:

Terminal window
npx nx-cloud record -- echo Hello World