Home > Net >  How to trigger a GitHub action with curl
How to trigger a GitHub action with curl

Time:10-03

I want to trigger the following GitHub action:

enter image description here

I then run curl:

curl -H "Accept: application/vnd.github json" -H "Authorization: Bearer $GITHUB_TOKEN" --request POST --data '{"event_type": "service-worker-test"}' https://api.github.com/repos/pass-culture/pass-culture-app-native/dispatches

Results:

{
  "message": "Not Found",
  "documentation_url": "https://docs.github.com/rest/reference/repos#create-a-repository-dispatch-event"
}

I have read :

  1. https://docs.github.com/en/actions/using-workflows/triggering-a-workflow
  2. https://goobar.dev/manually-trigger-a-github-actions-workflow/
  3. https://blog.knoldus.com/trigger-a-github-action-with-an-http-request
  4. https://kontent.ai/blog/how-to-trigger-github-action-using-webhook-with-no-code/

Appart that none of those tutorial agrees on the Accept headers, I did exactly what it is requested.

Also, there is a non understanding point, I will have to add payload to the curl request, and:

  • workflow_dispatch use event.inputs.versionFrom
  • repository_dispatch use events.client_payload.versionFrom

This will complexify the GitHub action, isn't there a way to simply use the same for both trigger method?

Does anybody what this errors means and how to achieve a trigger of the workflow using curl?

CodePudding user response:

The Not Found was due because I needed to have the scope repos write access, and not just read.

In order to use both workflow_dispatch and repository_dispatch with inputs and client_payload (that way, I can run the pipeline from cURL and GitHub UI), I needed to concat both placeholders like so:

name: example-client-payload-action
on:
  repository_dispatch:
    types: webook

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - run: 'echo "field: ${{ github.event.client_payload.foo }}${{ github.event.inputs.foo }}"'
      - run: 'echo "payload: ${{ toJson(github.event.client_payload) }}${{ toJson(github.event.inputs) }}"'

This way, one is interpolated as an empty string, and the second as the replaced value.

It's not very nice to read, but I am sure you can add it into another env for simplification sakes.

  • Related