【问题标题】:How to send data payload using http request to Github-Actions Workflow如何使用 http 请求将数据有效负载发送到 Github-Actions Workflow
【发布时间】:2021-12-29 06:54:19
【问题描述】:

我想使用带有数据负载的http request 触发 Github-Actions 工作流,并在工作流脚本中使用此数据。但我找不到任何关于如何发送和使用该有效负载数据的文档。 我正在使用下面的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.

【问题讨论】:

    标签: python rest http github github-actions


    【解决方案1】:

    您可能会混淆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

    【讨论】:

    • 嗨@rethab,你能告诉我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" }的价值是什么
    • 能否请教一下您正在使用的整个命令?
    • @AashutoshKumar 我怀疑您将repository_dispatchworkflow_dispatch 混合在一起。我已经用一个部分扩展了我的答案以区分两者。
    • 正如 rethab 解释的那样,event_type 仅支持 repository_dispatch 事件,因此如果 URL 返回此错误,可能是因为您没有使用正确的API。如果您想看一下,我最近在这里回答了类似的问题:stackoverflow.com/questions/69925264/…
    • 感谢 @rethab 和 @GuiFalourd 让我了解 diff b/w workflow_dispatch & repository_dispatch endpoints 现在已解决,我现在可以发送和访问有效负载。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 2019-11-14
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多