2

I want to trigger Github-Actions Workflow using http request with data payload and use this data in the workflow script. But I cannot find any documentation on how to send and use that payload data. I am using below curl command to trigger the workflow, but also need to send and use that data payload.

curl \
  -X POST \
  -H "Accept: application/vnd.github.v3+json" \
  --header 'Authorization: token ******' \
  https://api.github.com/repos/aashutosh0012/actions-test/actions/workflows/learn-github-actions.yml/dispatches \
  -d '{"ref":"main"}'

Demo Workflow Yaml file.

name: GitHub Actions Demo
on: [push, workflow_dispatch,repository_dispatch]
jobs:
  Explore-GitHub-Actions:
    runs-on: ubuntu-latest
    steps:
      - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
      - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
      - run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
      - name: Check out repository code
        uses: actions/checkout@v2
      - run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
      - run: echo "🖥️ The workflow is now ready to test your code on the runner."
      - name: List files in the repository
        run: |
          ls ${{ github.workspace }}
          echo ${{ github}}
      - run: echo "🍏 This job's status is ${{ job.status }}."
      - run: echo "Aashutosh"
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
        with:
          python-version: '3.x' # Version range or exact version of a Python version to use, using SemVer's version range syntax
          architecture: 'x64' # optional x64 or x86. Defaults to x64 if not specified
      
      - name: Run Python Script
        run: python test.py
      
      - name: Install python packages
        run: |
          python -m pip install --upgrade pip
          pip install requests Flask 
          pip list
          
      - name: Run Python Commands Single line
        run: python -c "import requests; print(dir(requests))"
      
      - name: Run Python Commands multi line
        uses: jannekem/run-python-script-action@v1
        id: script
        with:
          fail-on-error: false
          script: |
            from flask import Flask, request
            app = Flask(__name__)
            @app.route('/', methods=['POST'])
            def result():
                print(request.form['ref']) # should display 'bar'
                return 'Received !' # response to your request.

1 Answer 1

3

You might be confusing repository_dispatch and workflow_dispatch. A repository dispatch is an event you're sending to a repository. This event can be received e.g. by a GitHub app or it can be used to trigger a workflow by using on: repository_dispatch and the top of your workflow.

Docs:

The workflow dispatch on the other hand is about directly triggering a workflow to run. It's like clicking "run this workflow" on the UI. This requires your workflow to be set up to be manually triggered by using on: workflow_dispatch at the top of the workflow.

Docs:


Here's how you create a repository_dispatch event:

curl \
  -X POST \
  -H "Accept: application/vnd.github.v3+json" \
  https://api.github.com/repos/octocat/hello-world/dispatches \
  -d '{"event_type":"event_type", "client_payload": {"foo": "bar"}}'

(from the docs: https://docs.github.com/en/rest/reference/repos#create-a-repository-dispatch-event)

In your action, you can then access the payload like so:

  - run: 'echo "Foo: ${{ github.event.client_payload.foo }}"'

Note: The workflow you shared in your question is triggered on multiple events -- not just repository_dispatch. The field github.event.client_payload might not be set in those other cases.

Sign up to request clarification or add additional context in comments.

8 Comments

Hi @rethab, can you plz tell me what should be value for event_type { "message": "Invalid request.\n\n\"client_payload\", \"event_type\" are not permitted keys.\n\"ref\" wasn't supplied.", "documentation_url": "https://docs.github.com/rest/reference/actions#create-a-workflow-dispatch-event" }
Can you please shall the whole command you're using?
@AashutoshKumar I suspect you're mixing repository_dispatch with workflow_dispatch. I have extended my answer with a section to distinguish the two.
As rethab explained, event_type is only supported for repository_dispatch events, so if the URL is returning this error, it's probably because you're not using the correct API. I recently answered something similar here if you want to take a look: stackoverflow.com/questions/69925264/…
Thank you @rethab & @GuiFalourd for making me understand diff b/w workflow_dispatch & repository_dispatch endpoints It is resolved now, I am able to send and access payload now. Thanks again.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.