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