Skip to content

πŸ§‘β€πŸš’ GHA action to run a single command and capture the outputs

License

Notifications You must be signed in to change notification settings

gh640/command-result-action

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Repository files navigation

Command Result Action

This is a reusable action for GitHub Actions. This action just runs a single command and captures the outputs. Easy to use and useful to capture multi-line outputs.

Motivation

The way to capture a command's multi-line standard output / error is not intuitive on GitHub Actions.

Usage

Use this action gh640/command-result-action in your workflow with command input.

uses: gh640/command-result-action@v1
with:
  command: npm outdated
id: myaction

You can optionally change the working directory by passing cwd as input:

uses: gh640/command-result-action@v1
with:
  command: npm outdated
  cwd: ./src
id: myaction

You can use the output in subsequent steps. In the above case, the following variables are available:

  • ${{ steps.myaction.outputs.exitCode }}
  • ${{ steps.myaction.outputs.stdout }}
  • ${{ steps.myaction.outputs.stderr }}

Available inputs

name required description
command βœ“ Command to run
cwd Working directory

Available outputs

name description
exitCode Exit code of the command
stdout Stdout of the command
stderr Stderr of the command

Migrating from a standard run command

Change this

run: npm outdated
working-directory: ./src
id: myaction

to

uses: gh640/command-result-action@v1
with:
  command: npm outdated
  cwd: ./src
id: myaction

Please keep in mind that the run will not fail and subsequent steps will be executed even if the command exit code is not 0 when using gh640/command-result-action.

Different approaches to capture standard output / error

actions/github-script action

This action uses @actions/exec and @actions/core. The behavior of this action is almost same as one of the following action with actions/github-script:

uses: actions/github-script@v6
with:
  script: |
    const result = await exec.getExecOutput('npm outdated', [], {
      ignoreReturnCode: true,
    })
    core.setOutput('exitCode', result.exitCode)
    core.setOutput('stdout', result.stdout)
    core.setOutput('stderr', result.stderr)
id: myaction

Working Example

Questions

Can I execute multiple commands with this action?

No. This action supports only one command.

About

πŸ§‘β€πŸš’ GHA action to run a single command and capture the outputs

Topics

Resources

License

Stars

Watchers

Forks