Skip to content

Instantly share code, notes, and snippets.

@gavincampbell
Created September 8, 2017 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gavincampbell/a12a87477bd38402b310262040509a56 to your computer and use it in GitHub Desktop.
Save gavincampbell/a12a87477bd38402b310262040509a56 to your computer and use it in GitHub Desktop.
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