Configuring CI Using Azure Pipelines and Nx
Below is an example of an Azure Pipelines setup for an Nx workspace only building and testing what is affected.
Unlike GitHub Actions
and CircleCI
, you don't have the metadata to help you track the last successful run on main
. In the example below, the base is set to HEAD~1
(for push) or branching point (for pull requests), but a more robust solution would be to tag a SHA in the main job once it succeeds, and then use this tag as a base. See the nx-tag-successful-ci-run and nx-set-shas (version 1 implements tagging mechanism) repos for more information.
We also have to set NX_BRANCH
explicitly.
1trigger:
2 - main
3pr:
4 - main
5
6variables:
7 CI: 'true'
8 ${{ if eq(variables['Build.Reason'], 'PullRequest') }}:
9 NX_BRANCH: $(System.PullRequest.PullRequestNumber)
10 TARGET_BRANCH: $[replace(variables['System.PullRequest.TargetBranch'],'refs/heads/','origin/')]
11 BASE_SHA: $(git merge-base $(TARGET_BRANCH) HEAD)
12 ${{ if ne(variables['Build.Reason'], 'PullRequest') }}:
13 NX_BRANCH: $(Build.SourceBranchName)
14 BASE_SHA: $(git rev-parse HEAD~1)
15 HEAD_SHA: $(git rev-parse HEAD)
16
17jobs:
18 - job: main
19 pool:
20 vmImage: 'ubuntu-latest'
21 steps:
22 - script: npm ci
23
24 - script: npx nx workspace-lint
25 - script: npx nx format:check
26
27 - script: npx nx affected --base=$(BASE_SHA) --target=lint --parallel=3
28 - script: npx nx affected --base=$(BASE_SHA) --target=test --parallel=3 --ci --code-coverage
29 - script: npx nx affected --base=$(BASE_SHA) --target=build --parallel=3
30
The main
job implements the CI workflow.
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.
1trigger:
2 - main
3pr:
4 - main
5
6variables:
7 CI: 'true'
8 NX_CLOUD_DISTRIBUTED_EXECUTION: 'true'
9 ${{ if eq(variables['Build.Reason'], 'PullRequest') }}:
10 NX_BRANCH: $(System.PullRequest.PullRequestNumber)
11 TARGET_BRANCH: $[replace(variables['System.PullRequest.TargetBranch'],'refs/heads/','origin/')]
12 BASE_SHA: $(git merge-base $(TARGET_BRANCH) HEAD)
13 ${{ if ne(variables['Build.Reason'], 'PullRequest') }}:
14 NX_BRANCH: $(Build.SourceBranchName)
15 BASE_SHA: $(git rev-parse HEAD~1)
16 HEAD_SHA: $(git rev-parse HEAD)
17
18jobs:
19 - job: agents
20 strategy:
21 parallel: 3
22 displayName: Nx Cloud Agent
23 pool:
24 vmImage: 'ubuntu-latest'
25 steps:
26 - script: npm ci
27 - script: npx nx-cloud start-agent
28
29 - job: main
30 displayName: Nx Cloud Main
31 pool:
32 vmImage: 'ubuntu-latest'
33 steps:
34 - script: npm ci
35 - script: npx nx-cloud start-ci-run
36
37 - script: npx nx-cloud record -- npx nx workspace-lint
38 - script: npx nx-cloud record -- npx nx format:check --base=$(BASE_SHA) --head=$(HEAD_SHA)
39 - script: npx nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) --target=lint --parallel=3
40 - script: npx nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) --target=test --parallel=3 --ci --code-coverage
41 - script: npx nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) --target=build --parallel=3
42
43 - script: npx nx-cloud stop-all-agents
44 condition: always()
45
You can also use our ci-workflow generator to generate the pipeline file.