遍历 JSON 文件并将每个值设置为 Github 操作工作流程中的变量

Loop through JSON file and set each value as a variable within Github Actions workflow

目标:在 GHA 作业期间将存储在 JSON 文件中的 ~20 个变量设置到 $GITHUBENV 中。

目前,我正在通过使用 jq:

解析 JSON 文件对其进行硬编码
- name: Set env variables from variables json
      run: |
          echo "NAME=$(jq -r '.name' variables.json)" >> $GITHUB_ENV
          echo "AGE=$(jq -r '.age' variables.json)" >> $GITHUB_ENV
          echo "WEIGHT=$(jq -r '.weight' variables.json)" >> $GITHUB_ENV
          ...etc

如何“for循环”完成这个过程?

之后,我想检查通过作业的 workflow_dispatch 运行 手动输入的变量是否与 variables.json 文件中的变量匹配。如果它们不匹配,我想用新的手动输入值更新 json 文件:

- name: Set age if dispatching
      shell: bash -l {0}
      if: github.event.inputs.age != env.AGE
      run: echo "$( jq '.name = ${{github.event.inputs.age}}' variables.json )" > variables.json

同样,如何循环执行此过程?

我建议您使用 JSON to variables GitHub 操作。来自文档:

test.json

{
    "value": "value",
    "array": [
        {
            "value": "value 0"
        },
        "value 1"
    ],
    "obj": {
        "value1": "value1",
        "value2": "value2"
    }
}

workflow.main

- name: JSON to variables
  uses: antifree/json-to-variables@v1.0.1
  with:
    filename: 'test.json'
    prefix: test
- name: Show output
  run: echo "The time was ${{ env.test_value }}, ${{ env.test_array_0_value }}, ${{ env.test_obj_value1 }}"