Trigger GitHub actions with Azure DevOps Pipelines
DDDPerth’s website team are in the midst of a site refresh. There are many goals guiding the refresh but a significant one was gaining insight, and improving, our performance and accessibility.
A convenient method to gather these metrics is an automated step within the CD process. There’s a few services to choose from but we opted for Lighthouse-CI. It has a GitHub Action, making setup quick and easy, and Lighthouse scores provided the desired metrics. With that settled, how would this work in our pipeline?
From GitHub’s documentation:
You can use this endpoint to trigger a webhook event called repository_dispatch when you want activity that happens outside of GitHub to trigger a GitHub Actions workflow or GitHub App webhook.
Perfect. The POST endpoint is /repos/:owner/:repo/dispatches
. Replacing :owner
and :repo
for your GitHub username and repository name. The endpoint requires an event_type string parameter, this is the name of the event. There’s also an optional client_payload JSON object parameter if you want to send extra information. Say for configurable Actions.
Configure the action
The repo’s actions will live in the workflows directory and have a YAML configuration file. Setting the on
attribute to repository_dispatch
enables the action to be triggered when the repository dispatch event endpoint is called.
on: [repository_dispatch]
With the on event defined it’s possible to test at this point by hitting that endpoint in an application like Postman. If successful you should see the Action in the repo’s workflow run page.
Azure DevOps
DDDPerth’s build pipelines are in Azure DevOps but if you’re able to call a REST endpoint then you’ll be able to achieve the same in other systems.
Calling a rest endpoint in Azure
Navigate to your build tasks and, if you don’t yet have an agentless job, you’ll need to add one. Select the three dots and choose “Add an agentless job”.
Add a task to your agentless job, choosing “Invoke REST API”.
Configure REST API task
We need to authenticate to the GitHub API. A good way to do this is creating a generic service connection for the GitHub API (https://api.github.com/).
The account that has at least write access to the repository is the username. The password will be a personal access token you create. Read through GitHub’s documentation for personal access tokens. The scope, or permission, of the token needs to be public_repo
.
The remaining settings set the method to POST, the content-type header as application/json and the body to have the required event_type parameter.
With this task in your pipeline it will now trigger the GitHub action each time the task runs. In the case of DDDPerth that means Lighthouse-ci tests are run during each deployment.
TL;DR
- Add GitHub action
- Action’s on event:
on: [repository_dispatch]
- Agentless Job in Azure DevOps build task
- Invoke REST API task
- GitHub Access Token with scope
public_repo
- POST https://api.github.com/repos/:owner/:repo/dispatches
- Content-Type: “application/json”
- Body: { “event_type”: “EVENT_NAME” }
Thanks
I did not go into this with experience of Azure DevOps. So thanks to my DDDPerth teammates, Rob Moore and Mo Zaatar for pairing with me on the Azure side. And to those that gave valuable input when I asked on Twitter, Amy Kapers and Edward Thomson.