Reputation: 949
I have been working on a requirement with a release API to retain all the production releases.
Code -
param (
[string]$token="",
[string]$collection="",
[string]$projectName =""
)
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $token)))
$response = Invoke-RestMethod "https://vsrm.dev.azure.com/$collection/$projectName/_apis/release/definitions?`$expand=Environments&`$top=1000&api-version=6.0" -Method 'GET' -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)}
foreach ($releaseDefinition in $response.value){
write-host $releaseDefinition.name
write-host $releaseDefinition.id
$releaseDefinitionid = [convert]::ToInt32($releaseDefinition.id)
write-host "--------------------------------------------------"
[string] $releases = "https://vsrm.dev.azure.com/$collection/$projectName/_apis/release/releases?definitonid="
[string] $defid = $releaseDefinitionid
[string] $geturl = $releases + $defid + "&api-version=6.0"
$releaseresult = Invoke-RestMethod $geturl -Method 'GET' -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)}
#Write-Host $releaseresult.value
#Write-host $releaseresult.keepForever
foreach ($retain in $releaseresult.value)
{
Write-host $retain.id
$temp = $retain.id.ToString()
$id = "https://vsrm.dev.azure.com/$collection/$projectName/_apis/release/releases/$temp/?api-version=6.0"
$retainrelease = Invoke-RestMethod $id -Method 'GET' -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)}
if( $retainrelease.environments.name -eq 'PRD' -and $retainrelease.environments.status -eq 'succeeded')
{
if([string]$retainrelease.keepForever -eq 'False')
{
$keepforever = @{
keepforever='true'}
$jsonKeepForever = $keepforever | ConvertTo-Json -Depth 100
$uriForBuildUpdate = "https://vsrm.dev.azure.com/$collection/$projectName/_apis/release/releases/$temp/?api-version=6.0"
$patchreq = Invoke-RestMethod -Uri $uriForBuildUpdate -Method Patch -Body $jsonKeepForever -ContentType "application/json" -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)}
Write-Verbose "Result: $patchreq" -Verbose
}
}
else
{
}
}
}
It retains the releases, but the requirement is to retain only release definitions created in the last 60 days (or a particular date) and retain only releases happened in last 10 days to prod.
I couldn't find any querystring param to filter it by. How can I do this?
Upvotes: 0
Views: 774
Reputation: 11
I found that you can get a list of associated environments like prod/stage/dev (for each definition) by adding the $expand=environments
to query string.
https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/deployments?$expand=environments&api-version=6.0
So you'll get the list of all environments and their ids for each definition. The ids can be used as definitionEnvironmentId
for the other endpoints that seem to have what you're looking for (e.g.: maxCreatedTime
or minCreatedTime
) like this one for Releases:
Reference:
List of properties that can be expanded - https://learn.microsoft.com/en-us/rest/api/azure/devops/release/definitions/list?view=azure-devops-rest-6.0#releasedefinitionexpands
Upvotes: 1