Write Pipeline Failures to a Teams Channel
Tags: testing, azure, dotnet
Published: 2022-08-29 15:18:00

It can be super useful to write Azure DevOps pipeline failure events to a Teams Channel. It’s a easy way for the whole development team to know when something has gone wrong.

In the below example a failed automation pipeline run sends a notification to the an Automation Teams Channel that I created. The notification contains a link to the failed pipeline job in Azure DevOps.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eowqdbykmlrvkj2ii7vm.png)

**Setup:**

1) Select or create a Teams Channel to write failures to. I recommend creating a new dedicated channel for this to reduce noise from other notifications.

2) Select the Channel’s … menu then select “Connectors”

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zek7gvraw6i5954l8v6n.png)

3) On the Connectors page select “Incoming Webhook” then select the Configure button

4) Give the Webhook a name and select the Create button

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rw03sviyvxln2cql9z0d.png)

5) A Webhook URL will be created. Copy and save this URL then select the Done button. You will use this URL in your pipeline yml.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/685owkiac148p6l1bto3.png)

6) Add a step to your yml pipeline to write test failures to the Teams Channel. The step should run after your test execution. 

In the pipeline yml create a new _variable_ for your Team Channel Url using the Url value you copied above:

```
– name: TeamChannelUrl
value: 'https://nuancecommunications.webhook.office.com/webhookb2/2fe27450-5aff-4d80-9061-184ae51d90f2@29208c38-8fc5-4a03-89e2-9b6e8e4b388b/IncomingWebhook/6b8bfe1977214326ab198271cbf5fc93/b50aa873-d730-42b2-b5b2-26xa43abea7d' 
```

After adding the variable in the pipeline yml add a new _Powershell_ stage that writes failures to the Teams Channel. The step should be added after your automation tests (or pipeline) has been executed. 

Below is the _Powershell_ code you need that will grab the Azure Devops build information and send it to the Teams Channel. Update the title to reflect the automation/pipeline that was executed. In the below example I pass in the _test_environment_ and _test_category_ parameters that I am using in pipeline to further identify what failed.

```
– powershell: |
$body = [PSCustomObject][Ordered]@{
"@type"      = "MessageCard"
"@context"   = "http://schema.org/extensions"
"themeColor" = 'd72f00'
"title"      = "HAL Automation Pipeline Failure: Environment: ${{ parameters.test_environment }} Tests: ${{ parameters.test_category }} "
"text"       = "$(Build.DefinitionName)"
}
$body = ConvertTo-Json $body -Depth 100
$uri = "$(TeamChannelUrl)"
Invoke-RestMethod -uri $uri -Method Post -body $body -ContentType 'application/json'
displayName: Send Teams Notification
condition: failed()
```


Test your pipeline changes by running it. If you’re having trouble generating an automation or pipeline failure you can set the condition value above to _condition: always( )_. Just remember to put it back to _condition: failed( )_!

7) Link to pipeline failures. Now on a pipeline failure a link will be generated in the new Teams Channel entry to the failed pipeline job. Anyone can click it and see the pipeline failure. Now time to start debugging!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i1u4mayfayza6fdubd4v.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/80doft9t6xoaonfwb709.png)