Configuring CI Using Azure Pipelines and Nx
Below is an example of an Azure Pipelines setup for an Nx workspace - building and testing only what is affected.
Check your Shallow Fetch settingsNx needs additional Git history available for affected
to function correctly. Make sure Shallow fetching is disabled in your pipeline settings UI. For more info, check out this article from Microsoft here.
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 an SHA in the main job once it succeeds and then use this tag as a base. You can also try using the devops CLI within the pipeline yaml. See the nx-tag-successful-ci-run and nx-set-shas (version 1 implements tagging mechanism) repositories for more information.
We also have to set NX_BRANCH
explicitly. NX_BRANCH does not impact the functionality of your runs, but does provide a human-readable label to easily identify them in the Nx Cloud app.
1trigger:
2 - main
3pr:
4 - main
5
6variables:
7 CI: 'true'
8 ${{ if eq(variables['Build.Reason'], 'PullRequest') }}:
9 NX_BRANCH: $(System.PullRequest.PullRequestId) # You can use $(System.PullRequest.PullRequestNumber if your pipeline is triggered by a PR from GitHub ONLY)
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 # Set Azure Devops CLI default settings
23 - bash: az devops configure --defaults organization=$(System.TeamFoundationCollectionUri) project=$(System.TeamProject)
24 displayName: 'Set default Azure DevOps organization and project'
25
26 # Get last successfull commit from Azure Devops CLI
27 - displayName: 'Get last successful commit SHA'
28 condition: ne(variables['Build.Reason'], 'PullRequest')
29 env:
30 AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
31 bash: |
32 LAST_SHA=$(az pipelines build list --branch $(Build.SourceBranchName) --definition-ids $(System.DefinitionId) --result succeeded --top 1 --query "[0].triggerInfo.\"ci.sourceSha\"")
33 if [ -z "$LAST_SHA" ]
34 then
35 echo "Last successful commit not found. Using fallback 'HEAD~1': $BASE_SHA"
36 else
37 echo "Last successful commit SHA: $LAST_SHA"
38 echo "##vso[task.setvariable variable=BASE_SHA]$LAST_SHA"
39 fi
40
41 # Required for nx affected if we're on a branch
42 - script: git branch --track main origin/main
43 - script: npm ci
44 - script: npx nx format:check --base=$(BASE_SHA)
45 - script: npx nx affected --base=$(BASE_SHA) -t lint,test,build --parallel=3 --configuration=ci
46
The main
job implements the CI workflow.
Get the Commit of the Last Successful Build
In the example above we ran a script to retrieve the commit of the last successful build. The idea is to use Azure Devops CLI directly in the Pipeline Yaml
First, we configure Devops CLI
1# Set Azure Devops default settings
2- bash: az devops configure --defaults organization=$(System.TeamFoundationCollectionUri) project=$(System.TeamProject)
3 displayName: 'Configure Azure DevOps organization and project'
4
Then we can query the pipelines API (providing the auth token)
1# Get last successfully commit infos from Azure Devops
2- bash: |
3 LAST_SHA=$(az pipelines build list --branch $(Build.SourceBranchName) --definition-ids $(System.DefinitionId) --result succeeded --top 1 --query "[0].triggerInfo.\"ci.sourceSha\"")
4 echo "Last successful commit SHA: $LAST_SHA"
5 echo "##vso[task.setvariable variable=BASE_SHA]$LAST_SHA"
6 displayName: 'Get last successful commit SHA'
7 env:
8 AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
9
We can target a specific build, in this example we specified:
- The branch (--branch)
- The pipeline Id (--definition-ids)
- The result type (--result)
- The number of result (-top)
By default the command returns an entire JSON object with all the information. But we can narrow it down to the desired result with the --query
param that uses JMESPath format (more details)
Finally we extract the result in a common custom variable named BASE_SHA
used later by nx affected
commands
Distributed Task Execution with Nx Cloud
Read more about Distributed Task Execution (DTE).
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: agents
19 strategy:
20 parallel: 3
21 displayName: Nx Cloud Agent
22 pool:
23 vmImage: 'ubuntu-latest'
24 steps:
25 - script: npm ci
26 - script: npx nx-cloud start-agent
27
28 - job: main
29 displayName: Nx Cloud Main
30 pool:
31 vmImage: 'ubuntu-latest'
32 steps:
33 # Get last successfull commit from Azure Devops CLI
34 - displayName: 'Get last successful commit SHA'
35 condition: ne(variables['Build.Reason'], 'PullRequest')
36 env:
37 AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
38 bash: |
39 LAST_SHA=$(az pipelines build list --branch $(Build.SourceBranchName) --definition-ids $(System.DefinitionId) --result succeeded --top 1 --query "[0].triggerInfo.\"ci.sourceSha\"")
40 if [ -z "$LAST_SHA" ]
41 then
42 echo "Last successful commit not found. Using fallback 'HEAD~1': $BASE_SHA"
43 else
44 echo "Last successful commit SHA: $LAST_SHA"
45 echo "##vso[task.setvariable variable=BASE_SHA]$LAST_SHA"
46 fi
47
48 - script: git branch --track main origin/main
49 - script: npm ci
50 - script: npx nx-cloud start-ci-run --stop-agents-after="build"
51 - script: npx nx-cloud record -- npx nx format:check --base=$(BASE_SHA) --head=$(HEAD_SHA)
52 - script: npx nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) -t lint,test,build --parallel=3 --configuration=ci
53
You can also use our ci-workflow generator to generate the pipeline file.