如何使用json作为workflow_dispatch的参数

问题描述 投票:0回答:1

我正在尝试自动化一些测试,但我需要将一些特定的参数传递给最终的测试脚本,这些参数非常适合作为 json 文件。现在的问题是让 github action 能够处理 json 数据作为参数。

限制是 json 文件必须是本地的,因为工作流程必须从命令触发

gh workflow run ...

到目前为止,我尝试创建我的第一个

yml
文件:

name: setup

on:
  workflow_dispatch :
    inputs:
      config_file:
        description: 'json file containing the configuration for the runners'
        required: true
        type: string
...
env:
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
  setup-auth:
    name: setup-authentication
    uses: ./.github/workflows/single-device-authentication.yml
    with:
      devices: mlops
      config_file: ${{  inputs.config_file }}
    secrets: inherit

single-device-authentication.yml 看起来像这样,我评论了它失败的地方:

name: single-device-authentication

on: 
  workflow_call:
    inputs:
      devices:
        required: true
        type: string
      config_file:
        description: 'json file containing the configuration for jetson runners'
        required: true
        type: string
      

jobs:
  device-authentication:
    name: device-authentication
    runs-on: ${{  inputs.devices }}

    steps:
     - uses: PATH/TO/gh_auth@main
       with:
         app_id: 7
         private_key: ${{ secrets.MLOPS_BOT_PRIVATE_KEY }}

  json-parser:
    name: parser
    runs-on: ${{inputs.devices}}
    needs: device-authentication
    steps:
      - name: parser script
        run: | 
          echo ${{ inputs.config_file }}" # This fails

另外,为了触发工作流程,我尝试了这种方式:

gh workflow run setup.yml -f config_file="$(cat ${PATH_TO_CONFIG_FILE})"

github-actions
1个回答
0
投票

我认为错误源在

${{ inputs.config_file }}
之前缺少引号,您声称它失败了,所以应该是

json-parser:
  name: parser
  runs-on: ${{inputs.devices}}
  needs: device-authentication
  steps:
    - name: parser script
      run: | 
        echo "${{ inputs.config_file }}"

除了将 JSON 作为输入传递给workflow_dispatch/workflow_call。我碰巧发送包含空格和换行符的 JSON 时格式错误。例如 JSON 输入

{
  "first": "value",
  "second": "anotherValue"
}

在称为工作流程中的格式为

{first: value, second: anotherValue}

由于缺少引号,它不再是 JSON。我通过在将其作为输入参数传递给工作流之前删除所有空格和换行符来解决这个问题。例如

jq -c . config_file.json
创建一个紧凑的 JSON 作为不带空格的单行字符串。之后,被调用的工作流会收到有效的 JSON。

© www.soinside.com 2019 - 2024. All rights reserved.