Skip to content

Instantly share code, notes, and snippets.

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.
if ($WebhookData){
$requestBody = (ConvertFrom-Json -InputObject $WebhookData.RequestBody)
$refspec = $requestBody.resource.SourceRefName
$branchName = $requestBody.resource.SourceRefName.Split("/")[-1]
$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)"
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.