krocc97
krocc97

Reputation: 145

Triggering Github Action using a POST request (Github REST API)

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:

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?

Upvotes: 11

Views: 21879

Answers (2)

GuiFalourd
GuiFalourd

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

References

Upvotes: 1

scriptDaddy
scriptDaddy

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

Related Questions