【问题标题】:Azure DevOps REST API to create a release definition用于创建发布定义的 Azure DevOps REST API
【发布时间】:2020-05-23 11:03:28
【问题描述】:

我正在尝试使用 Azure DevOps REST API 创建发布定义。我创建了一个 json 文件,其中包含请求的配置详细信息。在创建发布定义时出现以下错误。

{
    "$id": "1",
    "innerException": null,
    "message": "Workflow of deploy job 'Run on the agent' in release pipeline stage 'development' is invalid. Add valid tasks and try again.",
    "typeName": "Microsoft.VisualStudio.Services.ReleaseManagement.Data.Exceptions.InvalidRequestException, Microsoft.VisualStudio.Services.ReleaseManagement2.Data",
    "typeKey": "InvalidRequestException",
    "errorCode": 0,
    "eventId": 3000 
}

我正在使用以下请求正文在 Azure 云中创建新的发布定义。

{ "name": "myreleasedefn1", "artifacts": [{ "type": "DockerHub", "alias": "_dockerusername_mydockerimage", "definitionReference": { "connection": { "id": "dd986f4a-123k-45d5-b8e6-fc4fds23rce", "name": "dockerservice" }, "defaultVersionType": { "id": "selectDuringReleaseCreationType", "name": "Specify at the time of release creation" }, "definition": { "id": "dockerusername/mydockerimage", "name": "dockerusername/mydockerimage" }, "namespaces": { "id": "dockerusername", "name": "dockerusername" } }, "isPrimary": true, "isRetained": false }], "releaseNameFormat": "Release-$(rev:r)", "environments": [{ "name": "development", "rank": 1, "retentionPolicy": { "daysToKeep": 30, "releasesToKeep": 3, "retainBuild": true }, "preDeployApprovals": { "approvals": [ { "rank": 1, "isAutomated": true, "isNotificationOn": false, "id": 10 } ], "approvalOptions": { "requiredApproverCount": null, "releaseCreatorCanBeApprover": false, "autoTriggeredAndPreviousEnvironmentApprovedCanBeSkipped": false, "enforceIdentityRevalidation": false, "timeoutInMinutes": 0, "executionOrder": "beforeGates" } }, "postDeployApprovals": { "approvals": [ { "rank": 1, "isAutomated": true, "isNotificationOn": false, "id": 12 } ], "approvalOptions": { "requiredApproverCount": null, "releaseCreatorCanBeApprover": false, "autoTriggeredAndPreviousEnvironmentApprovedCanBeSkipped": false, "enforceIdentityRevalidation": false, "timeoutInMinutes": 0, "executionOrder": "afterSuccessfulGates" } }, "deployPhases": [{ "deploymentInput": { "parallelExecution": { "parallelExecutionType": "none" }, "agentSpecification": { "identifier": "ubuntu-16.04" }, "skipArtifactsDownload": false, "artifactsDownloadInput": { "downloadInputs": [] }, "queueId": 9, "demands": [], "enableAccessToken": false, "timeoutInMinutes": 0, "jobCancelTimeoutInMinutes": 1, "condition": "succeeded()", "overrideInputs": {} }, "rank": 1, "phaseType": "agentBasedDeployment", "name": "Run on the agent", "workflowTasks": [{ "version": "4.*", "name": "Deploy Azure App Service", "refName": "", "enabled": true, "alwaysRun": false, "continueOnError": false, "timeoutInMinutes": 0, "overrideInputs": {}, "condition": "succeeded()", "inputs": { "ConnectionType": "AzureRM", "WebAppKind": "webAppContainer", "WebAppName": "azureappservice1", "DeployToSlotOrASEFlag": "false", "ResourceGroupName": "", "SlotName": "production", "DockerNamespace": "dockerusername", "DockerRepository": "mydockerimage", "DockerImageTag": "10"
} }] }] }] }

如果您为同一请求共享一个示例模板,那将非常有帮助。 请帮忙!!!

