Using VSTS Service Hooks in Release Management to add resource cleanup resilience


In this post, Principal Consultant/App Dev Manager Lou Sawyer describes how to integrate webhooks and runbooks to cleanup failed Azure deployments.


Release Management capabilities in Visual Studio Team Service are fantastic and offers extensibility points to make everyone's life easier. In this post, we'll be discussing the Service Hooks feature along with a potential use case integrating with a Runbook.

As detailed in Integrate with service hooks, Service Hooks allow you to perform tasks in other services in response to events in VSTS. This capability can be used to start a runbook from a webhook (runbooks and use cases are covered in Azure Automation User Documentation) to perform conditional tasks after a VSTS Release.

One common use of Release Deployments is to perform testing that can involve a significant number of resources in Azure to complete. By default, when a deployment fails in VSTS, it stops the deployment at that time and does not continue in order to give you a chance to investigate the failure.  This can lead to resources continuing to be available longer than needed adding cost to the environment. The option does exist to configure to continue on error, however this is not conditional and may result in more task failure (or the environment being deleted when you would want to investigate). In such a scenario, use a webhook to starts a runbook on deployment failure to examine which task failed and clean-up the environment.

Consider the image shown below, in this case there are intermittent failures and this release creates resources in a resource group in Azure. Depending on when it fails, these resources may result in unwanted overhead.

clip_image002

clip_image004

To mitigate this scenario, consider using a runbook. The image below shows a PowerShell Workflow runbook being created to handle a failed release event

clip_image006

The workflow needs to take one parameter named webhookData which will contain the body of the post request from VSTS.  The request body of webhookData then needs to be deserialized so we can examine the release details.  In the code below, we are walking through the environment deployment phase tasks to find the first task where we know the resourceGroupName and location are used as parameters.  We can use these values to test if the resourceGroupName exists at the specified location and remove it; a more elaborate script would iterate through all the tasks to find where it failed and then decide to remove the resource group or not.

clip_image008

The next step is to create a webhook for the runbook. This is covered fairly well in Starting an Azure Automation runbook with a webhook. As a word of caution, the URL for the webhook cannot be retrieved after creation, so save it before you click Create!

clip_image010

clip_image012

clip_image014

clip_image016

clip_image018

At this point, we need to create the Service Hook in VSTS. This can be done from a Service Hooks settings as shown below

clip_image020

From the Service Hooks pane, you have the option to create a subscription, choose it to create a Service hook.

clip_image022

There are lots of services you can send events to, in this case we are interested in Web hooks; this topic is covered in VSTS documentation on Web Hooks.

clip_image024

The trigger options can be narrowed to a specific release, environment, and status.  In this case, we are interested in failed deployments to UAT for TailSpin Toys as shown below.

clip_image026

The next window allows you to paste the URI generated when you created the webhook for your runbook automation.  There are a lot of options for message detail and it’s important to note that the highest level of detail (All) is the default. This option contains a lot of information about your release so it's important to use secure connections.  In this case, we need the full release details to pull out specific information from the tasks and any conditional work we want to do.

clip_image028

Finally, the service hook is done and the runbook is ready.  The next time a deployment fails, you should see an output in your runbook jobs like the screen below. As the snapshot shows, the failed deployment resource groups were found and removed.

clip_image030

There is a lot of power and flexibility in the Service Hooks feature in VSTS.  Hopefully this gives some additional ideas on how to do conditional processing on a release that needs attention.

 


Premier Support for Developers provides strategic technology guidance, critical support coverage, and a range of essential services to help teams optimize development lifecycles and improve software quality.  Contact your Application Development Manager (ADM) or email us to learn more about what we can do for you.

Comments (0)

Skip to main content