DEV Community

Cover image for Write Pipeline Failures to a Teams Channel

Posted on

Write Pipeline Failures to a Teams Channel

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


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

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

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

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: '' 
Enter fullscreen mode Exit fullscreen mode

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"   = ""
"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()
Enter fullscreen mode Exit fullscreen mode

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

Image description

Top comments (0)