Skip to content
Latchkey

How to Write a JavaScript Action in GitHub Actions

A JavaScript action runs a Node entry file directly on the runner via runs.using node20 and the Actions toolkit.

Set runs.using: "node20" and runs.main to your entry file. Read inputs with core.getInput and set outputs with core.setOutput from the @actions/core toolkit package.

Steps

  • Add @actions/core and @actions/github as dependencies.
  • Set runs.using: "node20" and runs.main: "index.js" in action.yml.
  • Read inputs with core.getInput and fail with core.setFailed.
  • Commit node_modules or bundle the code (see the ncc guide).

action.yml

action.yml
name: 'Greet'
inputs:
  who:
    description: 'Name to greet'
    default: 'world'
outputs:
  greeting:
    description: 'The greeting text'
runs:
  using: "node20"
  main: "index.js"

index.js

index.js
const core = require('@actions/core');
const github = require('@actions/github');

try {
  const who = core.getInput('who');
  const greeting = `Hello, ${who}!`;
  core.info(`Running in ${github.context.repo.repo}`);
  core.setOutput('greeting', greeting);
} catch (err) {
  core.setFailed(err.message);
}

Gotchas

  • The runner does not run npm install; ship dependencies with the action.
  • core.getInput reads the INPUT_<NAME> env var the runner sets from with:.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →