azure-devopsazure-pipelinesazure-devops-self-hosted-agent

Can I have my own self-hosted build agent server in Azure Devops, with fallback to cloud?


Is it possible to create an Agent Pool with self-hosted agents - which falls back to Azure Pipelines in case the self-hosted agents are unavailable?

I tried searching for an answer for it and poked around in Azure DevOps. It does not seem to be possible.


Solution

  • That requires the pipeline to know if there is any idle agent in a self-hosted agent pool before assigning the agent. For this, you may have a test to call the REST API request in the agentless job pipeline task of InvokeRESTAPI@1 to check if the agents in an agent pool are assigned with requests, then determine to run the subsequent job in a MS-hosted or a self-hosted agent pool.

    
    parameters:
    - name: agentIds
      type: object
      default:
      - 465
      - 547
    
    variables:
      system.debug: true
      orgName: ${{split(variables['System.CollectionUri'], 'https://dev.azure.com/')[1] }}
      poolId: 1 # Default
    stages:
    - stage: StageCheck
      jobs:
      - job: AgentlessJob
        pool: server
        steps:
        - ${{each agentId in parameters.agentIds}}:
          - task: InvokeRESTAPI@1
            inputs:
              connectionType: 'connectedServiceName'
              serviceConnection: 'AzureDevOpsServices'
              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: 'eq(root.assignedRequest.reservedAgent.id, ${{agentId}})'
            continueOnError: true
    
    - stage: StageOnSelfhostedAgentPool
      dependsOn: StageCheck
      condition: eq(dependencies.StageCheck.result, 'SucceededWithIssues')
      variables:
        stageDependencies: $[convertToJson(stageDependencies)]
      jobs:
      - job: AgentJob
        pool: Default
        steps:
        - script: |
            echo "Detected idle agents in the Default self-hosted agent pool"
            echo "$(stageDependencies)"
          displayName: Run agent job in Default self-hosted agent pool
    
    - stage: StageOnMShostedAgentPool
      dependsOn: StageCheck
      condition: eq(dependencies.StageCheck.result, 'Succeeded')
      variables:
        stageDependencies: $[convertToJson(stageDependencies)]
      jobs:
      - job: AgentJob
        pool:
          vmImage: ubuntu-latest
        steps:
        - script: |
            echo "No idle agents in the Default self-hosted agent pool"
            echo "$(stageDependencies)"
          displayName: Run agent job in MS-hosted agent pool
    

    enter image description here

    Besides, you can probably go through the features of Managed DevOps Pools and VMSS Agent Pools, which may be suitable for you.