Umbraco allows you to setup post deployment webhooks and you can use these to perform all kinds of post deployment tasks (notifying community members, announcement on Slack channels, signaling a monitoring application, etc.). For this post we will trigger custom code in Azure that sends out a simple email notification, but that can easily be extended into more complex scenarios.
For more information on Umbraco Cloud Webhooks, see the official documentation:
https://our.umbraco.org/documentation/Umbraco-Cloud/Deployment/Deployment-Webhook
Good to know: you can configure a webhook per Umbraco Cloud Project environment….very convenient!
So only 2 things we need:
1. An Azure Function waiting for our webhook call;
2. A webhook url for an Umbraco Cloud Project environment.
Setting up Azure
Azure Functions is a serverless compute service that enables you to run code on-demand without having to explicitly provision or manage any infrastructure. You can use Azure Functions to run a script or piece of code in response to a variety of events. In our case we need to respond to a webhook sent by Umbraco Cloud upon deployment.
A detailed description on Azure Functions is available Microsoft Docs: https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-azure-function
In short:
Create the Azure Function App by logging into the Azure Portal and clicking the New button found on the upper left-hand corner of the Azure portal. Then select Compute > Function App.
Name the app and configure the required settings, hit the Create button to provision the Function App.
Next step: create a function within our new Function App.
Our function will work with the incoming request data from the Umbraco webhook call and send out an email using the SendGrid service. To make SendGrid work with our Async method, we need to switch to Azure Functions V2. The following blogpost provides more information on why and how we need to do this: https://blogs.technet.microsoft.com/livedevopsinjapan/2017/12/04/azure-functions-v2-async-sample-with-sendgrid
Switching is easy and can be done through the function app settings:
Now we are ready to create our function. You can add it through the UI and paste the required code or start with the template “Webhook + API” and CSharp. Either way you should end up with the following code for the run.csx:
#r "Newtonsoft.Json" #r "SendGrid" using System.Net; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Primitives; using Newtonsoft.Json; using SendGrid.Helpers.Mail; using System.Text; public async static Task Run(HttpRequest req, IAsyncCollector messages, TraceWriter log) { log.Info("SendGrid message"); using (StreamReader reader = new StreamReader(req.Body, Encoding.UTF8)) { var body = await reader.ReadToEndAsync(); var subject = String.Empty; dynamic data = JsonConvert.DeserializeObject(body); subject = data?.ProjectName; var message = new SendGridMessage(); message.AddTo("xyz@outlook.com"); message.AddContent("text/html", body); message.SetFrom("xyz@domain.com"); message.SetSubject(subject); await messages.AddAsync(message); return (ActionResult)new OkObjectResult("The E-mail has been sent."); } }
And for function.json:
{ "bindings": [ { "authLevel": "function", "name": "req", "type": "httpTrigger", "direction": "in" }, { "name": "$return", "type": "http", "direction": "out" }, { "type": "sendGrid", "name": "messages", "apiKey": "SGKEY", "to": "xyz@outlook.com", "from": "xyz@domain.com", "subject": "New deployment!", "direction": "out" } ] }
Tip: for continuous deployment scenarios you can setup a deployment source like GitHub, a local Git repo or even sources like Dropbox or OneDrive! See this link for more information: https://docs.microsoft.com/en-us/azure/azure-functions/functions-continuous-deployment
Before we head over to our Umbraco Cloud project, we need to take note of our function app url:
This url includes our api key (code) which we need to be able to call our function.
Configure webhook
Configuring the webhook is straightforward: just input the url of our function app and add the webhook.
And now we trigger the webhook by deploying a change to the Development environment. If all goes well we should see some logging in Azure and eventually receive the email containing our project info.
That is all you need to make this work. You can find the source code used in this post on GitHub: https://github.com/yuriburger/umbraco-webhook-demo
/Y.