Answer a question

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 õrganization
  • repo: name of the repository
  • workflow_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 there

Using 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.

  1. Do I need to configure the GitHub end of this process differently?
  2. Is my curl statement wrong?
  3. Is there any way I can inspect the "problem[atic] JSON" in order to find out what is going on?

Answers

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

Logo

ModelScope旨在打造下一代开源的模型即服务共享平台,为泛AI开发者提供灵活、易用、低成本的一站式模型服务产品,让模型应用更简单!

更多推荐