Triggering Github Actions manually with the API

You can now trigger Github Workflows manually, hooray! Rather bafflingly the announcement doesn't mention the API, but there is one! Hunting through the also recently announced OpenAPI Github.com API Specifications yields the POST - /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches endpoint.

Before you can use the endpoint you'll need a couple of things:

  • A Github API Token with the following permissions:
  • The workflow_id - this is the numeric id, you can get it from the GET  - /repos/{owner}/{repo}/actions/workflows endpoint

Enable manual workflow trigger

First we need to tell our workflow to trigger on the manual event:

on:
  workflow_dispatch:

This will enable the button in the UI as well as enabling the API to trigger as well.

Call the workflow dispatch endpoint

curl --location --request POST 'https://api.github.com/repos/tegud/example/actions/workflows/123456/dispatches' \
--header 'Authorization: Bearer <MY TOKEN>' \
--header 'Content-Type: application/json' \
--data-raw '{ "ref": "master" }'

That's pretty much it.  Nice and easy.

You can provide optional input's as well, which introduces the possibility of workflows calling workflows - in fact someone's already created a Github Action for that: https://github.com/marketplace/actions/workflow-dispatch