'How to pass arguments in bulk to another step in a Github Action?
I want to pass the value set by core.setOutput
to different steps at once. So I passed the outputs of the step hello-action
to another step:
//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 }}
point is this.
outputs: ${{ steps.hello-action.outputs }}
The type of the above value is an object, but I couldn't access the key inside the object.
//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
Is there a way to pass arguments in bulk?
Solution 1:[1]
Objects cannot be transferred to output
from action, only strings.
Therefore, when passing an object from a custom action toString()
method will be called on it. As a result, you get your [object Object]
already at the moment of performing this action here:
- name: run hello action
id: hello-action
run: node index.js
To transfer data structures from a custom action, cast object to JSON string using JSON.stringify()
method.
first action
//index1.js
const obj = {
field1: "value1",
field2: "value2",
}
core.setOutput("outputs", JSON.stringify(obj));
And in your second step, parse the JSON
string
second action
//index2.js
const core = require("@actions/core");
const outputs = JSON.parse(core.getInput("outputs"));
This should help you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |