skip to Main Content

Is it possible to auto Disable an azure self hosted agent if it fails a job?

I am doing appium based android UI testing through azure self hosted agents. Occasionally one of the PCs will lose UsB debugging authorization to the android device. When this happens the agent will return Failed quite quickly and continue to grab test after test returning Failed jobs.

Is there a way to set a condition where if an agent returns a Failed job that we can disable the agent to prevent it from picking up any future jobs until I am able to manually address the issue?

2

Answers


  1. No, not really, but you could add a conditional step or job and use the API to disable the runner using the PATCH method:

    https://learn.microsoft.com/en-us/rest/api/azure/devops/distributedtask/agents/update?view=azure-devops-rest-7.1

    You may need to grant the Azure Pipelines user additional permissions to manage the Agent Pool.

    Login or Signup to reply.
  2. There is functionality out-of-the-box to disable a self-hosted if the agent job running on it is failed. You may consider the workaround to add a script to call this API to disable the current agent, on the condition that any of the previous steps within the same agent job is failed. We can collect the pool id from the URL like that in the screenshot below and retrieve the agentId with the predefined agent variable $(Agent.Id), which will always point to the agent that current job is running on.

    Here is a sample YAML pipeline for your reference.

    jobs:
    - job: FailedAgentJob
      pool: Default # Whose pool id is 1
      variables:
        poolId: 1
      steps:
      - powershell: |
          Write-Host "##vso[task.logissue type=error;]'Represent some failed tests on the current self-hosted agent'"
          Write-Host "##vso[task.complete result=Failed;]"
        displayName: Some UI tests
      - powershell: |
          Write-Host "Agent.Id - $(Agent.Id)"
          Write-Host "Agent.Name - $(Agent.Name)"
          Write-Host "Agent.MachineName - $(Agent.MachineName)"
          
          $URL = "$(System.CollectionUri)/_apis/distributedtask/pools/$(poolId)/agents/$(Agent.Id)?api-version=7.2-preview.1"
          $headers = @{
            'Authorization' = 'Bearer ' + "$(System.AccessToken)"
            'Content-Type' = 'application/json'
          }
          $body = @{
            id = $(Agent.Id)
            enabled = $false
          } | ConvertTo-Json
          
          Invoke-RestMethod -Uri $URL -Method Patch -Headers $headers -Body $body
        condition: failed()
    

    Image

    Image

    Since the sample request authenticates against the $(System.AccessToken) of the pipeline service accounts to disable the current self-hosted agent, make sure to grant Administrator role for either (or both) of the pipeline service accounts corresponding to the job authorization scope in your organization & project settings.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search