14

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?

4 Answers 4

13

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

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.

GH CLI

You can also trigger a workflow_dispatch event using the GH CLI tool through the command:

gh workflow run [<workflow-id> | <workflow-name>] [flags]

Create a workflow_dispatch event for a given workflow.

This command will trigger GitHub Actions to run a given workflow file. The given workflow file must support a workflow_dispatch 'on' trigger in order to be run in this way.

Reference.

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.

4
  • calling https://api.github.com/repos/octocat/hello-world/actions/workflows/42/dispatches with workflow_id is not possible to get from a feature branch if the workflow didn't run. If there is anyway to find the workflow_id from a fraure branch let me know.
    – change198
    Jun 18 at 21:53
  • If there is anyway to find the workflow_id from a feature branch let me know. The workflow id is generated automatically by Github, it's not related to the repository or its branches :/
    – GuiFalourd
    Jun 19 at 11:29
  • The github API POST shown above does not work on feature branches. Jul 17 at 16:21
  • 1
    @DeanSchulze yes it will not work on feature branch with workflow id but there is a workaround docs.github.com/en/rest/actions/… replace workflow_id with the workflow file name. How ever on feature branch make sure your workflow_dispatch and push are same indenatation.
    – change198
    Sep 12 at 14:17
8

You can also trigger workflow_dispatch via the GH CLI tool: gh workflow run docs.

E.g.: gh workflow run <WORKFLOW_ID> -f param_1=foo

  • You can get workflow IDs via gh workflow list.

Or you can also use the workflow file name instead of the workflow ID.

E.g.: gh workflow run staging.yml -f param_1=foo

4

Workflow ID can also by the workflow file name

  curl \
    -X POST \
  -H "Accept: application/vnd.github.v3+json" \
    https://api.github.com/repos/<USER>/<REPO>/actions/workflows/build.yaml/dispatches \
    -d '{"ref":"main"}'

https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#create-a-workflow-dispatch-event

1

Here's an example of python code to trigger GitHub workflow.

You can get the workflow ID via gh workflow list, see: https://stackoverflow.com/a/71972073/1661491

github_token is the classic personal token, see: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token

workflow_ref is the branch or tag.

import requests
import json

parameter_dict = ...
github_token = ...
workflow_ref = ...
workflow_id = ...
org = ...
repo_name = ..

inputs_str = json.dumps(parameter_dict)

response = requests.post( 
 f"https://api.github.com/repos/{org}/{repo_name}/actions/workflows/{workflow_id}/dispatches",
  headers={"Authorization": f"Bearer {github_token}"},
  data=f'{{"ref": "{workflow_ref}", "inputs": {inputs_str}}}'
)

if response.status_code >= 400:
  raise ValueError(f"Failed to trigger workflow: {response.status_code} {response.text}")
1
  • This is great! Github featured ghapi (github.blog/…), a Python library which allows you to also do this. If you're just wanting to create workflows it's probably not worth pulling in ghiapi, but if you need other parts it could be handy. The call is actions.create_workflow_dispatch(owner, repo, workflow_id, ref, inputs), which does what the above answer does.
    – Josh
    Dec 16, 2022 at 0:55

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.