Github @actions/exec

Published Mon Feb 05 2024

Today I learned about Github’s @actions/exec package. This allows you to execute shell commands from JavaScript in a Github action! There are 2 methods to use, the one I needed was not in the README example for this library.


First is exec.exec. This method runs the provided command, returning a Promise that resolves once the command has finished running.

const exec = require('@actions/exec');

await exec.exec('nu', ['']);

When you need the output of the executed command this example code is provided:

const exec = require('@actions/exec');

let myOutput = '';
let myError = '';

const options = {};
options.listeners = {
  stdout: (data: Buffer) => {
    myOutput += data.toString();
  stderr: (data: Buffer) => {
    myError += data.toString();
options.cwd = './lib';

await exec.exec('nu', [''], options);

This setup with options.listeners to collect stdout and stderr is not all needed. The library includes an additional helper named getExecOutput that includes this bit of code for you. This was not as visible in the documentation as it should be since getting output is likely a common use case.

const { stdout } = await exec.getExecOutput('nu', ['']);

The Github actions toolkit is very conveniently set up. I have liked using this @actions/exec package.