Build a custom JavaScript or Docker action
Publish your own action with inputs and outputs.
By Invicly Tech, , in Git, GitHub & GitHub Actions. About 11 minutes to read.
In short
A custom action packages logic behind a simple interface of inputs and outputs, defined in action.yml. JavaScript actions start fastest, Docker actions bring any toolchain, and composite actions just chain steps.
Explained simply
Sometimes no Marketplace action does exactly what you need, and a long shell script in every workflow is hard to maintain. A custom action packages your logic behind a clean interface: a few named inputs go in, a few outputs come out, and users write a single uses: line.
Every action has a metadata file called action.yml. It is the action's label: name, description, inputs, outputs, and how it runs. GitHub reads this file to know what to start.
There are three kinds. JavaScript actions run directly with the Node.js that is already on the runner, so they start fast and work on Linux, Windows and macOS. Docker actions run in a container, so you can use any language, but only on Linux. Composite actions are just a list of steps.
When it works, you tag a version and others can use it, either from your organization or from the public GitHub Marketplace. Like any library, you keep it stable, document the inputs, and use version numbers so users know when an upgrade might break them.
Press Play on a diagram to walk through it one step at a time.
- Runs on Node directly
- Fast startup
- All runner OSes
- Use @actions/core toolkit
- Runs in a container
- Any language or tools
- Linux runners only
- Slower to start
- A list of steps
- No build needed
- Calls other actions
- Good for glue
- 1Write action.yml
Name, inputs, outputs and runs entry point.
- 2Code with toolkit
getInput, setOutput and setFailed from @actions/core.
- 3Bundle to dist
Compile code and dependencies into one file.
- 4Test in workflow
uses: ./ runs it from the same repo.
- 5Tag a release
Push v1.0.0 and move the v1 tag.
- 1Download action repo at tag v1Runner to Runner: Download action repo at tag v1
- Runner to Action code: node dist/index.js with INPUT_* vars2node dist/index.js with INPUT_* vars
- Action code to GitHub API: Octokit call with the token input3Octokit call with the token input
- GitHub API to Action code: API response4API response
- Action code to Runner: setOutput writes to GITHUB_OUTPUT5setOutput writes to GITHUB_OUTPUT
- 6Exit code 0 is success; setFailed is failureRunner to Runner: Exit code 0 is success; setFailed is failure
In depth
action.yml in detail
inputs: each has a description and may have required and default. Inside the action they arrive as environment variables named INPUT_ plus the input name in capitals with spaces turned into underscores, which is what core.getInput reads. All inputs are strings; getBooleanInput accepts true or false in the usual YAML spellings. outputs: list names and descriptions (composite actions also need value:).
runs: for JavaScript says using: node20 or node24 and main: dist/index.js, with optional pre: and post: scripts that run before and after the job, handy for cleanup. For Docker it says using: docker and image:, plus optional args and entrypoint. branding: sets an icon and color for the Marketplace listing.
- Input who-to-greet becomes env INPUT_WHO-TO-GREET
- post: runs at the end of the job, even after failures by default
- core.saveState and core.getState pass data from main to post
The toolkit and bundling
GitHub publishes npm packages for action authors. @actions/core reads inputs, sets outputs, fails the step (setFailed), masks values (setSecret), writes warning and error annotations that show on the PR, groups log lines and writes job summaries. @actions/github gives you the event context and an authenticated Octokit client. @actions/exec runs commands, @actions/io moves files, and @actions/tool-cache downloads and caches tools.
The runner does not run npm install for your action. It downloads the repo at the tag and runs the main file. So you bundle your code and all dependencies into one file with a tool such as @vercel/ncc, esbuild or Rollup, and commit dist/. A small CI check that rebuilds and runs git diff --exit-code dist/ catches a forgotten rebuild.
- 1Workflow step
uses: acme/size-label@v1 with inputs
- 2action.yml
runs: using: node24, main: dist/index.js
- 3dist/index.js
Your code bundled with its dependencies
- 4Toolkit calls
getInput, Octokit, setOutput
- 5Runner
INPUT_* env vars, GITHUB_OUTPUT file, token
Docker actions: rules that trip people up
With image: Dockerfile, the runner builds the image at the start of every job that uses the action, which adds time. With image: docker://ghcr.io/acme/tool:1.2, it only pulls a prebuilt image, which is faster. Either way, Docker actions need a Linux runner with Docker.
The runner mounts the workspace into the container at /github/workspace and sets the working directory itself, so GitHub recommends not using WORKDIR in your Dockerfile. The container must run as the default user (root), because the mounted files and the GITHUB_OUTPUT file must be writable. Inputs reach the container as INPUT_* variables, and you can also pass them as args in action.yml. Write outputs by appending to $GITHUB_OUTPUT, just like in a run step.
Testing and releasing an action
Test at two levels. Unit tests (for example with Jest or Vitest) call your functions with a mocked @actions/core. Integration tests run the real action in a workflow of its own repo with uses: ./, then check its outputs in a later step. For a JavaScript action, run that workflow on a matrix of ubuntu, windows and macos to catch path and shell differences.
Release with semantic version tags. Push v1.4.0, then move the major tag: git tag -f v1 v1.4.0 and git push -f origin v1. Users on @v1 get the update. Publishing to the Marketplace needs a public repo with action.yml at the root and a unique action name; you tick the publish option when drafting a GitHub Release.
- 1Change code
Plus unit tests
- 2Rebuild dist
CI checks dist is current
- 3Test with uses: ./
Matrix of runner OSes
- 4Tag v1.x.y
Move v1 to it
- 5Users upgrade
Issues and feedback
- Back to step 1
Learn it in 5 short steps
About 50m each. Track them with a timer in your plan.
- 1Compare JavaScript, Docker and composite actions~50m
- 2Define inputs and outputs in action.yml~50m
- 3Build a JavaScript action with the toolkit~50m
- 4Test the action in a workflow~50m
- 5Tag and publish a release~50m
Key points
- action.yml declares inputs, outputs and how the action runs.
- Commit the bundled dist file; runners do not install your node_modules.
- Fail clearly with core.setFailed so the step and job turn red.
- Publish semver tags and a moving major tag like v1 for users.
Key terms
Tap a card to flip it. 0/4 known
Examples
# action.yml
name: greet
description: Say hello and output the time
inputs:
who: { description: Name to greet, required: true }
outputs:
time: { description: When we greeted }
runs:
using: node24
main: dist/index.js
# src/index.js
# const core = require("@actions/core");
# core.info(`Hello ${core.getInput("who")}`);
# core.setOutput("time", new Date().toISOString());Metadata plus the core of a JavaScript action.
A JavaScript action that labels pull requests by size
Reviewers want to see at a glance how big a PR is. Step 1: action.yml declares a required token input and a label output, using node24 with main: dist/index.js. Step 2: the code reads the PR from the event payload and counts added plus deleted lines. Step 3: it adds size/S, size/M or size/L through Octokit. Step 4: any error fails the step clearly. The package uses "type": "module" so top-level await works; bundle with npx @vercel/ncc build src/index.js -o dist.
import * as core from "@actions/core";
import * as github from "@actions/github";
try {
const octokit = github.getOctokit(core.getInput("token", { required: true }));
const pr = github.context.payload.pull_request;
if (!pr) throw new Error("Run this action on pull_request events");
const size = pr.additions + pr.deletions;
const label = size < 100 ? "size/S" : size < 500 ? "size/M" : "size/L";
await octokit.rest.issues.addLabels({ ...github.context.repo, issue_number: pr.number, labels: [label] });
core.setOutput("label", label);
} catch (err) {
core.setFailed(err instanceof Error ? err.message : String(err));
}A small Docker action that runs ShellCheck
You want a reusable linter for shell scripts. Step 1: action.yml says using: docker with image: Dockerfile and passes the path input as an argument. Step 2: the Dockerfile installs shellcheck on a small Alpine image, with no WORKDIR. Step 3: entrypoint.sh (executable, starting with #!/bin/sh) finds every .sh file under the path and runs shellcheck on them. A non-zero exit fails the step.
# action.yml
name: shellcheck-dir
description: Run ShellCheck on every .sh file in a folder
inputs:
path: { description: Folder to scan, default: "." }
runs:
using: docker
image: Dockerfile
args: ["${{ inputs.path }}"]
# Dockerfile
FROM alpine:3.20
RUN apk add --no-cache shellcheck findutils
COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
# entrypoint.sh: find "$1" -name '*.sh' -print0 | xargs -0 -r shellcheckQuick reference
- Inputs reach the action as INPUT_NAME env vars; core.getInput reads them
- runs.using: node20 or node24 for JavaScript, docker for containers, composite for steps
- Commit the bundled dist/ file; the runner never installs your dependencies
- npx @vercel/ncc build src/index.js -o dist bundles code and node_modules
- core.setFailed(message) marks the step failed with a clear error
- Docker actions: Linux runners only, run as root, avoid WORKDIR
- Release: tag v1.2.3, then git tag -f v1 v1.2.3 && git push -f origin v1
- Test locally in the action's repo with uses: ./
Common mistakes
- Forgetting to rebuild dist before tagging, so users run old code.
- Choosing a Docker action when users need Windows or macOS runners.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Write action.yml for a JavaScript action called hello with a required input name and an output greeting.
- Exercise 2medium
Implement the hello action, bundle it, and test it in the same repo, printing the output in a later step.
- Exercise 3hard
Add a CI job that fails if dist/ is out of date, and write the commands to release v1.1.0 and move v1.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Should I write my action in JavaScript or TypeScript?
- TypeScript is popular because the toolkit ships with types; it compiles to JavaScript before bundling. GitHub's official action templates offer both.
- Can a Docker action run on Windows or macOS runners?
- No. Docker container actions only run on Linux runners. Use a JavaScript or composite action if users need other operating systems.
- How do users get a token into my action?
- Declare an input like token with default ${{ github.token }}, so it works out of the box but can be overridden with a different token.
- Does publishing to the Marketplace cost anything?
- No. You need a public repo with action.yml at the root and a unique name, and you choose to publish when you draft a release.