Continuous deployment
.NET Core Azure Azure DevOps Azure Function C# Continuous Integration DevOps Pipelines Triggers Web Hook

Application Integration with Azure DevOps Pipelines and Web Hooks

Welcome to today’s post.

In today’s post I will be showing how to integrate notifications and alerts into an Azure DevOps pipeline with Web Hooks.

Notifications are a useful feature in Azure DevOps build and release pipelines that help keep the project team members alerted to important milestones and issues that will occur during both the running of both the build and release pipelines.

Before I show how to configure and test notifications within an Azure DevOps pipeline, I will explain what Azure DevOps notifications are and when they are triggered.

What are Azure DevOps Pipeline Notifications?

A notification can be integrated into either or both a build or release pipeline. The notification is triggered from a variety of pipeline events.

These include events related to:

  • Build completion
  • Release completion
  • Code pushes
  • Pull requests (creation, comments, updates, branch merges)

A notification is created as a subscription to a Service Hook in an Azure DevOps project. This is done by selecting the Service hooks option under the Project Settings. In the Service Hooks pane, select the Create subscription button.

In the service hooks dialog, there are numerous services that can be integrated. These include Azure App Service, App Service Bus, Azure Storage, Slack, Trello, Microsoft Teams, Office 365, Web Hooks and so on.

In the next section, I will show how to create a web hook.

Creation of a Web Hook

What I will do is integrate an Azure function trigger HTTP POST request through a web hook.

In the service hooks subscription dialog, select Web Hooks, then Next.

The next screen in the dialog will show the event triggers to hook into. As I plan to integrate a notification in the completion of a build, I select the Build completed event that I will trigger from the drop-down list.  Then I click on Next.

In the FILTERS section, select the build pipeline that you will trigger the web hook for. If you don’t select a pipeline, the event will trigger on any pipeline build in the project.

In the Build Status filter field, select the Succeeded option.

In the next section, I will show how to create an HTTP trigger function.

Creation of an Azure HTTP Trigger Function

Before we complete the web hook, I will create an Azure HTTP trigger function that will be run from the pipeline web hook.

To create the Azure function app, I can either do this from the portal or by Azure CLI.  

Below is a sample Azure CLI Bash code: 

MY_RG_NAME='[your resource group]'
MY_FUNCTION_APP_NAME='BookLoanAppTrigger'$RANDOM$RANDOM
MY_STORAGE_ACCT='[your storage account]'
MY_LOCATION='[your location]'
MY_FUNCTION_VER='4'

To get a location use the Azure CLI query to obtain a list of locations:

az account list-locations -o table

To create the function app, use the command:

az functionapp create 
--resource-group $MY_RG_NAME 
--name $MY_FUNCTION_APP_NAME 
--consumption-plan-location $MY_LOCATION 
--storage-account $MY_STORAGE_ACCT 
--os-type Windows 
--runtime dotnet 
--functions-version $MY_FUNCTION_VER

After the function app is created in the CLI, open the Azure portal, and ensure the function app is visible.

Next, add a HTTP trigger function from the template.

The function app that I had created will be of the form:

https://bookloanapptrigger[some random number].azurewebsites.net

Once the HTTP trigger function has been created, you can select the Code + Test option to run a test on the HTTP trigger. When the Azure function is created, it will have a unique function key that is created for access via the HTTP POST request. For a more secure function you can also use a master key that has a secure access key. To retrieve the HTTP POST URL for the Azure function HTTP trigger, click on the Get function URL link, then copy the link to the clipboard. 

In the HTTP trigger code, you can amend the log information slightly to show when the pipeline is called as shown:

We just changed line:

log.LogInformation("C# HTTP trigger function processed a request.");

to

log.LogInformation("C# HTTP trigger function processed a request from Azure Devops Build Pipeline.");

Save the trigger event code change.

Click on the Test/Run action to test the HTTP trigger function.

Once the test request is run, the result will show in the Output tab as shown.

In the next section, I will show how to integrate the Web hook with the Azure HTTP trigger function.

Integration of the Web Hook with the Azure HTTP Trigger Function

Now we can complete the integration with the HTTP trigger Azure function. Paste in the Azure function HTTP trigger URL into the SETTINGS -> URL field as shown:

The URL field can be amended slightly to include the URL query string key and value which is additional data we can pass into the HTTP function trigger:

&name=Andrew%20Test  

The complete URL will be:

https://bookloanapptrigger[random number].azurewebsites.net/api/HttpTrigger1?code=[function key]&name=Andrew%20Test

Leave other fields intact, then click on Test.

If you see Succeed in the Summary tab, then call to the integrated HTTP trigger has been successful.

If you return to the HTTP trigger function resource in the Azure portal, you will see noticed that a second call to the HTTP trigger function has been made through the execution count charts for the Azure function. The first call we made was when we tested the HTTP trigger within the Azure portal.

From tests we executed on the HTTP trigger function from both the Azure portal and the Service hook integration setup in Azure DevOps, we can see three invocation traces from Application Insights monitoring of the function app:

Now that we know that the Azure function and HTTP trigger are setup and working as expected, we can proceed to complete the web hook subscription in Azure DevOps.

Click Finish to complete the web hook subscription in the New Service Hooks Subscription.

The Service Hooks pane will now show the new service hook subscription.

In the next section, I will show how to trigger the service hook from the DevOps pipeline.

Triggering the Service Hook from the Azure DevOps Pipeline

To be able to trigger our service hook from the pipeline, all we need do now is to run the build pipeline that we have filtered our web hook for.

Hit the Run pipeline action for the build pipeline.

The duration is takes to complete the build pipeline will depend on the number of tasks within our pipeline. In my build pipeline, I restore packages, build the source, run unit tests, determine code coverage, and copy tests over to the artifact storage, and publish the web application to artifacts storage.

As a refresher, refer to one of my recent posts, where I detailed how to create a build pipeline in Azure DevOps.  

To monitor progress of the web hook you can click on the web hook subscription to view the history of the web hook execution. Initially it will start off empty.

Once the build pipeline completes, note the start time of the pipeline and its duration. From here you can work out the time the build pipeline job has completed.

Now return to the Web hook history pane and refresh it. The status and completion time will show. Note that the history will show as UTC time compared to your Agent job which showed the start time in local time.

Back to the Execution Count for the Azure function in the portal, we notice that an additional execution has occurred. This will be in the local time of the agent that our build pipeline was run under.

For more details on the transaction request to our Azure function, open the end-to-end transaction details from Application Insights performance. From here you can access the request at the exact time it was received and the log that was output within the function trigger.

After completing the above, you can remove the Azure function and any resources that were created, including the Application Insights resource either from the portal or by CLI command az functionapp delete.

There are other variations to the above integration with Azure. We could also use the Azure function to send a record into Azure storage from the HTTP trigger function or use Azure service bus or Azure app service to send a message from a pipeline trigger event. We could also use one of the messaging services such as Slack or Microsoft Teams to send notifications.  

As I mentioned earlier, additional web hooks can be configured for other pipeline events, including those during a release pipeline, such as when a deployment into an environment succeeds or fails, or when a suite of E2E tests in a deployment stage fails.

That is all for today’s post.

I hope you have found this post useful and informative.

Social media & sharing icons powered by UltimatelySocial