如何使用 http 请求将数据负载发送到 Github-Actions Workflow

How to send data payload using http request to Github-Actions Workflow

我想使用 http request 和数据负载触发 Github-Actions Workflow,并在工作流脚本中使用此数据。但是我找不到任何关于如何发送和使用有效负载数据的文档。 我正在使用下面的 curl 命令来触发工作流程,但还需要发送和使用该数据负载。

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"}'

演示工作流 Yaml 文件。

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.

您可能会混淆 repository_dispatchworkflow_dispatch。存储库分派是您发送到存储库的事件。可以接收此事件,例如通过 GitHub 应用程序,或者它可以用于通过使用 on: repository_dispatch 和工作流程的顶部来触发工作流程。

文档:

另一方面,工作流调度是关于直接触发工作流到 运行。这就像在 UI 上单击“运行 此工作流程”。这需要将您的工作流程设置为使用工作流程顶部的 on: workflow_dispatch 手动触发。

文档:


创建 repository_dispatch 活动的方法如下:

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"}}'

(来自文档:https://docs.github.com/en/rest/reference/repos#create-a-repository-dispatch-event

在您的操作中,您可以像这样访问负载:

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

注意:您在问题中分享的工作流是在多个事件上触发的——而不仅仅是 repository_dispatch。在其他情况下可能不会设置字段 github.event.client_payload