【问题标题】:Error while trying to create a release definition using Azure DevOps Rest API尝试使用 Azure DevOps Rest API 创建发布定义时出错
【发布时间】:2021-08-12 13:02:04
【问题描述】:

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

调用-RestMethod : {"$id":"1","innerException":null,"message":"VS402903: 指定 value 不能转换为 ReleaseDefinition 类型。确保它是 可转换为 ReleaseDefinition 类型,然后重试。”

代码sn-p是:

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

【问题讨论】:

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


    【解决方案1】:

    您在 json 正文中有几个问题:

    1. description 存在两次。

    2. 您在"variables": null 附近缺少,

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

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

    5. 你不需要再转换成json,把| ConvertTo-Json -Depth 100从代码中去掉。

    【讨论】:

    • 感谢 Shayki,我已经进行了更改,现在我收到此错误。 Invoke-RestMethod : {"$id":"1","innerException":null,"message":"找不到标识符为 391 的代理池。","typeName":"Microsoft.TeamFoundation.DistributedTask.WebApi.TaskAgentQueueNotFoundException, Microsoft.TeamFoundation.DistributedTask.WebApi","typeKey":"TaskAgentQueueNotFoundException","errorCode":0,"eventId":3000}
    • queueId391 更改为有效数字。你可以去这里https://dev.azure.com/{org-name}/_settings/agentpools,输入一个池,然后在 URL 中查看 id。
    • 你能建议我在下面的链接中查询吗? stackoverflow.com/q/67670649/13450322
    猜你喜欢
    • 2022-01-02
    • 2020-05-23
    • 2020-10-04
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 2021-03-19
    • 2020-09-04
    相关资源
    最近更新 更多