github-actionsgithub-apiworkflow-dispatch

Github dispatches workflow Invalid request


I'm trying to trigger a workflow event in Github.

for some reason, I'm able to GET information about my organization repository workflow but can not use '/dispatches'

Work is based on: https://docs.github.com/en/rest/actions/workflows#create-a-workflow-dispatch-event

Here is the curl code:

curl -X POST \
         -H "Accept:application/vnd.github.v3+json" \
         -H 'Authorization:token ${{ github.token }}' \
         'https://api.github.com/repos/[owner/org]/[repo]/actions/workflows/9999999/dispatches' \
         -d '{"event_type":"semantic-release"}'

Getting error:

422 Unprocessable Entity
"message": "Invalid request.\n\nFor 'links/0/schema', nil is not an object.",
"documentation_url": "https://docs.github.com/rest/reference/repos#create-a-repository-dispatch-event"

Am I missing some basic information for this to work and trigger an event?


Solution

  • Instead of trying to call the GitHub API directly, try and use the GitHub CLI gh (that you can install first to test locally).

    You can also use GitHub CLI in workflows.

    GitHub CLI is preinstalled on all GitHub-hosted runners.
    For each step that uses GitHub CLI, you must set an environment variable called GITHUB_TOKEN to a token with the required scopes

    It has a gh workflow run, which does create a workflow_dispatch event for a given workflow.

    Authenticates first (gh auth login, if you are doing a local test):

    # authenticate against github.com by reading the token from a file
    $ gh auth login --with-token < mytoken.txt
    

    Examples:

    # Run the workflow file 'triage.yml' at the remote's default branch
    $ gh workflow run triage.yml
    
    # Run the workflow file 'triage.yml' at a specified ref
    $ gh workflow run triage.yml --ref my-branch
    
    # Run the workflow file 'triage.yml' with command line inputs
    $ gh workflow run triage.yml -f name=scully -f greeting=hello
    
    # Run the workflow file 'triage.yml' with JSON via standard input
    $ echo '{"name":"scully", "greeting":"hello"}' | gh workflow run triage.yml --json
    

    In your case (GitHub Action):

    jobs:
      push:
        runs-on: ubuntu-latest
        steps:
          - run: gh workflow run triage.yml
            env:
              GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    

    As explained by hanayama in the comments:

    Found out the secrets. GITHUB_TOKEN doesn't work, even with permissions edited for the entire workflow.
    Using a personal access token worked.