繁体   English   中英

尝试使用 Azure DevOps Rest API 创建发布定义时出错

[英]Error while trying to create a release definition using Azure DevOps Rest API

我正在使用以下脚本在 Azure DveOps 中使用 PowerShell 脚本创建发布定义,但它失败并显示如下错误消息。 不确定在哪里提供了不正确的详细信息。

Invoke-RestMethod: {"$id":"1","innerException":null,"message":"VS402903: 指定的值不能转换为 ReleaseDefinition 类型。请确保它可以转换为 ReleaseDefinition 类型,然后重试。"

代码片段是:

Param( 
[string]$organisation = "ORGNAME", 
[string]$project = "PROJECTNAME", 
[string]$keepForever = "true", 
[string]$user = "userid", 
[string]$token = "tokenID" ) 

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
Write-host $base64AuthInfo

$postresults = "https://vsrm.dev.azure.com/$organisation/$project/_apis/release/definitions?api-version=6.0"
Write-host $postresults

 $body = 
@"
{  "name": "New release pipeline ",
     "comment": "test",
    "definitionId": 860,
    "description": "Create Release from PowerShell",
    "artifacts": [],
    "isDraft": false,
    "reason": "Demo purpose",
    "manualEnvironments": null,
    "environmentsMetadata": null, 
    "properties": null, 
    "variables": null
    "environments": [
    {
      "name": "PROD",
      "preDeployApprovals": {
        "approvals": [
          {
            "rank": 1,
            "isAutomated": false,
            "isNotificationOn": false,
            "approver": {
              "displayName": null,
              "id": ""
            },
            "id": 0
          }
        ]
      },
      "postDeployApprovals": {
        "approvals": [
          {
            "rank": 1,
            "isAutomated": true,
            "isNotificationOn": false,
            "id": 0
          }
        ]
      },
      "deployPhases": [
        {
          "deploymentInput": {
            "parallelExecution": {
              "parallelExecutionType": "none"
            },
            "skipArtifactsDownload": false,
            "artifactsDownloadInput": {},
            "queueId": 391,
            "demands": [],
            "enableAccessToken": false,
            "timeoutInMinutes": 0,
            "jobCancelTimeoutInMinutes": 1,
            "condition": "succeeded()",
            "overrideInputs": {}
          },
          "rank": 1,
          "phaseType": "agentBasedDeployment",
          "name": "Run on agent",
          "workflowTasks": []
        }
      ],
      "retentionPolicy": {
        "daysToKeep": 30,
        "releasesToKeep": 3,
        "retainBuild": true
      }
    }
  ],
  "path": "\\",
  "releaseNameFormat": "Release",
  "description": ""
} 
"@ | ConvertTo-Json -Depth 100

$result = Invoke-RestMethod -Uri $postresults -Method Post -Body $body -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

if ($result.count -eq 0)
{
     throw "Unable to locate Release Definition Id $($definitionId)"
}
else
{
    Write-host "Success!!!"
}

您在 json 主体中几乎没有问题:

  1. description存在两次。

  2. 您在“变量”附近缺少, "variables": null

  3. 您需要在preDeployApprovals中指定一个有效的id

     "approver": { "displayName": null, "id": "PUT-HERE-ID" },
  4. 将上述部分也添加到

  5. 您不需要再次将其转换为 json,删除| ConvertTo-Json -Depth 100 代码中的| ConvertTo-Json -Depth 100

暂无
暂无

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

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