Lesson 5 of 5 Monitoring a DevOps Environment

Configuring Alerts for GitHub Actions and Azure Pipelines

This lesson explains how to raise useful alerts from delivery events.

Learning outcomes

After completing this lesson, you will be able to:

  • Send an alert from a workflow.
  • Create notification subscriptions and service hooks.
  • Subscribe Teams channels to pipeline events.
  • Alert on trends, and reduce noise.

Alerts in GitHub Actions

Built-in notifications

GitHub notifies the person who triggered a workflow when it completes, according to their notification settings, and these can be limited to failed runs. This suits personal awareness, but not team-wide operations.

Notification steps

A workflow can send its own alert. A step with if: failure() runs only when an earlier step has failed. Here, a final job posts to a Teams channel through a workflow webhook.

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - run: ./deploy.sh

  notify:
    needs: deploy
    if: failure()
    runs-on: ubuntu-latest
    steps:
      - name: Post to the operations channel
        env:
          WEBHOOK: ${{ secrets.TEAMS_WEBHOOK_URL }}
        run: |
          curl -sf -H "Content-Type: application/json" \
            -d "{\"text\":\"Deployment failed: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID\"}" \
            "$WEBHOOK"

Events and webhooks

The workflow_run event starts a separate workflow after another finishes, which allows one central notification workflow to watch several others. Repository or organization webhooks can send workflow_run and workflow_job events to external systems. The GitHub app for Teams can subscribe a channel to workflow notifications, with @GitHub subscribe OWNER/REPO workflows.

Spot the bug

A failure alert that never fires

Exam skills 5.1.4

Find the lines that stop this workflow from alerting on a failed deployment.

Log in to try this activity Interactive activities are free with an account.

Alerts in Azure Pipelines

Notification subscriptions

Azure DevOps notifications are created in Project settings, or at team or personal level. A subscription selects an event, such as Build completes or Release deployment approval pending, adds filters such as the pipeline name and status, and delivers an email to chosen people. A team subscription suits a pipeline owned by one team.

Service hooks

Service hooks send events to external services, as explained earlier. Events include run state changed, stage state changed and approval pending. These can post to a webhook or to Azure Service Bus.

Azure Pipelines app for Teams

The Azure Pipelines app for Teams subscribes a channel to pipeline events, and an approver can respond from within the channel.

@azure pipelines signin
@azure pipelines subscribe https://dev.azure.com/contoso/payments/_build?definitionId=12
@azure pipelines subscriptions

Failure steps in YAML

A job with condition: failed() can raise an alert, or create a work item. Classic build pipelines also have a Create work items on failure option, and a YAML pipeline can create a work item through a REST call.

The following diagram shows the Azure Pipelines app in Microsoft Teams, its channel subscription and filters.

The simplest route to targeted Teams messages. The Azure Pipelines app in Teams subscribes the channel to the pipeline, and the subscription is edited to keep only approvals waiting and failed stages, so nothing else is posted.
The scenario is illustrative.
Scenario questions

Pipeline alerts in Azure DevOps

Exam skills 5.1.4

Choose the Azure Pipelines alerting mechanism for each need.

Log in to try this activity Interactive activities are free with an account.

Alerting on trends

A single failure often does not matter, but a pattern does. A rising failure rate, or queue times above a limit, may need attention before anybody complains. Exporting pipeline events to Log Analytics allows a log search alert on a trend, for example when more than 20% of runs fail within an hour. Alerts on self-hosted agents should be raised too: Azure Monitor can watch their virtual machines, and an agent that has gone offline leaves jobs waiting, which is easy to overlook.

True or false

Alerting on patterns

Exam skills 5.1.4

Check how to alert on pipeline trends rather than single failures.

Log in to try this activity Interactive activities are free with an account.

Reducing noise

  • Alert on what matters: production and main branch failures, not every pull request.
  • Send to the owner, with a link to the failed run.
  • Use severity, so that a blocked production release differs from a failed nightly test.
  • Remove alerts that nobody acts on.

The following table recaps the options.

NeedGitHub ActionsAzure Pipelines
Notify the person who started a runPersonal notificationsDefault subscriptions
Notify a teamGitHub app for TeamsAzure Pipelines app, or a team subscription
Send to any systemWebhookService hook
Custom logic after failureif: failure()condition: failed()
Alert on a trendExport to Log AnalyticsExport to Log Analytics
Match the pairs

Pipeline alerts on both platforms

Exam skills 5.1.4

Match each alerting need with the Azure Pipelines and GitHub Actions mechanisms.

Log in to try this activity Interactive activities are free with an account.
Worked example

Notifying a team

A team wants a message in its Microsoft Teams channel whenever a production Azure Pipelines deployment waits for approval or fails, and nothing for other events. What is the simplest approach?

Install the Azure Pipelines app in Teams, subscribe the channel to the pipeline, and edit the subscription to keep only approvals and failed stages. A custom script in each stage would need upkeep, and it could not report an approval wait in the same way. An email subscription would not post to the channel.

Check yourself

How can one GitHub workflow react to the failure of others?

Show answer

Use the workflow_run event with a completed type, and test the conclusion of the triggering run.

Exam Tips

Remember for the exam

  • Use if: failure() in GitHub Actions and condition: failed() in Azure Pipelines.
  • workflow_run lets one workflow react to others.
  • Service hooks send pipeline events to external services.
  • Team subscriptions filter by event and pipeline.
  • Export events to Log Analytics to alert on trends.