azure-devopsazure-pipelinesazure-pipelines-yaml

Azure Devops: Detect if an agent is available before running a job


I would like to check if a particular agent is available before running any jobs. I have encountered the problem that if the agent is unavailable the the run just hangs. I have tried setting a timeout on the job but this is ignored.

This is my pipeline:

trigger:
- none

jobs:
- job: Build
  pool: testAgents
  timeoutInMinutes: 2
  steps:
    - task: CmdLine@2
      inputs:
        script: |
          echo Write your commands here
          
          echo Hello world

Ok some more background I have a pipeline that runs when a pull request is created. Recently I was away from the office for a couple of weeks. During that time several pull requests were created. However, whilst I was away the build machine running the agent was powered down so no builds were happening and users had no idea what was going on. So I was tasked to see if we could give some feedback to users if the agent was not available.


Solution

  • You may use the InvokeRESTAPI@1 task in an Agentless job to call this API to check the status of APaticularAgent in the self-hosted agent pool testAgents. If the agent is not online or not enabled, it then sends a custom notification to Teams Channel via Power Automate webhook (see the example). Here is a sample YAML pipeline for your reference.

    trigger: none
    
    variables:
      poolId: 68 # testAgents
      orgName: ${{split(variables['System.CollectionUri'], 'https://dev.azure.com/')[1] }} # extract orgName from $(System.CollectionUri) - https://dev.azure.com/orgName
      system.debug: true
    
    jobs:
    - job: AgentlessJob
      strategy:
        matrix:
          agent01:
            agentId: 731 # AParticularAgent
          # agent02:
          #   agentId: 732
      pool: server # note: the value 'server' is a reserved keyword which indicates this is an agentless job
      steps:
      - task: InvokeRESTAPI@1
        displayName: Check if agent-$(agentId) is enabled and online
        inputs:
          connectionType: 'connectedServiceName'
          serviceConnection: 'AzureDevOpsServices' # https://dev.azure.com
          method: 'GET'
          headers: |
            {
              "Content-Type":"application/json",  
              "Authorization": "Bearer $(system.AccessToken)"
            }
          urlSuffix: '/$(orgName)/_apis/distributedtask/pools/$(poolId)/agents/$(agentId)?includeAssignedRequest=true&api-version=7.1-preview.1'
          waitForCompletion: 'false'
          successCriteria: and( eq(root.enabled, 'true'), eq(root.status, 'online'))
    
      - task: InvokeRESTAPI@1
        condition: failed()
        displayName: Send notification to Teams
        inputs:
          connectionType: 'connectedServiceName'
          serviceConnection: 'PowerAutomateWebHook'
          method: 'POST'
          headers: |
            {
            "Content-Type":"application/json"
          body: |
            {
              "title": "Agent Status Check - FAILED",
              "text": "<b>View the build results:</b><br /><a href=\"$(System.CollectionUri)$(System.TeamProjectId)/_build/results?buildId=$(Build.BuildId)&view=results\">$(Build.DefinitionName) - $(Build.BuildId)</a><br /><b>View the agent $(agentId) in:</b><br /><a href=\"$(System.CollectionUri)_settings/agentpools?poolId=$(poolId)&view=agents\">testAgents</a>"
            }
          urlSuffix: '&sig=$(sigSecret)'
          waitForCompletion: 'false'
    
    - job: AgentJobSelf
      dependsOn: AgentlessJob
      pool:
        name: testAgents
        Agent/Name: AParticularAgent
      steps:
      - powershell: |
          Write-Host "Detected the Particular Agent is enabled and online - Queued the agent job"
        displayName: Run agent job in self-hosted agent pool testAgents
    

    Image

    enter image description here