Azure DevOps REST api 添加持续部署触发器

问题描述 投票:0回答:1

我正在尝试向现有的一组发布触发器添加一个新分支。我可以查看发布定义的 URL 内容,但是 PATCH 操作失败并出现以下错误。有人可以帮助我审查和完善我的代码吗?

"innerException": null,   "message": "VS403051: Could not delete pipeline with ID 515 because it was not found. Specify a valid ID     | and try again.",   "typeName": "Microsoft.VisualStudio.Services.ReleaseManagement.Data.Exceptions.DeletedReleaseDefinitionNotFoundException

参考代码

$releaseDefinitionId = "515"
$pat = "test_pat_token"

# Define the continuous deployment trigger
$continuousDeploymentTrigger = @{
    triggerType = "continuousIntegration"
    settingsSourceType = "definition"
    branchFilters = @(
        "+refs/heads/develop"
    )
}

# Convert the continuous deployment trigger to JSON format
$body = @{
    triggers = @(
        $continuousDeploymentTrigger
    )
} | ConvertTo-Json

# Define the REST API URL
$url = "https://vsrm.dev.azure.com/org/project/_apis/release/definitions/$($releaseDefinitionId)?api-version=7.1-preview.4"

# Invoke the REST API to update the release definition
Invoke-RestMethod -Uri $url -Method Patch -Headers @{
    Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($pat)"))
} -Body $body -ContentType "application/json"
azure-devops ado azure-pipelines-release-pipeline azure-devops-rest-api
1个回答
0
投票

要更新发布定义,您应该使用 REST API“Definitions - Update”和

PUT
方法。

  • 首先需要调用API“Definitions - Get”获取发布定义的响应体。

  • 通过将新的分支过滤器添加到 CD 触发器来更新响应正文。

  • 最后,您可以使用新的正文调用API“Definitions - Update”来更新发布定义。

下面是一个 PowerShell 脚本示例作为参考。

$organization = "{organization}"
$project = "{project}"
$definitionId = "{definitionId}"
$branchName = "{branchName}"  # e.g., develop

# Need to convert the PAT to be a base64 string.
$pat = "{pat}"
$base64Token = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $pat)))

# Set HTTP request headers.
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", ("Basic {0}" -f $base64Token))

# Get the release definition that needs to be updated.
$getDef_uri = "https://vsrm.dev.azure.com/${organization}/${project}/_apis/release/definitions/${definitionId}?api-version=7.0"
$Def = Invoke-RestMethod -Method GET -Uri $getDef_uri -Headers $headers

# Set the new branch filter that will be added to the CD trigger of the release definition.
$newTrigger = @{
    sourceBranch = "$branchName"
    tagFilter = $null
    useBuildDefinitionBranch = $false
    createReleaseOnBuildTagging = $false
}

# Add the new branch filter to the CD trigger. Get a new json body of the release definition. 
$triggerConditions = $Def.triggers[0].triggerConditions
$triggerConditions += $newTrigger
$Def.triggers[0].triggerConditions = $triggerConditions
$updateDef_body = @($Def) | ConvertTo-Json -Depth 100

# Update the release definition with the new json body.
$updateDef_uri = "https://vsrm.dev.azure.com/${organization}/${project}/_apis/release/definitions?api-version=7.0"
$updateDef = Invoke-RestMethod -Method PUT -Uri $updateDef_uri -Body $updateDef_body -ContentType "application/json" -Headers $headers
Write-Host $($updateDef | ConvertTo-Json -Depth 100)


© www.soinside.com 2019 - 2024. All rights reserved.