I have tried everything I can thing of for variations of this and have not been able to find the right syntax. I have a GitHub Actions workflow that currently has this step:
steps:
- name: Parse Assignee Properties
id: assignee_properties
uses: actions/github-script@v6
with:
script: |
const assignees = context.payload.issue.assignees;
const assigneeStrings = assignees.map(a => `{'login': '${a.login}', 'id': ${a.id}, 'url': '${a.html_url}'}`);
const jsonArrayString = `[${assigneeStrings.join(', ')}]`;
console.log(`::set-output name=assigneeJson::${jsonArrayString}`);
The above uses the old set-output command it is being removed. The documentation examples at https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/ all use echo "::set-output name={name}::{value}" and how to change them.
I’ve tried that format in the above script, it returns an error that the syntax is wrong. I tried different variables of using console.log like:
- console.log(
assigneeJson='${jsonArrayString}' >> $GITHUB_OUTPUT
); - console.log("assigneeJson=’${jsonArrayString}’ >> $env:GITHUB_OUTPUT");
- console.log("assigneeJson=’${jsonArrayString}’") >> $env:GITHUB_OUTPUT;
And none work. I’m guessing this is super simple and I’m missing something obvious but there appears to be very little documentation of using this within a script and using console.log.
>Solution :
You should not use console.log
, but rely in the imported @actions/core
library using:
core.setOutput('outputKey', 'outputVal');
To use this action, provide an input named script that contains the body of an asynchronous function call. The following arguments will be provided:
- github A pre-authenticated octokit/rest.js client with pagination plugins
context An object containing the context of the workflow run- core A reference to the @actions/core package
From the docs or @actions/core:
Inputs/Outputs
Action inputs can be read with getInput which returns a string or getBooleanInput which parses a boolean based on the yaml 1.2 specification. If required set to be false, the input should have a default value in action.yml.
Outputs can be set with setOutput which makes them available to be mapped into inputs of other actions to ensure they are decoupled.
const myInput = core.getInput('inputName', { required: true }); const myBooleanInput = core.getBooleanInput('booleanInputName', { required: true }); const myMultilineInput = core.getMultilineInput('multilineInputName', { required: true }); core.setOutput('outputKey', 'outputVal');
Alternatively you can set the setOutput
property of the github-script
action:
- uses: actions/github-script@v6
id: set-result
with:
script: return "Hello!"
result-encoding: string
The return value will be auto-assigned to an output variable called result
.
- name: Get result
run: echo "${{steps.set-result.outputs.result}}"
This will work when you only need to assign a single value and when you don’t care about the name being result
.