【问题标题】:Updating release def results in error VS402903: The specified value is not convertible to type ReleaseDefinition更新 release def 导致错误 VS402903: The specified value is not convertible to type ReleaseDefinition
【发布时间】:2020-12-03 08:58:26
【问题描述】:

我正在使用本地 Azure DevOps API 使用以下 powershell 脚本更新版本定义:

$listurl="https://onpremdomain/{ogr}/{proj}/_apis/release/definitions?api-version=6.0"

$PAT="Personal access token"

$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))

#get the releases' ids.
$result = Invoke-RestMethod -Uri $listurl -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -Method get

#loop the ids to get each release's definition
foreach($release in $result.value){
 
#get each release's definition
 $definitionurl="https://onpremdomain/{ogr}/{proj}/_apis/release/definitions/$($release.id)?&api-version=6.0"
   
  $releaseDefinition = Invoke-RestMethod -Uri $definitionurl-Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -Method get

  #loop through each stage 
  foreach( $environment in $releaseDefinition.environments){
   
     #loop through each tasks to find the task group
     foreach($task in $environment.deployPhases.workflowTasks){
      
       # change the 'taskId'  to the taskId of your task group
        if($task.taskId -eq "{taskId}"){
           
            $task.version = "2.*"  # update the taskgroup version to the newest version
       }
     }
  }

    $updateurl="https://onpremdomain/{ogr}/{proj}/_apis/release/definitions?api-version=6.0"
   
    # update the release definition 
    Invoke-RestMethod -Uri $updateurl -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -ContentType "application/json" -Method PUT -Body (convertto-json $releaseDefinition -Depth 100)
}

当调用最后一行中的 Invoke-RestMethod 时,出现以下错误:

 Invoke-RestMethod : {"$id":"1","innerException":null,"message":"VS402903: The specified value is not convertible to type ReleaseDefinition. Make sure it is convertible to type ReleaseDefinition and 
try again.","typeName":"Microsoft.VisualStudio.Services.ReleaseManagement.Data.Exceptions.InvalidRequestException, Microsoft.VisualStudio.Services.ReleaseManagement2.Data","typeKey":"InvalidRequestE
xception","errorCode":0,"eventId":3000}

知道为什么会出现这个错误吗?

【问题讨论】:

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


    【解决方案1】:

    REST API 文档:

    Get a list of release definitions.

    Get a release definition.

    Update a release definition.

    Power shell 脚本

    $listurl="https://vsrm.dev.azure.com/{Org name}/{Project name}/_apis/release/definitions?api-version=6.0"
    $PAT="{PAT}"
    $base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
    
    #get the releases' ids.
    $result = Invoke-RestMethod -Uri $listurl -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -Method get
    
    write-host $result.value.id
    
    #loop the ids to get each release's definition
    foreach($release in $result.value){
     
    #get each release's definition
    $definitionurl="https://vsrm.dev.azure.com/{Org name}/{Project name}/_apis/release/definitions/$($release.id)?api-version=6.0"
    #write-host $definitionurl
    $releaseDefinition = Invoke-RestMethod -Uri $definitionurl -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -Method get
    
      #loop through each stage 
      foreach( $environment in $releaseDefinition.environments){
       
         #loop through each tasks to find the task group
         foreach($task in $environment.deployPhases.workflowTasks){
         write-host $task.taskId 
           # change the 'taskId'  to the taskId of your task group
            if($task.taskId -eq "e213ff0f-5d5c-4791-802d-52ea3e7be1f1"){
                write-host $task.version
                $task.version = "1.*"  # update the taskgroup version to the newest version
                write-host $task.version
           }
         }
      }
        $updateurl="https://vsrm.dev.azure.com/{Org name}/{project name}/_apis/release/definitions?api-version=6.0"
       
        # update the release definition 
        Invoke-RestMethod -Uri $updateurl -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -ContentType "application/json" -Method PUT -Body (convertto-json $releaseDefinition -Depth 100)
    }
    

    更新

    我发现了问题,第二个Invoke-RestMethod方法在-Headers参数前缺少空格,问题应该是Invoke-RestMethod : A positional parameter cannot be found that accepts argument 'System.Collections.Hashtable'.

    在参数前加个空格后就可以了,可以看下图。

    【讨论】:

    • 谢谢!但即使对于尚未更新的发布定义,我仍然会遇到同样的错误
    • 嗨@doorman,我已经更新了我的答案,请检查它,然后在这里分享结果。如果问题仍然存在,您介意在此处分享屏幕截图吗?如上。谢谢
    • 嗨@doorman,刚刚签到看看这个问题现在是否仍然阻碍你?这个问题有更新吗?
    • 嗨@VitoLiu-MSFT 感谢您的帮助,我仍然遇到同样的错误,所以我决定现在手动更新它。使用 API 感觉有点像 hack,我觉得它可能会对版本产生意想不到的变化。因此,我在每个环境中将所有版本更新为版本 2 任务组。我喜欢使用 draft->preview->publish 方法编辑任务组的简单方法,但它不支持将最新版本发布到多个版本。我们可能只会更新相同的版本,直到 DevOps UI 支持多更新功能。
    • 嗨@doorman,我发现了这个问题,我已经更新了答案,你可以检查更新,它应该可以工作,请检查它,然后在这里分享结果。
    猜你喜欢
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 2022-12-19
    • 2013-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多