Sub topic 5
Using @actions/core and @actions/github
This chapter assumes you can write basic workflow YAML, know JavaScript and npm, and have seen an action.yml file with inputs and outputs. It covers the two toolkit packages that a JavaScript action uses to talk to the runner and to GitHub.
Why use the toolkit
A JavaScript action is a Node script that the runner starts. You could read process.env and print special strings yourself. The toolkit wraps those details so you don't have to. Install both packages in the action's directory:
npm install @actions/core @actions/github@actions/core: talking to the runner
@actions/core handles the data going in and out of your action.
| Function | What it does |
|---|---|
| `core.getInput(name)` | Reads an input declared in `action.yml`. Always returns a string. |
| `core.getBooleanInput(name)` | Parses `true` or `false` into a boolean. Throws on other values. |
| `core.setOutput(name, value)` | Sets an output that later steps read as `steps.<id>.outputs.<name>`. |
| `core.setFailed(message)` | Logs an error and sets the exit code to 1. |
| `core.info`, `core.warning`, `core.error` | Write log lines at different levels. |
| `core.setSecret(value)` | Masks a value in the logs. |
Inputs reach your script as environment variables named INPUT_<NAME>, in upper case, with spaces turned into underscores. Hyphens are kept. getInput hides this naming rule from you. Pass { required: true } to make it throw when the input is empty.
const core = require('@actions/core');
const who = core.getInput('who-to-greet', { required: true });
core.info(`Hello ${who}!`);
core.setOutput('time', new Date().toTimeString());@actions/github: context and API access
@actions/github exposes two things.
github.contextis an object built from the runner's environment and the event payload. It haseventName,repo(withownerandrepo),sha,ref,actor, andpayload, which is the full webhook body.github.getOctokit(token)returns an authenticated Octokit client for calling the GitHub API.
Pass the token in as an input. The action cannot read GITHUB_TOKEN unless the workflow hands it over.
const core = require('@actions/core');
const github = require('@actions/github');
async function run() {
try {
const token = core.getInput('token', { required: true });
const octokit = github.getOctokit(token);
const { owner, repo } = github.context.repo;
if (github.context.eventName !== 'pull_request') {
core.info('Not a pull request, nothing to do.');
return;
}
const number = github.context.payload.pull_request.number;
await octokit.rest.issues.createComment({
owner,
repo,
issue_number: number,
body: 'Thanks for the pull request!'
});
} catch (error) {
core.setFailed(error.message);
}
}
run();The workflow supplies the token and the permission to write comments:
permissions:
pull-requests: write
steps:
- uses: ./
with:
token: ${{ secrets.GITHUB_TOKEN }}The matching action.yml needs a token input, or getInput('token') returns an empty string.
Common errors
- Empty input. The input name in
getInputmust match the name inaction.ymland inwith:. A typo gives an empty string, not an error. - Booleans as strings.
getInput('dry-run')returns'false', which is truthy in JavaScript. UsegetBooleanInput. - Missing permissions. A 403 from Octokit usually means the token lacks a permission. Set
permissions:in the workflow. - Uncaught rejections. If an async function throws outside a
tryblock, the step may fail without a clear message. Wrap the body and callsetFailed. - Missing modules.
Cannot find module '@actions/core'means the dependencies weren't committed or bundled. The next chapter covers this.
Exercise
A step uses if (core.getInput('verbose')) { ... }, and a user passes verbose: false. Why does the block still run, and what is the fix?
Show answer
getInput always returns a string, and the string 'false' is truthy. Replace the check with core.getBooleanInput('verbose'), which returns the boolean false.