如何在 GitHub 操作中将对象传递给矩阵?

时间:2021-05-28 15:28:03

标签: javascript object github-actions

我有一个 JavaScript GitHub 操作,它输出一个简单的对象数组,如下所示:

[
  {
    prop1: val1a,
    prop2: val2a,
    prop3: val3a
  },
  {
    prop1: val1b,
    prop2: val2b,
    prop3: val3b
  }
]

在一个新作业中,我想读入这个数组,循环它,并在循环的每次迭代中打印属性值。这是怎么做的,尤其是最后一部分?

使用下面的代码,我设法创建了一个新作业,并让它遍历数组中的每个元素。但是在每次迭代中,如何访问每个元素的属性值?

提前致谢。


我的代码

myAction.yml

name: GitHub Actions Demo
on: [push]
jobs:
  job1:
    outputs:
      matrix: ${{ steps.hello.outputs.matrix }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2.3.4
        with:
          fetch-depth: 0
      - name: Test JS action # Run the JavaScript action to output an array of objects.
        id: hello
        uses: ghusername/myreponame@master

  # Run the second job here, to read in the output of the previous job, set its value to a matrix, then iterate over that matrix.
  job2:
    needs: [job1]
    runs-on: ubuntu-latest
    strategy:
      matrix: ${{fromJSON(needs.job1.outputs.matrix)}}
    steps:
      - run: echo "I want to outputted objects from job1 to print here"

myJavaScriptAction.js(位于 GitHub 上的 ghusername/myreponame@master

const core = require("@actions/core");
 
const myArr = [
  {
    prop1: 111,
    prop2: 222,
    prop3: 333
  },
  {
    prop1: 444,
    prop2: 555,
    prop3: 666
  }
]

/* Convert output array to valid JSON, then stringify it. This is required in order to pass it to the next GitHub action.*/
outputArr = {
  include: myArr,
};
core.setOutput(
  "matrix",
  JSON.stringify(outputArr)
);

0 个答案:

没有答案