Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Azure Automation Runbook to parse a VSTS merge commit hook and delete the resource group corresponding to the source branch identified therein.
[CmdletBinding()]
param([object]$WebhookData)
if ($WebhookData){
$requestBody = (ConvertFrom-Json -InputObject $WebhookData.RequestBody)
$refspec = $requestBody.resource.SourceRefName
$branchName = $requestBody.resource.SourceRefName.Split("/")[-1]
if($branchName){
$servicePrincipalConnection = Get-AutomationConnection -Name AzureRunAsConnection
"Logging in to Azure..."
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
Write-Output "Starting to remove resource group: $($branchName)"
Remove-AzureRmResourceGroup -Name $branchName -Force
if ((Get-AzureRmResourceGroup -Name $($branchName)-ErrorAction SilentlyContinue) -eq $null) {
Write-Output "...successfully removed resource group: $($branchName)"
}
}
}
else
{
Write-Error -Message 'Runbook was not started from Webhook' -ErrorAction stop
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.