codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Follow publication

Using Github Actions with Repository Dispatch Event

--

Photo by James Harrison on Unsplash

If you’ve ever set up a Github Actions workflow on a repository, you will recognize the YAML file that executes steps upon any code you push to the repository.

These steps could entail building and deploying your code for production, for instance.

Above we define the steps for Github Actions to build the code in a node environment and deploy it to Netlify. This workflow is for my Gatsby code for my website. But what happens if I want to trigger the steps above on events other than pushing code?

Invoking a Repository Dispatch Event From a CMS

I used Strapi CMS to manage content for my website. And I realized that I needed to trigger the workflow above every time I updated the content on Strapi. I needed to update my site every time I changed the content.

Into the picture came the amazing manual Repository Dispatch event. I realized I could use the Github API to let my Gatsby repository know it should build and deploy my updated site.

To trigger a repository dispatch one must send a POST request to the following Github API endpoint

https://api.github.com/repos/{owner}/{repo}/dispatches

The owner is your GitHub username, and the repo is the repository name.

I set the accept header parameter to application/vnd.github.v3+json and sent a body with the parameter event_type . This last parameter is required. You could also send other payloads with the body.

I had to create and use a personal Github token with the repo scope to obtain write access. Your repository dispatch will not be authorized otherwise. See here to learn more about creating a personal token on Github.

Once you’ve created the token, copy it and store it somewhere safe. You will not be able to access your Github token value after it has been created.

I added the token to the above POST request as the Authorization header parameter with the value:

`Bearer ${process.env.GITHUB_TOKEN}`

The entire POST request should look something like:

URL: https://api.github/com/repos/{owner}/{repo}/dispatchesHeaders: {
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
Accept: `application/vnd.github.v3+json`,
Content-Type: `application/json`,
}
Body: {
'event_type': 'custom_event_type'
}

Finally, I made sure to add the repository dispatch event type to the main.yml file

Now, every time I update the content on Strapi and send the POST request above to the Github API, the repository_dispatch event is triggered and the workflow file is executed.

Conclusion

In my case, I had to trigger my Gatsby code build upon any changes to my content on Strapi. But the repository dispatch webhook could come in handy in all sorts of ways. I am excited to utilize it more in my future projects. Thanks for reading my article, I hope you find it helpful! Please leave questions or feedback in the comments section.

--

--

Published in codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Written by Sanjar Kairosh

Full Stack Engineer. Enjoys reading. Writes about a mixture of topics to satisfy curiosity.

Responses (2)