Search code examples
githubgithub-apigithub-actions

How to trigger github actions with dispatch using gh cli


I have a action which has the following yaml in it:

on:
  workflow_dispatch:
    inputs:
      BuildTarget:
        description: "Targets to rebuild. Set to all to rebuild everything."
        required: false
        default: ""

Which I can trigger with:

gh api /repos/:owner/:repo/actions/workflows/build_dev.yml/dispatches -F ref=":branch"

But I can't seem to figure out how to pass inputs into the action from the cli.

I have tried:

gh api /repos/:owner/:repo/actions/workflows/build_dev.yml/dispatches -F ref=":branch" -F BuildTarget=all

Which tells "BuildTarget" is not a permitted key. (HTTP 422)

and trying this:

gh api /repos/:owner/:repo/actions/workflows/build_dev.yml/dispatches -F ref=":branch" -F inputs='{ "BuildTarget": "all" }'

Gives me For 'properties/inputs', "{ \"BuildTarget\": \"all\" }" is not an object. (HTTP 422)

Any idea on how to call this api from the cli and pass in input properties to a workflow?


Solution

  • You can send the raw body directly using --input - to read from standard input :

    gh api /repos/:owner/:repo/actions/workflows/build_dev.yml/dispatches \
       --input - <<< '{"ref":"master","inputs":{"BuildTarget":"all"}}'
    

    Checkout this documentation