Reputation: 831
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.
Upvotes: 2
Views: 5153
Reputation: 8423
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:
repository_dispatch
event via API: https://docs.github.com/en/rest/reference/repos#create-a-repository-dispatch-eventrespository_dispatch
events: https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows#repository_dispatchThe 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:
workflow_dispatch
event via API: : https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-eventHere'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.
Upvotes: 3