'How to trigger a `workflow_dispatch` from Github API?

From the GH Rest API docs, seems we're able to create a repository_dispatch event, but no workflow_dispatch event. In the GH GraphQL API, I couldn't find how to dispatch events.

Is it even possible to trigger a workflow_dispatch event using the API?



Solution 1:[1]

Yes, it's possible, manually or through the Github API.

Manually (through the Actions tab on your repository.)

Here is an official documentation about it

Basically, once you select the workflow on the tab, if the workflow implementation has the workflow_dispatch trigger, the option Run workflow will appear on the right part of the window, like this:

enter image description here

With the Github API

On the official Github Documentation, there is a service to create a workflow dispatch event

Here is a curl example:

curl \
  -X POST \
  -H "Accept: application/vnd.github.v3+json" \
  https://api.github.com/repos/octocat/hello-world/actions/workflows/42/dispatches \
  -d '{"ref":"main"}'

Note that you can also send workflow inputs through this API as well.

You can also find more references about this in this article.

There is also another Github API service to trigger repository_dispatch events.

Bonus

If you're looking for triggering those workflow_dispatch events (and repository_dispatch events) through a terminal command line, you can use this automation in Python. The implementation can be found in this class.

Solution 2:[2]

You can also trigger workflow_dispatch via GH CLI tool https://cli.github.com/.

E.g.:

gh workflow run <WORKFLOW_ID> -f param_1=foo

You can get workflow IDs via gh workflow list.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 rav