Azure DevOps – Undelete a Release Pipeline

A few months ago I deleted a folder with inside a release definition.
My friend Alessandro Alpi helped me a lot pointing me to this msdn article.
With the PowerShell script that you find following the link you can restore a deleted release pipeline but only within 4 weeks of the deletion.

I write down the PowerShell script here for future memory in case the MSDN blog post is moved.


param (
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string] $token
)
## Construct a basic auth head using PAT
function BasicAuthHeader()
{
param([string]$authtoken)
$ba = (":{0}" -f $authtoken)
$ba = [System.Text.Encoding]::UTF8.GetBytes($ba)
$ba = [System.Convert]::ToBase64String($ba)
$h = @{Authorization=("Basic{0}" -f $ba);ContentType="application/json"}
return $h
}

# Fill in your account name/project name/definition name
$accountName = "aseemb"
$projectName = "dockerConf"
$definitionNameToRecover = "New Release Definition"
# Find the Id of release definition that got deleted
$deletedReleaseDefinitionsUri = "https://$accountName.vsrm.visualstudio.com/$projectName/_apis/Release/definitions?api-version=4.0-preview.3&isDeleted=true&searchText=$definitionNameToRecover"
$h = BasicAuthHeader $token
$deletedDefinitions = Invoke-RestMethod -Uri $deletedReleaseDefinitionsUri -Headers $h -ContentType “application/json" -Method Get
$deletedDefinitionJSON = $deletedDefinitions | ConvertTo-Json -Depth 100
write-host "Found the deleted definitions : $deletedDefinitions"
$deletedReleaseDefinitionId = $deletedDefinitions.Value[0].id
write-host "Found the deleted id : $deletedReleaseDefinitionId " # Recover the deleted release definition
$undeleteReason = '{ "Comment" : "Deleted by mistake" }'
$undeleteReleaseDefinitionUri = "https://$accountName.vsrm.visualstudio.com/$projectName/_apis/Release/definitions/$deletedReleaseDefinitionId`?api-version=4.0-preview.3"
$undeletedDefinition = Invoke-RestMethod -Uri $undeleteReleaseDefinitionUri -Headers $h -ContentType “application/json" -Method Patch -Body $undeleteReason
$name = $undeletedDefinition.name
write-host "$name recovered successfully"



Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.