Configuring CI Using Azure Pipelines and Nx

Below is an example of an Azure Pipelines setup building and testing only what is affected.

azure-pipelines.yml
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: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" # this line enables distribution 44 - script: npm ci 45 - script: npx nx-cloud record -- nx format:check --base=$(BASE_SHA) 46 - script: npx nx affected --base=$(BASE_SHA) -t lint test build --parallel=3 47
Check your Shallow Fetch settings

Nx 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 a 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.

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