@streetsidesoftware/github-action-toolkit
v1.0.0
Published
A small replacement for @actions/core in the code of a GitHub Action, with no dependencies.
Maintainers
Readme
@streetsidesoftware/github-action-toolkit
A small replacement for @actions/core in the code of
a GitHub Action, with no dependencies.
An action ships with its dependencies bundled in. With @actions/core, that means an HTTP client and a command runner
too, about 700 kB, or 400 kB minified, whether the action uses them or not.
This package talks to the runner directly, so an action's bundle holds little more than its own code. It covers the basics, such as reading inputs, setting outputs, adding to the job summary and failing the step.
For what it doesn't cover, use @actions/core.
Install
npm install @streetsidesoftware/github-action-toolkitUsage
An action that counts the files in a directory declares one input and one output in its action.yaml:
inputs:
directory:
description: The directory to look in.
default: .
outputs:
count:
description: The number of files found.
runs:
using: node24
main: dist/index.jsIts code reads the input, sets the output, and reports what it did:
import { readdir } from 'node:fs/promises';
import { appendJobSummary, getInput, setFailed, setOutput } from '@streetsidesoftware/github-action-toolkit';
try {
const directory = getInput('directory');
const files = await readdir(directory);
setOutput('count', files.length);
appendJobSummary(`Found ${files.length} files in \`${directory}\`.\n`);
} catch (error) {
setFailed(String(error));
}getInput(name)
Read one of the action's inputs. The value comes back without the whitespace around it. An input without a value
gives ''.
Every input is a string. A workflow that sets dry-run: true gives 'true':
const dryRun = getInput('dry-run') === 'true';setOutput(name, value)
Set one of the action's outputs, for later steps to read from steps.<step id>.outputs.<name>.
Every output is a string too, so a number or boolean is stored as its text, such as '3' or 'true'. A string can
span several lines.
appendJobSummary(markdown)
Add Markdown to the end of the job summary, which shows on the workflow run's page.
The Markdown goes in as given, right after what the summary already holds, so end it with a newline:
appendJobSummary('## Updated\n\n');
appendJobSummary('- actions/checkout\n');setFailed(message)
Report an error to the runner and make the step fail. The message shows as an error in the step's log and on the workflow run's page.
The process keeps running. Its exit code is set to 1, so the step fails when the process ends.
Outside a workflow run
The functions talk to the GitHub Actions runner through environment variables and files. To run an action's code in a test or on your own machine, set what it uses:
| Function | Uses |
| ------------------ | ------------------------------------------------------------------------------------------------ |
| getInput | INPUT_<NAME>: the input's value. The name is in upper case, with each space as _. |
| setOutput | GITHUB_OUTPUT: the path of a file the outputs are added to. Without it, the call throws. |
| appendJobSummary | GITHUB_STEP_SUMMARY: the path of a file the Markdown is added to. Without it, the call throws. |
| setFailed | Standard output, where it prints an ::error:: line, and process.exitCode. |
Coming from @actions/core
Three of the functions have the names they have in
@actions/core, with these differences:
getInputtakes no options. It always trims the value, and never throws for an input without one.setOutputtakes a string, number or boolean. Convert any other value to a string first, such as withJSON.stringify.setFailedtakes a string. Convert anErrorfirst, such as withString(error).
In place of summary.addRaw(markdown) followed by summary.write(), call appendJobSummary(markdown).