【问题讨论】:

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


    【解决方案1】:

    用于创建发布定义的 Azure DevOps REST API

    根据报错信息:

    "Workflow of deploy job 'Run on the agent' in release pipeline stage 'development' is invalid.
    

    我们可以知道workflowTasks中的任务Deploy Azure App Service是无效的,我们需要为该任务提供正确的请求体。

    正如我回答您之前的帖子 How to create new build pipeline using Azure DevOps REST API 一样,如果我们完全手动添加巨大的请求正文,这将非常困难且容易出错。通常,我们使用 REST API Definitions - Get 从类似的发布管道中获取响应体,然后通过修改响应体来更新相应的属性。

    作为测试,我在发布管道中添加任务部署 Azure 应用服务,然后使用 REST API:

    GET https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/definitions/{definitionId}?api-version=5.0
    

    获取响应正文:

                    "workflowTasks": [
                        {
                            "environment": {},
                            "taskId": "497d490f-eea7-4f2b-ab94-48d9c1acdcb1",
                            "version": "4.*",
                            "name": "Azure App Service Deploy: xxxx",
                            "refName": "",
                            "enabled": true,
                            "alwaysRun": false,
                            "continueOnError": false,
                            "timeoutInMinutes": 0,
                            "definitionType": "task",
                            "overrideInputs": {},
                            "condition": "succeeded()",
                            "inputs": {
                                "ConnectionType": "AzureRM",
                                "ConnectedServiceName": "xxxxx",
                                "PublishProfilePath": "$(System.DefaultWorkingDirectory)/**/*.pubxml",
                                "PublishProfilePassword": "",
                                "WebAppKind": "webApp",
                                "WebAppName": "xxxx",
                                "DeployToSlotOrASEFlag": "false",
                                "ResourceGroupName": "",
                                "SlotName": "production",
                                "DockerNamespace": "",
                                "DockerRepository": "",
                                "DockerImageTag": "",
                                "VirtualApplication": "",
                                "Package": "$(System.DefaultWorkingDirectory)/**/*.zip",
                                "RuntimeStack": "",
                                "RuntimeStackFunction": "",
                                "StartupCommand": "",
                                "ScriptType": "",
                                "InlineScript": ":: You can provide your deployment commands here. One command per line.",
                                "ScriptPath": "",
                                "WebConfigParameters": "",
                                "AppSettings": "",
                                "ConfigurationSettings": "",
                                "UseWebDeploy": "false",
                                "DeploymentType": "webDeploy",
                                "TakeAppOfflineFlag": "true",
                                "SetParametersFile": "",
                                "RemoveAdditionalFilesFlag": "false",
                                "ExcludeFilesFromAppDataFlag": "true",
                                "AdditionalArguments": "-retryAttempts:6 -retryInterval:10000",
                                "RenameFilesFlag": "true",
                                "XmlTransformation": "false",
                                "XmlVariableSubstitution": "false",
                                "JSONFiles": ""
                            }
                        }
                    ]
    

    您可以使用此响应正文,并覆盖您要更改的那些属性。

    在 Azure 门户中哪里可以获取 taskId?

    你可以在Github上查看任务的source code,一般在task.json的第一行。或者你可以使用 REST API/F12 来获取它。

    我们能否在请求中使用“AzureContainerRegistry”作为工件类型 身体?

    答案应该是肯定的(不是通过样本测试)。您可以在正文中添加以下请求正文:

    "artifacts": [
        {
            "sourceId": "xxxxxxxxxxxxxxx",
            "type": "AzureContainerRepository",
            "alias": "xxx",
            "definitionReference": {
                "connection": {
                    "id": "xxxxxx",
                    "name": "xxxx"
                },
                "defaultVersionType": {
                    "id": "latestType",
                    "name": "Latest"
                },
                "definition": {
                    "id": "xx",
                    "name": "xx"
                },
                "registryurl": {
                    "id": "22",
                    "name": "22"
                },
                "resourcegroup": {
                    "id": "xx",
                    "name": "xx"
                }
            },
            "isPrimary": true,
            "isRetained": false
        }
    ],
    

    希望这会有所帮助。

    【讨论】:

    • 如何在工作流中提供 Azure 订阅 ID?
    • 我们可以在请求正文中使用“AzureContainerRegistry”作为工件类型吗?
    • Azure 订阅正在使用 ConnectedServiceName 属性工作。我在使用 Azure Container Repository 时遇到了服务连接问题。我已经为 ACR 创建了服务连接,但它没有显示在 Azure 门户的列表中。可以请教吗?
    • 感谢@Leo Liu-MSFT。我开了一个新帖子stackoverflow.com/questions/60146484/…你能帮忙吗?
    • @KumareshBabuNS,当然,当我尽快与当前线程分开时,我会检查该线程。顺便说一句,如果我的回答解决了您在此线程中的先前问题,您可以接受它作为答案,这样它可以帮助遇到相同问题的其他社区成员,我们可以存档此线程,谢谢。
    【解决方案2】:

    我可以在这里提供帮助。在workflowTasks 中,您需要包含"taskId": "497d490f-eea7-4f2b-ab94-48d9c1acdcb1",它是AzureRmWebAppDeploymentV4 的ID。

    "workflowTasks": [
                {
                  "taskId": "497d490f-eea7-4f2b-ab94-48d9c1acdcb1",
                  "version": "4.*",
                  "name": "Deploy Azure App Service",
                  "refName": "",
                  "enabled": true,
                  "alwaysRun": false,
                  "continueOnError": false,
                  "timeoutInMinutes": 0,
                  "definitionType": null,
                  "overrideInputs": {},
                  "condition": "succeeded()",
                  "inputs": {
                      "ConnectionType": "AzureRM",
                      "WebAppKind": "webAppContainer",
                      "WebAppName": "azureappservice1",
                      "DeployToSlotOrASEFlag": "false",
                      "ResourceGroupName": "",
                      "SlotName": "production",
                      "DockerNamespace": "dockerusername",
                      "DockerRepository": "mydockerimage",
                      "DockerImageTag": "10"
    
                  }
    

    添加后,我可以使用您的请求正文创建发布定义。

    【讨论】:

    • 谢谢。在 Azure 门户中的哪里可以获取 taskId
    • 很高兴答案有所帮助。从 Azure DevOps,您可以导出 GUI 版本定义并在 json 中找到任何任务的taskId。或者您也可以使用其余 api 获取该信息 vsrm.dev.azure.com{organization}/{project}/_apis/release/definitions/{definitionId}?api-version=5.1>
    猜你喜欢
    • 2021-08-12
    • 2019-08-24
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 2020-08-03
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多