简体   繁体   English

如何通过 REST API 为管道运行指定 sourceBranch?

[英]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.在 UI 中,有一个方便的选项,但我不明白在请求中要尝试什么。

在此处输入图像描述

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我尝试填写存储库参数但无济于事: 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.从您的屏幕截图中,您似乎正在使用 YAML 管道。

I have tested your example, and the root cause of this issue is that the request body(data-raw) has some issues.我已经测试了你的例子,这个问题的根本原因是请求正文(data-raw)有一些问题。

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)对于 Http 请求(可以用 Postman 测试)

1. Get pipeline api url like this https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs?api-version=7.1-preview.1 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来源: 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.填写您的组织、项目和 pipelineId。 (all of this can be found in the link when you open your pipeline definition in Azure DevOps) (当您在 Azure DevOps 中打开管道定义时,所有这些都可以在链接中找到)

2. Add Basic Auth headers 2.添加基本认证头

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

Should look like this "Basic OndzNWdz43FKfjdi98hjKDJFH8kkg9854HJKHF9D8RFEHui4387lkNXE="应该看起来像这样“基本 OndzNWdz43FKfjdi98hjKDJFH8kkg9854HJKHF9D8RFEHui4387lkNXE="

And set content type to application/json并将内容类型设置为 application/json

like this像这样

key: Content-Type, value: application/json键:内容类型,值:应用程序/json

3. Put this JSON Into Raw Body 3. 将此 JSON 放入原始机身

 { "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.将 refName 值替换为您要为其运行管道的分支。

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如果您有多个 repo 管道,请查看此主题(但我尚未测试该解决方案): Azure DevOps API to Trigger Multi Repo changes branches

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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