How to select agent pool dynamically on running a pipeline using restapi

Lokeshna Virdi 0 Reputation points
2023-12-14T12:30:39.4466667+00:00

I want to select the agent pool while I run the pipeline using restapi call, Is it possible ? if yes can you share the request structure and the sample pipeline yaml also the restAPI URL

Community Center | Not monitored
{count} votes

2 answers

Sort by: Most helpful
  1. Sedat SALMAN 14,180 Reputation points MVP
    2023-12-14T13:06:48.3+00:00

    modify your Azure DevOps YAML pipeline to accept a parameter

    parameters:
      - name: agentName
        displayName: Agent Name
        type: string
        default: 'DefaultPool'
    
    pool:
      ${{ if eq(parameters.agentName, 'DefaultPool') }}:
        name: DefaultPool
      ${{ if ne(parameters.agentName, 'DefaultPool') }}:
        name: ${{ parameters.agentName }}
    
    
    

    The REST API endpoint to trigger a pipeline is

    POST https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs?api-version=7.1-preview.1
    

    https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/runs/run-pipeline?view=azure-devops-rest-7.1

    The request body should include the parameter

    {
        "templateParameters": {
            "agentName": "CustomAgentPool"
        }
    }
    
    
    

    API call to trigger the pipeline

    const AZURE_PIPELINE_URL = 'https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs?api-version=7.1-preview.1';
    
    fetch(AZURE_PIPELINE_URL, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Basic ' + AZURE_PERSONAL_ACCESS_TOKEN,
      },
      body: JSON.stringify({
        templateParameters: {
          agentName: 'CustomAgentPool'
        }
      }),
    })
    
    
    0 comments No comments

  2. Lokeshna Virdi 0 Reputation points
    2023-12-15T07:56:20.57+00:00

    It is giving an error

    {
        "$id": "1",
        "innerException": null,
        "message": "/got-RapidDev.yaml (Line: 11, Col: 8): A template expression is not allowed in this context",
        "typeName": "Microsoft.Azure.Pipelines.WebApi.PipelineValidationException, Microsoft.Azure.Pipelines.WebApi",
        "typeKey": "PipelineValidationException",
        "errorCode": 0,
        "eventId": 3000
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.