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

Posted

技术标签:

【中文标题】尝试使用 Azure DevOps Rest API 创建发布定义时出错【英文标题】:Error while trying to create a release definition using 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!!!"

【问题讨论】:

【参考方案1】:

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

    description 存在两次。

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

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

        "approver": 
      "displayName": null,
      "id": "PUT-HERE-ID"
    ,
    

    将上述部分也添加到

    你不需要再转换成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。 你能建议我在下面的链接中查询吗? ***.com/q/67670649/13450322

以上是关于尝试使用 Azure DevOps Rest API 创建发布定义时出错的主要内容,如果未能解决你的问题,请参考以下文章

Azure DevOps REST API - 选项列表如何与字段关联?

使用 .Net 客户端 SDK 获取 Azure DevOps 团队

无法在 PowerShell 中使用 REST API 删除 DevOps 分支

如何使用 Azure DevOps REST Api 编写构建计划脚本?

Azure devops Server 2019中的Analytics Widget-寻找Rest api和class libraby用于Analytics Widget

Azure DevOps 发布 Api 400 错误请求错误