【问题标题】:Azure REST API for running builds or pipelines用于运行生成或管道的 Azure REST API
【发布时间】:2020-08-30 05:49:08
【问题描述】:

我正在尝试使用他们的 REST api 为特定分支自动创建 Azure Pipelines。

但是,我很难使用他们几乎所有的 API,因为他们的文档缺少示例。

List 和 Get 之类的东西很简单。

但是,在对构建进行排队时: https://docs.microsoft.com/en-us/rest/api/azure/devops/build/builds/queue?view=azure-devops-rest-6.0

POST https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=6.0
{
    "parameters": <parameters>, // how do i send paramters
    "definition": {
        "id": 1
    },
    "sourceBranch": "refs/heads/feature/my-pipeline",
    "sourceVersion": "d265f01aeb4e677a25725f44f20ceb3ff1d7d767"
}

我目前正在努力发送参数。 我试过了:

类似的简单 JSON:

"parameters": {
    "appId": "bab",
    "platform": "android",
    "isDemo": true
}

和字符串化的 JSON 版本,如:

"parameters": "{\"appId\": \"bab\",\"platform\": \"android\",\"isDemo\": true}"

但似乎没有一个工作。

它一直给我错误:

{
    "$id": "1",
    "customProperties": {
        "ValidationResults": [
            {
                "result": "error",
                "message": "A value for the 'appId' parameter must be provided."
            },
            {
                "result": "error",
                "message": "A value for the 'platform' parameter must be provided."
            },
            {
                "result": "error",
                "message": "A value for the 'isDemo' parameter must be provided."
            }
        ]
    },
    "innerException": null,
    "message": "Could not queue the build because there were validation errors or warnings.",
    "typeName": "Microsoft.TeamFoundation.Build.WebApi.BuildRequestValidationFailedException, Microsoft.TeamFoundation.Build2.WebApi",
    "typeKey": "BuildRequestValidationFailedException",
    "errorCode": 0,
    "eventId": 3000
}

文档非常不清楚如何发送这些数据:https://docs.microsoft.com/en-us/rest/api/azure/devops/build/builds/queue?view=azure-devops-rest-6.1#propertiescollection

非常感谢您的帮助。

【问题讨论】:

  • 在文档中它说它是一个字符串,所以我会尝试使用类似这样的参数:“appId=xxxxx platform=xxxx isDemo=xxxxx”,这就是我发送变量的方式使用 Azure Cli for powershell 构建,在该文档中它就像这样一个字符串。试试看!!
  • 刚刚尝试过:appId=test platform=android isDemo=true,但也出现错误。

标签: azure azure-devops azure-pipelines azure-rest-api


【解决方案1】:

我相信您无法通过 Queue API 传递运行时参数。相反,请使用Runs API

这样,您的请求正文(使用Content-type: application/json)应该类似于以下内容:

{
    "resources": {
        "repositories": {
            "self": {
                "refName": "refs/heads/feature/my-pipeline"
            }
        }
    },
    "templateParameters": {
        "appId": "bab"
        "platform": "android"
        "isDemo": true
    }
}

【讨论】:

  • 感谢这工作。你救命啊!你怎么知道如何发送资源部分。它没有像这样记录。
  • 很高兴能帮上忙!您可以查看以THIS 开头并跟随资源类型列下的超链接的文档。我看到只有 self 参数没有记录在那里,它只是表示您正在使用的当前存储库。
  • “Builds”和“Runs”还有什么区别?
  • self 参数部分记录在 HERE。我认为您还应该能够通过在请求正文中包含确切的存储库参数(而不仅仅是self)来定义它,但在这种情况下这似乎是不必要的,因为您只使用一个主存储库。前一阵子实验弄明白的,当时没有发现具体的文档,现在好像也没有确切的定义。
  • 关于差异(构建与运行)。 Builds API 允许您更具体地访问每个构建并以这种方式定义它,而 Runs(包含在 Pipelines API 中)通常用于在这种情况下 Pipeline 本身“更高”一级的操作。这只是我与他们两人合作的一些经验的看法。
【解决方案2】:

我刚刚意识到,在 api-version=6.0 中,您还可以在队列服务上发送 templateParameters:

POST https://dev.azure.com/{organization}/{project}/_apis/build/builds?sourceBuildId={BUILD_BUILDID}&api-version=6.0
{
    "templateParameters": { "doReleaseBuild": "True" }, 
    "definition": {
        "id": 1
    },
    "sourceBranch": "refs/heads/feature/my-pipeline",
    "sourceVersion": "d265f01aeb4e677a25725f44f20ceb3ff1d7d767"
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 2021-10-30
    • 2021-11-25
    • 1970-01-01
    • 2020-12-28
    • 1970-01-01
    相关资源
    最近更新 更多