Sub topic 4
Writing a JavaScript action
This chapter assumes you know basic workflow syntax (jobs, steps, uses, with), can write JavaScript with npm, and have read how action.yml declares inputs and outputs. You will build a small JavaScript action and see how the runner executes it.
How a JavaScript action runs
A JavaScript action is a Node.js program that the runner starts directly on the runner machine. There is no container, so it starts fast and works on Linux, Windows, and macOS runners. The runs section of action.yml names the Node runtime and the entry file.
name: 'Hello World'
description: 'Greet someone and record the time'
inputs:
who-to-greet:
description: 'Who to greet'
required: true
default: 'World'
outputs:
time:
description: 'The time we greeted you'
runs:
using: 'node20'
main: 'index.js'using: 'node20' selects the Node 20 runtime. main is the file the runner executes. The path is relative to the action's root directory.
The entry script
The script reads inputs, does its work, and sets outputs. The @actions/core package wraps the runner's input and output conventions. The @actions/github package exposes details of the event that triggered the workflow.
const core = require('@actions/core');
const github = require('@actions/github');
try {
const nameToGreet = core.getInput('who-to-greet');
console.log(`Hello ${nameToGreet}!`);
core.setOutput('time', new Date().toTimeString());
console.log(`Event name: ${github.context.eventName}`);
} catch (error) {
core.setFailed(error.message);
}Each call does one job:
core.getInput('who-to-greet')returns the value as a string. The runner passes inputs to the process as environment variables, andgetInputreads them for you.core.setOutput('time', ...)makes the value available assteps.<id>.outputs.timein later steps.github.context.eventNameis the event that started the workflow, such aspush.core.setFailed(error.message)prints the message as an error and sets the process exit code to 1, which fails the step.
Install the dependencies
The script requires two packages, so initialize the project and install them.
npm init -y
npm install @actions/core @actions/githubThe runner does not run npm install for you. If node_modules is missing when the action runs, require throws Cannot find module. Later chapters cover bundling, which solves this problem. For now, you can commit node_modules, but a bundle is the better long-term choice.
Common errors
| Symptom | Likely cause |
|---|---|
| `Cannot find module '@actions/core'` | Dependencies were not committed or bundled. |
| Input is an empty string | The input name in `getInput` does not match `action.yml`, or the caller did not set it and no default exists. |
| Step passes after an error | The code logged the error but never called `setFailed` or exited non-zero. |
| Runtime warning about Node versions | `using` names a runtime that GitHub has deprecated. |
Try it from a workflow
Use uses: ./ to run the action from the repository root. Check out the code first, because the runner needs the action files on disk.
name: Test hello action
on: [push]
jobs:
hello:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run the local action
id: hello
uses: ./
with:
who-to-greet: 'Mona'
- name: Show the output
run: echo "The time was ${{ steps.hello.outputs.time }}"The id: hello on the action step is what lets the last step read steps.hello.outputs.time.
Exercise
A teammate's JavaScript action logs "Something went wrong" in a catch block, yet the workflow step shows a green check. What is missing, and what is the one-line fix?
Show answer
The catch block never marks the step as failed. The runner only fails a step when the process exits with a non-zero code. Add core.setFailed(error.message); in the catch block, which prints the message as an error and sets exit code 1.