Monitoring GitHub Actions with Azure Logic Apps
RSS feed
Date: Jun 01, 2023
Tags: azure github
Share: Share on Twitter
NOTE: Technology changes fast, so some of the images and/or code in this article maybe be out of date. Feel free to leave a comment if you notice something that needs updating.
If you’re using GitHub, then you most certainly have heard about GitHub Actions. From simple “build and deploy” workflows to elaborate multi-project collaborations, Actions are the key to getting your awesome code to where you need it go. While the API-backed capabilities are impressive, the built-in monitoring features may not meet every company’s needs. In this article, I’m going to show how to leverage Azure Logic Apps to track your GitHub Actions and send out customized alerts when someone divides by zero.

I love blending Azure services together to make cool things happen. Whether it’s custom code in a Function or a massively scalable App Service, there’s not much the platform can’t do. When you combine GitHub and Azure together you start unlocking some amazing capabilities. For GitHub Actions, you can use Azure Logic Apps to track your workflow progress, evaluate results, and notify your team if there’s a problem.

The Basics

Here is a basic outline of what I’d like to implement. I want to run a GitHub Action and track the progress with Azure Logic Apps. If the Action fails, I want to push a notification to Microsoft Teams.

Process



This process contains the following components:

GitHub Action

This is my GitHub workflow to do something with my code. 


GitHub Webhook

This is my trigger for the Azure Logic App when the GitHub Action is run.


Azure Logic App

This is my processor that will monitor the GitHub Action and perform steps based on the results.


Microsoft Teams

This is alerting component. In my demo, I’m going to post an alert in a Teams channel if the GitHub Action fails.

Create a GitHub Action

The first step of the process is to create my GitHub Action. In my repository, I create a new Action.



For my demo, I am calling a custom step to print out some messages. Exciting, I know!


Note

If you’re new to GitHub Actions, check out the MANY templates provided. GitHub will evaluate your project and recommend specific templates based on your language/project type.

Create a GitHub Webhook

With the Action created, I am ready to create my webhook. GitHub Webhooks can be created for all sorts of events, so be sure to review the options and select the events for your needs. In my case, I only want to track when a workflow is run/completed.



For the Payload URL, I will fill any value. When the Azure Logic App is created, I will update the Payload URL to the Azure Logic App URL

For the Which events would you like to trigger this webhook I select the Let me select individual events option.



I select the Workflow runs event. This event will occur anytime my Action is run and when it completes.



Note

Several of the Webhook events may result in multiple posts with different data. Be sure you review the webhook data that is generated to find the events you need to track.

Create an Azure Logic App

The next component is the Azure Logic App. Azure Logic Apps are workflows that can be triggered from a variety of sources, including HTTP requests. I will configure my webhook to respond when the GitHub Webhook data is posted.



The GitHub Webhook post will contain an array of data, so I need to loop through the items to find the payload value. This value will contain the GitHub Action run details.



The payload value will be a string. With Logic Apps, it’s much easier to work with JSON, so I use the built-in Parse JSON step to format the data.


NOTE

When using the Parse JSON step, use the Use sample payload to generate schema option to simplify the schema definition.

Modify the schema

In my demo, I’m going to look at the conclusion value for the Action run details. This value may be null, depending on the current status of the workflow. For this reason, I need to update my schema to allow nulls for this value.

{
    "properties": {
	...
        "workflow_run": {
            "properties": {
                "conclusion": {
                    "type": [
                        "string",
                        "null"
                    ]
                },
	...
}



Because the GitHub Action will send a Webhook payload for multiple events (run started, in progress, completed, etc.), I need to check if the run has been completed. I also only want to take action if the run failed. I add the following checks for action = completed and conclusion = failure.



With the above in place, my Logic App is ready to handle a failure. For my demo, I want to post a message to a Teams channel, alerting the members that a workflow has failed.

I add the Teams connector and select my team/channel and define my message.



Here is the adaptive card JSON:

{
    "type": "AdaptiveCard",
    "version": "1.0",
    "body": [
        {
            "type": "Image",
            "url": "[URL TO IMAGE]"
        },
        {
            "type": "TextBlock",
            "text": "GitHub Action failed!"
        },
        {
            "type": "TextBlock",
            "text": "Run date/time: @{body('Parse_payload_JSON_')?['workflow_run']?['run_started_at']}"
        },
        {
            "type": "TextBlock",
            "text": "Repo:  @{body('Parse_payload_JSON_')?['workflow_run']?['repository']?['name']}"
        },
        {
            "type": "TextBlock",
            "text": "Action:  @{body('Parse_payload_JSON_')?['workflow']?['name']}"
        }
    ]
}


When posting to Teams, there are a lot of options for formatting the data. You can post a simple message or leverage cards to format the data and even allow responses. For demo, I’m using an adaptive card with an image and some text. I use the dynamic values from the Parse JSON step to add contextual details.

With the Azure Logic App created, I need to update my GitHub Payload URL. In my Logic App, I get the URL.



In my GitHub Webhook, I updated the Payload URL with the value.



Note

You can download a template to deploy the Logic App from my GitHub repository. This template does not contain the Teams integration, as that requires authentication configuration.

Testing

With all the pieces in place, I’m ready to test. In my GitHub Action, I edit the workflow file to ensure a failure. In my demo, I’m changing the Hello world action step to a version that doesn’t exist.



When the Action completes, I confirmed it failed within the GitHub UI.



In the Logic App, I see the executions. GitHub will post multiple webhook messages for a workflow run, so it’s normal to see multiple Logic App runs.



NOTE

Because my Logic App is configured to post to Teams when the Action failed, the execution time will be longer. 



In the Logic App run details, I can see where the payload was evaluated, and the conclusion is failure.



Finally, in my Teams Channel, I can see my formatted message.


Moving Forward

When working with GitHub, nearly every aspect of the platform is extendable. By levering Webhooks and Azure Logic Apps, you can build robust monitoring and alerting into your CI/CD process. Be sure to check out all the Webhook events to tailor the experience to your needs. Good luck!

Helpful Links