简体   繁体   中英

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.

在此处输入图像描述

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://docs.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"
    }
}'

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:

在此处输入图像描述

For Http Request (you can test it with Postman)

1. Get pipeline api url like this https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs?api-version=7.1-preview.1

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

Fill in your organization, project and pipelineId. (all of this can be found in the link when you open your pipeline definition in Azure DevOps)

2. Add Basic Auth headers

key: Authorization, value:"Basic [your azure Person Access Token from Azure Dev Ops with Access to Pipeline goes here]"

Should look like this "Basic OndzNWdz43FKfjdi98hjKDJFH8kkg9854HJKHF9D8RFEHui4387lkNXE="

And set content type to application/json

like this

key: Content-Type, value: application/json

3. Put this JSON Into Raw Body

 { "templateParameters":{ "inputName":"johnsmith" }, "resources":{ "repositories":{ "self":{ "refName":"refs/heads/feature/#JIRATask01_Blabla" } } }, "variables":{ } }

Replace refName value with branch you want to run the pipeline for.

If you have multiple repo pipeline look into this topic (but i haven't tested that solution): Azure DevOps API to Trigger Multi Repo changing branches

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM