从JSON文件迭代JSON对象

nuypyhwy  于 2023-01-18  发布在  其他
关注(0)|答案(1)|浏览(167)

我试图迭代JSON文件中的对象数组,但无法执行。我当前收到以下错误:
评估作业“创建”的“策略”时出错。.github/workflowers/rolling-deployment.yml(行:66,第15栏):不需要序列
以下是当前工作流文件:

name: Rolling Deploy

on:
  push:
    branches: [ "main" ]

jobs:
  servers:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.read-json.outputs.matrix }}
    environment: Staging
    steps:
      ... a few other steps in here to create the json file
      - name: Read JSON
        id: read-json
        run: |
          JSON=$(cat ./neededServers.json)
          echo "matrix=${JSON//'%'/'%25'}" >> $GITHUB_OUTPUT
    
  create:
    needs: servers
    runs-on: ubuntu-latest
    strategy:
      matrix: ${{ fromJson(needs.servers.outputs.matrix) }}
    steps:
      - name: Test
        run: echo "${{matrix.reference}}"

我在create中运行了echo ${{needs.servers.outputs.matrix}},但没有策略,它打印了JSON文件,下面也是一个JSON文件示例:

[
    {
        "config":"small",
        "ID":"1234",
        "memory":"4096",
        "vcpus":"2"
    },{
        "config":"small",
        "ID":"2345",
        "memory":"4096",
        "vcpus":"2"
    }
]

我不知道我是做错了什么还是不可能。

8yoxcaq7

8yoxcaq71#

JSON应该包含数组的"include"键:

{
    "include": [
        {
            "config":"small",
            "ID":"1234",
            "memory":"4096",
            "vcpus":"2"
        },
        {
            "config":"small",
            "ID":"1234",
            "memory":"4096",
            "vcpus":"2"
        }
    ]
}

相关问题