Reputation: 145
I have a private GitHub repository hosted in a GitHub organization.
The repo contains a GitHub Action with the workflow_dispatch
option
(cf. GitHub Documentation).
Excerpt from the Action YAML file:
on:
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
I can successfully trigger this Action from the GitHub Actions tab.
However, I would like to be able to trigger this Action through a POST
request to the GitHub API.
This should be possible using the GitHub API.
The relevant API endpoint seems to be /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches
as described in the
Documentation.
The Documentation further states:
You must authenticate using an access token with the repo scope to use this endpoint.
Thus, in my personal account Settings under "Developer settings" → "Personal access tokens", I have created a token and granted access to all "repo" items as well as the "workflow" item.
I have tested triggering the GitHub Action by making a POST
request using curl
as well as Postman.
To that end I use the following parameters as per the
GitHub Documentation:
accept
: application/vnd.github.v3+json
(per GitHub recommendation)owner
: name of the GitHub õrganizationrepo
: name of the repositoryworkflow_id
: using the full file name of the GitHub Action YAML file (including the ending, .yml
), as I'm not sure where to find the ID.ref
: main
, as I think this refers to the relevant repository branch (?) and the repo has only a main
branch and the Action resides thereUsing the curl
example from the Documentation (but adding authentication to it):
curl -X POST -H "Accept: application/vnd.github.v3+json" \
-u GITHUBUSERNAME:GITHUBPERSONALACCESSTOKEN \
https://api.github.com/repos/ORGNAME/REPONAME/actions/workflows/YMLFILE/dispatches \
-d '{"ref":"main"}'
I get the following result:
{
"message": "Problems parsing JSON",
"documentation_url": "https://docs.github.com/rest/reference/actions#create-a-workflow-dispatch-event"
}
I also tested issuing the POST
request from Postman (by importing the above curl
statement).
This yields exactly the same result.
curl
statement wrong?Upvotes: 11
Views: 21879
Reputation: 23040
There is also the Repository Dispatch action that can perform this operation for you (dispatch an event using this Github API service), no matter the OS.
Usage
- name: Repository Dispatch
uses: peter-evans/repository-dispatch@v1
with:
token: ${{ secrets.REPO_ACCESS_TOKEN }}
event-type: my-event
Upvotes: 1
Reputation: 182
following this instruction https://goobar.dev/manually-trigger-a-github-actions-workflow/ you do probably most of it correct
try to run it on LINUX with
curl -H "Accept: application/vnd.github+json" -H "Authorization: token your-token" --request POST --data '{"event_type": "do-something"}' https://api.github.com/repos/yourname/yourrepo/dispatches
On Windows: cURL POST command does not work on Windows Command Prompt because single quotes are used see https://github.com/spring-guides/gs-accessing-data-rest/issues/11
Upvotes: 11