Custom Distributed Task Execution on Azure Pipelines

Using Nx Agents is the easiest way to distribute task execution, but it your organization may not be able to use hosted Nx Agents. With an enterprise license, you can set up distributed task execution on your own CI provider using the recipe below.

Run Custom Agents on Azure Pipelines

Run agents directly on Azure Pipelines with the workflow below:

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.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 - checkout: self 26 fetchDepth: 0 27 persistCredentials: true 28 29 - script: npm ci 30 - script: npx nx-cloud start-agent 31 32 - job: main 33 displayName: Nx Cloud Main 34 pool: 35 vmImage: 'ubuntu-latest' 36 steps: 37 # Get last successfull commit from Azure Devops CLI 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 displayName: 'Get last successful commit SHA' 48 condition: ne(variables['Build.Reason'], 'PullRequest') 49 env: 50 AZURE_DEVOPS_EXT_PAT: $(System.AccessToken) 51 52 - script: git branch --track main origin/main 53 - script: npm ci 54 - script: npx nx-cloud start-ci-run --distribute-on="manual" --stop-agents-after="e2e-ci" 55 - script: npx nx-cloud record -- nx format:check --base=$(BASE_SHA) --head=$(HEAD_SHA) 56 - script: npx nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) -t lint,test,build,e2e-ci --parallel=2 --configuration=ci 57

This configuration is setting up two types of jobs - a main job and three agent jobs.

The main job tells Nx Cloud to use DTE and then runs normal Nx commands as if this were a single pipeline set up. Once the commands are done, it notifies Nx Cloud to stop the agent jobs.

The agent jobs set up the repo and then wait for Nx Cloud to assign them tasks.

Two Types of Parallelization

The agent strategy of parallel: 3 and the nx affected --parallel=2 flag both parallelize tasks, but in different ways. The way this workflow is written, there will be 3 agents running tasks and each agent will try to run 2 tasks at once. If a particular CI run only has 2 tasks, only one agent will be used.