Azure DevOps REST API 创建发布定义

Azure DevOps REST API to create a release definition

我正在尝试使用 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":是的, "isRetained": 错误 }], "releaseNameFormat": "Release-$(rev:r)", "environments":[{ "name": "development", "rank": 1, "retentionPolicy":{ "daysToKeep": 30, "releasesToKeep": 3, "retainBuild":真 }, "preDeployApprovals":{ "approvals": [ { "rank": 1, "isAutomated":是的, "isNotificationOn":错误, "id": 10 } ], "approvalOptions":{ "requiredApproverCount":空, "releaseCreatorCanBeApprover":假的, "autoTriggeredAndPreviousEnvironmentApprovedCanBeSkipped":错误, "enforceIdentityRevalidation":错误, "timeoutInMinutes": 0, "executionOrder": "beforeGates" } }, "postDeployApprovals":{ "approvals": [ { "rank": 1, "isAutomated":是的, "isNotificationOn":错误, "id": 12 } ], "approvalOptions":{ "requiredApproverCount":空, "releaseCreatorCanBeApprover":假的, "autoTriggeredAndPreviousEnvironmentApprovedCanBeSkipped":错误, "enforceIdentityRevalidation":错误, "timeoutInMinutes": 0, "executionOrder": "afterSuccessfulGates" } }, "deployPhases":[{ "deploymentInput":{ "parallelExecution":{ "parallelExecutionType": "none" }, "agentSpecification":{ "identifier": "ubuntu-16.04" }, "skipArtifactsDownload":假的, "artifactsDownloadInput":{ "downloadInputs": [] }, "queueId": 9, "demands": [], "enableAccessToken":错误, "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": 是的, "alwaysRun":假的, "continueOnError":错误, "timeoutInMinutes": 0, "overrideInputs":{}, "condition": "succeeded()", "inputs":{ "ConnectionType": "AzureRM", "WebAppKind": "webAppContainer", "WebAppName": "azureappservice1", "DeployToSlotOrASEFlag": "false", "ResourceGroupName": "", "SlotName": "production", "DockerNamespace": "dockerusername", "DockerRepository": "mydockerimage", "DockerImageTag": "10"<br> } }] }] }] }

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

我可以帮忙。在 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 DevOps REST API to create a release definition

根据报错信息:

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

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

正如我之前回答你post, this is very difficult and error-prone, if we add a huge request body completely manually. Usually, we use REST API Definitions - Get从类似的release pipeline中获取Response Body,然后我们通过修改Response Body来更新相应的属性。

作为测试,我在我的发布管道中添加任务 Deploy Azure App Service,然后我使用 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": ""
                        }
                    }
                ]

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

Where can I get taskId in Azure portal?

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

Can we use 'AzureContainerRegistry' as artifact type in the request body?

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

"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
    }
],

希望这对您有所帮助。