繁体   English   中英

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

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

我一直在尝试为我正在使用的存储库的特定分支运行管道。

在 UI 中,有一个方便的选项,但我不明白在请求中要尝试什么。

在此处输入图像描述

无论我做什么,我总是逃避主人。

我该如何改变呢? 我尝试填写存储库参数但无济于事: https://docs.microsoft.com/en-us/rest/api/azure/devops/pipelines/runs/run%20pipeline?view=azure-devops-rest- 6.0#repositoryresourceparameters

这是一个示例请求:

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"
    }
}'

从您的屏幕截图中,您似乎正在使用 YAML 管道。

我已经测试了你的例子,这个问题的根本原因是请求正文(data-raw)有一些问题。

你可以试试我的样品

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":{}
}'

结果:

在此处输入图像描述

对于 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

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

填写您的组织、项目和 pipelineId。 (当您在 Azure DevOps 中打开管道定义时,所有这些都可以在链接中找到)

2.添加基本认证头

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

应该看起来像这样“基本 OndzNWdz43FKfjdi98hjKDJFH8kkg9854HJKHF9D8RFEHui4387lkNXE="

并将内容类型设置为 application/json

像这样

键:内容类型,值:应用程序/json

3. 将此 JSON 放入原始机身

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

将 refName 值替换为您要为其运行管道的分支。

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

暂无
暂无

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

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