Sub topic 1
Composite actions, other action types and reusable workflows
Prerequisites
This chapter assumes you can read basic GitHub Actions workflow syntax (jobs, steps, uses, run). It also assumes you are comfortable with YAML, can read short bash commands, and know how Git tags and repositories work.
What an action is
An action is a unit of reuse that a workflow step calls with uses:. GitHub supports three kinds of action. A separate feature, the reusable workflow, solves a related problem at a different level.
The three action types
Each action lives in a directory with a metadata file named action.yml. The runs.using key in that file selects the type.
| Type | `runs.using` | What runs | Runner support |
|---|---|---|---|
| Composite action | `composite` | A list of steps, written in YAML | Linux, Windows, macOS |
| JavaScript action | `node20` (or another Node version) | A Node.js file | Linux, Windows, macOS |
| Docker container action | `docker` | A container image | Linux only |
A composite action bundles several workflow steps into one step. Its steps can be run commands or calls to other actions. You write no JavaScript and build no image, so it is the cheapest type to create and maintain.
A JavaScript action runs code directly on the runner. It starts fast and can use the Actions toolkit libraries. It needs a Node.js project, and usually a build step that commits the compiled output.
A Docker container action runs inside a container image you control. That gives you a fixed environment and any language or tool you want. The cost is a slower start, because the image must be built or pulled, and it only works on Linux runners.
Reusable workflows
A reusable workflow is not an action. It is a whole workflow file with an on: workflow_call trigger, and another workflow calls it at the job level:
jobs:
call-build:
uses: my-org/shared/.github/workflows/build.yml@v1The difference is the unit of reuse. A composite action replaces some steps inside one job, and those steps share the caller's runner and filesystem. A reusable workflow replaces one or more complete jobs, and each job gets its own runner.
Choosing between them
Use these questions to decide:
- Do you want to share a few steps that run in the caller's job and use its checked-out files? Choose a composite action.
- Do you need to share whole jobs, such as a matrix build, with their own runners,
secretsandpermissions? Choose a reusable workflow. - Do you need complex logic, API calls through a library, or fast startup? Choose a JavaScript action.
- Do you need a specific operating system environment or toolchain that you cannot install on the runner? Choose a Docker container action.
What the rest of this tutorial covers
The remaining chapters focus on composite actions. They cover the layout of action.yml, inputs and outputs, the shell requirement, bundled scripts, safe handling of inputs and secrets, testing, and releasing versions for other repositories.
Exercise
Your team wants to share a sequence of four steps (install Node.js, restore a cache, run npm ci, run lint) across many repositories. The steps must run in the caller's job and use the already checked-out code. Which option fits best, and why would a reusable workflow be a weaker fit?
Show answer
A composite action fits best. It runs the four steps inside the caller's job, on the same runner and workspace, so it can use the checked-out code. A reusable workflow runs as separate jobs on its own runners, so it would not see the caller's workspace and would need to check out the code again.