Using Visual Studio Online Service Hooks with Azure Automation Webhooks

Author by Christopher Mank

UPDATE:  Check out this post on the Azure Blog! If you weren't able to attend //Build or Ignite a couple weeks ago, hopefully you've had the chance to watch some of the recorded sessions on the great new features coming to the Microsoft stack of products.  The two I'm most excited about are Visual Studio Online (VSO) and Azure Automation.  Two really great product sets to assist you in your ALM/DevOps journey. Of all the current and soon to be released features in VSO, I'm most excited about the extensibility options.  One that was announced last year is Service Hooks.  Service Hooks let you perform tasks on other services when something happens in a VSO project (e.g. new build created, work item updated, etc.).  One of the available services is a Webhook.  A Webhook can post JSON data about the event from VSO to any URL of your choice.  So that got me thinking… With the recent announcement of Azure Automation Webhooks, you could trigger a custom Runbook hosted in Azure to perform any action you want when an event happens in VSO.  Is that not awesome!?  Think of the possibilities this could open for your DevOps adventures. To prove this works, I created a Runbook in Azure Automation that will receive data from VSO and email it out (via O365) to the addresses specified.  Here are the steps to create this automation. 1.  Browse to portal.azure.com and login to your organization's Azure portal. From here, click on Browse --> Automation Accounts. 2.  On the Automation Accounts blade, you can choose to create a new account or put the Runbook in an existing Automation Account. 3.  From the Automation Account click on Assets --> Credentials. Create a new credential of the username/password of the user you'd like to have send the email. Make note of the name you assigned to the credential. AzureAutomationCredential 4.  Browse to the Microsoft Script Center and download the Send-VsoEmail script (found here). 5.  Open the script and make the following changes per your environment: 5a.  Line #30 – Update the credential asset name to what you set up in Step #3. 5b.  Line #31 – Update the array of email addresses that will be added to the To field of the email. 5c.  Line #32 – Update the SMTP server you wish to use. 6.  Browse back to the Automation Account and from there click Add a runbook --> Import an existing runbook. Select the file you modified from step #5 and it will create the VsoSendEmail Runbook. NOTE: The Runbook will be created with the same name as the file. If you would like to use a different name, simply rename the file before importing. NewAzureAutomationRunbook ImportAzureAutomationRunbook 7.  You should now have a Runbook in your environment that looks like this. VsoSendEmailRunbook 8.  Next, we need to create a Webhook in Azure Automation, allowing us to kick off the Runbook from VSO. From the VsoSendEmail Runbook, click on Webhooks --> Create new webhook. Make sure you save off the URL because once you click Create, there is no way to get it back. We now have a Webhook that allows us to call that Runbook from VSO (and really anywhere for that matter). CreateNewAzureAutomationWebhook 9.  Browse to your Visual Studio Online account and the project you wish to integrate with. From the project home page, click on the gear in the top-right corner. VsoProjectSettings 10.  From the control panel page, click on the Service Hooks tab. Then create a new Web Hook subscription. VsoServiceHook 11.  Set the Trigger value to Work item created. You can build integrations off any of the events listed but we'll use this one for our purposes. VsoServiceHookTrigger 12.  In the URL field, enter in the Webhook address created from step #8. You have the option to configure additional settings but for our purpose, we'll configure it as shown below. Then click Test. NOTE: For our email to pull the data correctly, you must at a minimum send the Resource details. VsoServiceHookAction 13.  When you click Test, it will send test data to our Azure Automation Runbook via the Webhook. Assuming everything is OK, you should get a success message like below. VsoServiceHookTest 14.  Since our test is a live test, you should receive the email with the proper data. VsoServiceHookTestEmail 15.  To conclude our test, let's create a real item and make sure it works. Browse to the Backlog view of your VSO project and create a new Work Item. VsoNewBacklogItem 16.  We should now get a new email, notifying us of the new backlog item that was created. VsoNewBacklogItemEmail And there you have it!  You now have the power to build all sorts of cool integrations using Visual Studio Online Service Hooks with Azure Automation Webhooks. Well done, Christopher
Author

Christopher Mank

Systems Architect