代码之家  ›  专栏  ›  技术社区  ›  negabaro

如何在github操作中将参数批量传递给另一个步骤

  •  0
  • negabaro  · 技术社区  · 4 年前

    我想将设置的值传递给 core.setOutput 同时执行不同的步骤。因此,我传递了该步骤的输出 hello-action 进入下一步骤:

    //index.js
    const core = require("@actions/core");
    core.setOutput("res1","res1");
    core.setOutput("res2","res2");
    core.setOutput("res3","res3");
    
    jobs:
      build:
        runs-on: ubuntu-latest
        name: Hello
        steps:
          - name: Checkout
            uses: actions/checkout@master
          - name: run hello action
            id: hello-action
            run: node index.js
          - name: run hello2 action
            id: hello2-action
            run: node index2.js
            with:
              outputs: ${{ steps.hello-action.outputs }}
    

    重点是。

    outputs: ${{ steps.hello-action.outputs }}
    

    上述值的类型是一个对象,但我无法访问对象内的键。

    //index2.js
    const core = require("@actions/core");
    const outputs = core.getInput("outputs");
    console.log("hello2 outputs:", outputs); //result is [object Object]
    console.log("hello2 outputs:", outputs.res1); //result is undefined
    

    有没有一种方法可以批量传递论点?

    0 回复  |  直到 3 年前
        1
  •  3
  •   xom9ikk    4 年前

    对象不能转移到 output 从行动来看,只有字符串。 因此,当从自定义操作传递对象时 toString() 方法将被调用。因此,您将获得 [object Object] 在执行此操作时:

    - name: run hello action
      id: hello-action
      run: node index.js
    

    要从自定义操作传输数据结构,请使用以下命令将对象转换为JSON字符串 JSON.stringify() 方法。

    第一次行动

    //index1.js
    const obj = {
        field1: "value1",
        field2: "value2",
    }
    core.setOutput("outputs", JSON.stringify(obj));
    

    在第二步中,解析 JSON 一串

    第二次行动

    //index2.js
    const core = require("@actions/core");
    const outputs = JSON.parse(core.getInput("outputs"));
    

    这应该对你有帮助。