0

I'm working on a GitHub Actions workflow using Node.js, and I need to set an output value to be used in subsequent steps. I am getting deprecated warning for for the line.

process.stdout.write(`::set-output name=my-key::'${value}'`);

Action.yml:

name: Test New Output
description: 'Task to test new output'



runs:
  using: 'node16'
  main: 'test-output.js'

Full node.js is below

const fs = require('fs');


async function run() {
  try {
    // Calculate the value for 'blocks'
    const blocks = "some value";
    process.stdout.write(`::set-output name=my-key::'${blocks}'`);
  } catch (error) {
    console.error(error);
    process.exit(1);
  }
}

run();

Can anybody suggest what is the alternative for this deprecated usage.

subair_a
  • 1,028
  • 2
  • 14
  • 19
  • See [`core.setOutput`](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#using-workflow-commands-to-access-toolkit-functions). – Azeem Aug 12 '23 at 17:33
  • I am getting errors when tried to import actions/core. – subair_a Aug 12 '23 at 21:20
  • Are you following https://docs.github.com/en/actions/creating-actions/creating-a-javascript-action? – Azeem Aug 13 '23 at 04:14
  • This is for a private project. So I can't create a public repo for custom action. My custom action works fine now. I am trying to address the warning about the usage of deprecated 'set-output' – subair_a Aug 13 '23 at 06:51
  • 1
    See https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter and you need to run it modify it for invocation from your script. – Azeem Aug 13 '23 at 09:54
  • @Azeem I didn't get it. Could you please point to an example. – subair_a Aug 13 '23 at 11:02
  • Currently, you're writing to stdout but with the new syntax of `GITHUB_OUTPUT`, you have to run it as a command and redirect its output to `GITHUB_OUTPUT`. You may use `child_process` for this if you don't want to use `core.setOutput`. – Azeem Aug 14 '23 at 08:10
  • See https://stackoverflow.com/questions/20643470/execute-a-command-line-binary-with-node-js. So, in your case, the command would be `echo "key=value" >> "$GITHUB_OUTPUT"` and you'll be invoking it with `child_process`'s `exec` or something similar. – Azeem Aug 14 '23 at 08:13

1 Answers1

0

Thanks to @Azeem. For anyone looking for a quick solution.

The .js file will look like this.

const { exec } = require('child_process');

const outputValue = 'Your output value'; // Replace this with your actual output value

exec(`echo "MY_OUTPUT_NAME=${outputValue}" >> $GITHUB_OUTPUT`);
subair_a
  • 1,028
  • 2
  • 14
  • 19