Created
September 8, 2017 00:54
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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