Hello Azure DevOps Specialists
I do have in Azure DevOps some old build pipelines, that I want to delete using Power Shell. I wrote a little power shell script to get the build definitions first and then get the build id, name, etc. Before I can delete a definition, the retained releases must be deleted. The program code is prepared, but the API requires an ownerId or userId or Minimal Retention Leases. And that is my challenge!
I read the documentation https://learn.microsoft.com/en-us/rest/api/azure/devops/build/leases?view=azure-devops-rest-6.0 and the code behind (hosted on GitHub https://github.com/microsoft/azure-devops-node-api/blob/470f9ca7bdfccd87e1c1fdea8023b8c3d2b1047a/api/interfaces/BuildInterfaces.ts#L2006), but I have no idea what the ownerId or userId is or how I can find it.
$personalToken = "AzureDevOpsPersonalToken"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))
$header = @{authorization = "Basic $token"}
$organization = "myOrga"
$project = "myProj"
$pipelineName = Read-Host "Please enter pipeline to delete"
#all build definitions
$url = "https://dev.azure.com/$organization/$project/_apis/build/definitions?api-version=6.0-preview.7"
$builddefinitions = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header
$builddefinitions.value | Sort-Object id|ForEach-Object {
Write-Host $_.id $_.name $_.queueStatus
if ( $_.name -ne $pipelineName ) {
return;
}
#all builds for a definition
$url = "https://dev.azure.com/$organization/$project/_apis/build/builds?definitions=" + $_.id + "&api-version=6.0-preview.5"
$builds = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header
$builds.value | Sort-Object id|ForEach-Object {
#report on retain status
Write-Host " BuildId:" $_.id " retainedByRelease:" $_.retainedByRelease
# ownerId UNKNOWN - where is this come from?
$url = "https://dev.azure.com/$organization/$project/_apis/build/retention/leases?ownerId=&api-version=6.0-preview.1"
$leases = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header
Write-Host $leases
#$url = "https://dev.azure.com/$organization/$project/_apis/build/retention/leases?ids=" + $_.id + "&api-version=6.0-preview.1"
#Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header
#Write-Host " BuildId:" $_.id " removed retentions"
#
#$url = "https://dev.azure.com/$organization/$project/_apis/build/builds/" + $_.id + "?api-version=6.0-preview.1"
#Invoke-RestMethod -Uri $url -Method DELETE -ContentType "application/json" -Headers $header
#Write-Host " BuildId:" $_.id " deleted"
}
#$url = "https://dev.azure.com/$organization/$project/_apis/build/definitions/" + $_.id + "?api-version=6.0-preview.1"
#Invoke-RestMethod -Uri $url -Method Delete -ContentType "application/json" -Headers $header
#
#Write-Host " Pipeline:" $pipelineName " (" $_.id ") deleted"
Write-Host
}
Can you help me? Thank you in advance Tino