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/coreand@actions/githubas dependencies. - Set
runs.using: "node20"andruns.main: "index.js"in action.yml. - Read inputs with
core.getInputand fail withcore.setFailed. - Commit
node_modulesor 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.getInputreads theINPUT_<NAME>env var the runner sets fromwith:.
Related guides
How to Use @actions/exec and the Toolkit in a JavaScript ActionRun shell commands from a JavaScript action with @actions/exec and capture output, using @actions/core for in…
How to Build a TypeScript Action With ncc BundlingBuild a TypeScript action and bundle it with @vercel/ncc into a single dist file, so the runner has no node_m…
How to Set Outputs From a Custom Action With GITHUB_OUTPUTSet an output from a composite or container action by appending name=value to the GITHUB_OUTPUT file, then ex…