Sub topic 1
Choosing between JavaScript, container, and composite actions
A custom action packages reusable automation so that many workflows can call it with a single uses: line. GitHub supports three kinds of custom action. Each one runs your logic in a different way, and the choice affects speed, portability, and how much work you do to maintain it. This chapter gives you the working model for each type, a way to decide between them, and the errors people commonly make.
Prerequisites
This chapter assumes you already have the following:
- Basic GitHub Actions workflow syntax: jobs, steps,
uses, andwith. - Working knowledge of JavaScript and npm.
- Basic Docker concepts: Dockerfile, image, and container.
- Basic Git usage: commits, tags, and branches.
- Comfort with a shell such as bash.
You do not need to have written an action before. Later chapters build each type in detail. Here you only learn to choose.
The three action types
Every action has a metadata file named action.yml (or action.yaml) at its root. The runs: key in that file tells the runner which type of action it is.
| Type | `runs.using` value | Where your code runs | Runner operating systems |
|---|---|---|---|
| JavaScript | `node20` (or another supported Node version) | Directly on the runner machine, in a Node process | Linux, macOS, Windows |
| Docker container | `docker` | Inside a container built from your image | Linux only |
| Composite | `composite` | A list of steps run in the calling job | Linux, macOS, Windows |
JavaScript actions
A JavaScript action is a Node program. The runner starts Node, loads the file named in runs.main, and runs it on the runner machine itself. There is no container layer, so startup is fast, and the action works on every runner operating system.
Because it runs on the host, a JavaScript action can use the @actions/core and @actions/github toolkit packages to read inputs, set outputs, and read the event payload. It can also see the runner's file system and the tools already installed on it.
The cost is that your dependencies must be available when the action runs. The runner does not run npm install for you. You either commit node_modules or bundle your code into a single file. A later chapter covers bundling.
Docker container actions
A Docker container action runs inside a container. The runs.image key points at a Dockerfile in your repository, or at an image in a registry. The runner builds or pulls the image, starts a container, and runs the entrypoint.
The container gives you complete control of the environment. You pick the base image, the language, and the exact tool versions. This suits actions that need a specific toolchain, such as a particular Python version or a command-line tool that is awkward to install on the runner.
The limits are real:
- Container actions run only on Linux runners. A job on
windows-latestormacos-latestcannot use one. - When
imagepoints at a Dockerfile, the runner builds the image on every run. A large or slow build delays every job that uses the action. - The container has a different file system view from the runner's host. The workspace is mounted in, but tools installed on the runner are not available inside.
Composite actions
A composite action has no code of its own to execute. Its runs: block is a list of steps, and those steps are the same kind you write in a workflow. Each step is either a run: command or a uses: call to another action.
name: 'Setup and lint'
description: 'Install dependencies and run the linter'
inputs:
node-version:
description: 'Node version to install'
required: false
default: '20'
runs:
using: 'composite'
steps:
- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
- run: npm ci
shell: bash
- run: npm run lint
shell: bashNote the shell: bash line on each run: step. Composite actions require you to name the shell explicitly. A run: step without shell is rejected.
Composite actions are the cheapest to write and need no build step. They suit cases where you want to group several existing steps under one name. They are a poor fit when you need real logic, such as parsing an API response or branching on complex conditions, because you are limited to shell scripting and expressions.
A decision flow
Ask these questions in order. The first one that gives a definite answer usually settles the choice.
The same reasoning in words:
- If the job is to run a few commands or other actions in sequence, use a composite action. Do not write code you do not need.
- If the action must run on Windows or macOS, a container action is ruled out. Use JavaScript or composite.
- If the action needs a language other than JavaScript, or system packages the runner lacks, use a container action.
- Otherwise, use JavaScript. It starts fastest and works everywhere.
Comparing the trade-offs
| Concern | JavaScript | Docker container | Composite |
|---|---|---|---|
| Startup time | Fast | Slower: image build or pull | Fast |
| Cross-platform | Yes | Linux only | Yes |
| Language choice | JavaScript or anything that compiles to it | Any | Shell and existing actions |
| Environment control | Limited to the runner | Full | Limited to the runner |
| Build step before release | Bundling or committing `node_modules` | Dockerfile (built at run time) | None |
| Best for | API calls, event handling, file processing | Custom toolchains, non-JavaScript tools | Grouping steps |
Two points deserve emphasis. First, speed matters more than it looks. A container action that builds its Dockerfile adds that build time to every run in every repository that uses it. Publishing a prebuilt image and referencing it as image: 'docker://ghcr.io/your-org/your-image:1.0.0' removes the build from the job. Second, a JavaScript action avoids the cross-platform limit but cannot easily run a tool written in another language. If you need to call a Python script, a container or composite action is simpler.
What the runner does with each type
The runner treats each type differently at the start of a step. This explains the performance differences above.
For all three types, the runner first downloads the action's repository at the version named in uses:, then reads action.yml. What happens next depends on runs.using.
A minimal metadata file for each type
The runs: block is the part that differs. The inputs and outputs sections look the same in all three.
# JavaScript
runs:
using: 'node20'
main: 'index.js'# Docker container
runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.who-to-greet }}# Composite
runs:
using: 'composite'
steps:
- run: echo "Hello ${{ inputs.who-to-greet }}"
shell: bashThe later chapters expand the first two into full working actions. The composite example above is complete enough to use as written, as long as the metadata file also declares the who-to-greet input.
Common errors
- Choosing a container action, then using it on a macOS or Windows job. The job fails because Docker container actions need a Linux runner. Check where the action will be used before choosing.
- Writing a JavaScript action and forgetting dependencies. The step fails with
Cannot find module '@actions/core'. The runner does not install packages. Bundle the code or commitnode_modules. - Omitting
shellin a compositerun:step. The action fails validation. Addshell: bashor another shell to eachrun:step. - Using a composite action for logic that is too complex. If the steps grow into long shell scripts with nested conditions, they become hard to test. Move the logic into a JavaScript or container action.
- Building a large image on every run. Publish the image to a registry and reference it with
docker://so jobs do not pay the build cost. - Assuming a container sees the runner's tools. A tool installed by an earlier step on the runner is not present inside the container. Install what you need in the Dockerfile.
Summary of the choice
Pick the type from the constraints, not from habit. Composite actions are for combining steps. JavaScript actions are for logic that must run quickly on any operating system. Container actions are for work that needs an environment you control, at the cost of Linux-only support and slower startup.
Exercise
Your team wants an action that runs a Python-based security scanner with a pinned version of Python and several system libraries. The action will be used only on Linux runners. Which action type fits best, and what would you do to keep startup fast?
Show answer
A Docker container action fits best, because it lets you pin Python and install the system libraries in the image. To keep startup fast, build the image ahead of time, publish it to a registry, and set runs.image to a docker:// reference so the job pulls a prebuilt image instead of building the Dockerfile on every run.
Exercise
A colleague wrote a JavaScript action whose index.js starts with require('@actions/core'). It works on their machine but fails on the runner with "Cannot find module". What is the cause, and name two ways to fix it?
Show answer
The runner does not run npm install for a JavaScript action, so the dependency is missing at run time. Fix it by committing the node_modules directory, or by bundling the code and its dependencies into a single file (for example with a bundler such as @vercel/ncc) and pointing runs.main at that file.
Exercise
You wrote a composite action with a step - run: npm ci. The workflow fails before the step runs. What is missing?
Show answer
The step has no shell key. Composite run: steps must set one explicitly, for example shell: bash.