azure-devopsazure-pipelines

How do you specify the sourceBranch for a Run of Pipelines via the REST API?


I've been trying to run a pipeline for a particular branch of the repository I'm using.

In the UI, there is a convenient option, but I don't understand what to try in the request.

enter image description here

No matter what I do I always run from master.

How do I change that? I tried filling out the repository parameters but to no avail: https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/runs/run%20pipeline?view=azure-devops-rest-6.0#repositoryresourceparameters

Here is an example request:

curl --location --request POST 'https://dev.azure.com/<redacted>/<redacted>/_apis/pipelines/<redacted>/runs?api-version=6.0-preview.1' \
--header 'Authorization: Basic <redacted>' \
--header 'Content-Type: application/json' \
--header 'Cookie: VstsSession=<redacted>' \
--data-raw '{   
    "previewRun": true,
    "resources": {
        "repositories": {
            "refName": "refs/heads/<redacted>"
        }
    },
    "runParameters":
    {
        "namespace" : "<redacted>",
        "image" : "<redacted>",
        "tag" : "<redacted>",
        "package" : "<redacted>",
        "version" : "8.4.4"
    }
}'

Solution

  • From your screenshot, it seems that you are using the YAML pipeline.

    I have tested your example , and the root cause of this issue is that the request body(data-raw) has some issues.

    You could try my sample

    curl --location --request POST 'https://dev.azure.com/<redacted>/<redacted>/_apis/pipelines/<redacted>/runs?api-version=6.0-preview.1' \
    --header 'Authorization: Basic <redacted>' \
    --header 'Content-Type: application/json' \
    --header 'Cookie: VstsSession=<redacted>' \
    --data-raw '{   
    "stagesToSkip":[],
        "resources":
        {
            "repositories":
            {
                "self":{"refName":"refs/heads/{Branchname}"}
                }
    
        },
        "templateParameters":
        {
            "namespace":"{value}",
            "image":"{value}",
            "tag":"{value}",
            "package":"{value}",
            "version":"{value}"
        },
        "variables":{}
    }'
    

    Result:

    enter image description here