Sub topic 1
Composite actions, other action types and reusable workflows
GitHub Actions gives you four ways to reuse automation: composite actions, JavaScript actions, Docker container actions, and reusable workflows. This chapter explains how each one works and how they differ. It also shows how to choose between them. The rest of the tutorial then builds composite actions in detail.
Prerequisites
This chapter assumes you already have the following:
- Basic workflow syntax:
on,jobs,runs-on,steps,usesandrun. - Comfort reading and writing YAML.
- Basic bash scripting.
- Familiarity with Git repositories and tags.
You do not need to have written an action before.
The working model: reuse at two levels
Reuse in GitHub Actions happens at two levels.
- An action reuses one or more steps. It runs inside a job that the caller defines. You call it with
uses:on a step. - A reusable workflow reuses whole jobs. It runs as its own jobs on its own runners. You call it with
uses:on a job.
The difference matters because a step cannot choose its runner, and a job can.
The three kinds of action
Every action has a metadata file named action.yml (or action.yaml). The runs.using key in that file tells the runner how to execute it.
Composite actions
A composite action is a list of steps written in YAML. Each step is either a run step or a uses step that calls another action. There is no code to compile and no image to build.
name: Say hello
description: Print a greeting
runs:
using: composite
steps:
- shell: bash
run: echo "Hello from a composite action"The runner expands these steps inside the caller's job. They share the caller's runner, working directory, filesystem, installed tools and environment variables. That is why composite actions are fast and easy to write. It is also why they have limits, covered below.
JavaScript actions
A JavaScript action runs a Node.js file directly on the runner. The metadata names the Node runtime and the entry point:
name: Example JS action
description: Runs a Node.js entry point
runs:
using: node20
main: dist/index.jsThis kind can run on Linux, macOS and Windows. It can use the official toolkit packages (such as @actions/core), make API calls, and define pre and post scripts for setup and cleanup. You must commit the dependencies or a bundled build of them, so the repository needs a build step.
Docker container actions
A Docker container action runs inside a container image. The metadata names a Dockerfile or a published image:
name: Example Docker action
description: Runs inside a container
runs:
using: docker
image: DockerfileThis gives you full control over the operating system and tools. The cost is speed, because the runner must build or pull the image on every run. Docker actions run only on Linux runners.
Reusable workflows
A reusable workflow is a normal workflow file that declares on: workflow_call. Callers invoke it at the job level:
# .github/workflows/test.yml in the repository my-org/ci
name: Reusable test
on:
workflow_call:
inputs:
node-version:
type: string
default: "20"
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
- run: npm ci && npm test# In the calling repository
name: CI
on: [push]
jobs:
test:
uses: my-org/ci/.github/workflows/test.yml@v1
with:
node-version: "22"The test job in the caller has no runs-on and no steps. Those come from the called workflow. The called workflow chooses the runner and can define several jobs, services, needs dependencies and a strategy matrix.
How the choices compare
| Feature | Composite action | JavaScript action | Docker action | Reusable workflow |
|---|---|---|---|---|
| Called from | A step | A step | A step | A job |
| Defined in | `action.yml` | `action.yml` plus Node code | `action.yml` plus image | A workflow file with `workflow_call` |
| Runs on | The caller's runner and job | The caller's runner and job | The caller's runner, in a container | Its own runner or runners |
| Operating systems | Any runner OS (depends on the shell) | Linux, macOS, Windows | Linux only | Any, set by the workflow |
| Build step needed | No | Usually, to bundle dependencies | Image build or pull | No |
| Can define several jobs | No | No | No | Yes |
| Can set `runs-on` or a matrix | No | No | No | Yes |
How secrets differ
Secrets are a common source of confusion. A reusable workflow can declare secrets under workflow_call. The caller can pass them one by one, or pass all of them with secrets: inherit for workflows in the same organization or enterprise. A composite action has no secrets key. The secrets context is not available inside action.yml, so the caller must pass a secret in as an ordinary input, for example with: token: ${{ secrets.MY_TOKEN }}. A later chapter covers this in detail.
What a composite action looks like in a log
All the steps of a composite action appear as one step in the caller's job. You can expand it to see the inner steps. A reusable workflow appears as separate jobs in the run's graph, each with its own log. This affects debugging, because a failing inner step shows up under the action's name rather than as a top-level step.
Choosing between them
The questions below capture most decisions. A later chapter returns to this topic in more depth.
Some practical rules follow from this:
- Choose a composite action to package a repeated sequence of steps, such as "set up Node, install, cache, test". It needs no build and is easy to read.
- Choose a reusable workflow to standardize a whole pipeline across many repositories, or when you need a job-level feature such as a matrix,
servicesor a different runner. - Choose a JavaScript action when the logic is complex, needs to work on every operating system, or benefits from the toolkit libraries and structured error handling. Shell scripting becomes hard to maintain at that point.
- Choose a Docker action when you need a fixed environment that you cannot install on the runner quickly, and Linux is acceptable.
Common errors at this stage
- Calling a reusable workflow from a step.
uses: owner/repo/.github/workflows/x.yml@v1is valid only at the job level. On a step it fails, because a step expects an action. - Expecting a composite action to change the runner. It cannot. It runs on whatever runner the calling job uses.
- Using a local action without checking out. A reference such as
uses: ./.github/actions/greetreads the file from the runner's workspace. If the job has not runactions/checkoutfirst, the path does not exist. - Reading
secretsinside an action. The context is not there. Pass the value as an input. - Assuming a composite action is portable by default. A
runstep withshell: bashruns on Windows runners only because Git Bash is installed there. Commands such asapt-getstill fail.
Exercise
Your team has twelve repositories. Each one needs the same pipeline: a test job on Linux, a test job on Windows (a matrix), and a deploy job that starts only after both tests pass. Should you write a composite action or a reusable workflow, and why?
Show answer
A reusable workflow. The pipeline needs several jobs, a matrix over operating systems, and a needs dependency between jobs. A composite action runs as steps inside one job on one runner, so it can express none of these. Each repository would call the workflow with uses: at the job level.
Exercise
A step contains uses: ./.github/actions/greet, and the run fails with "Can't find 'action.yml'". Name the most likely cause and the fix.
Show answer
The job has not checked out the repository, so the local path does not exist in the workspace. Add uses: actions/checkout@v4 as an earlier step in the same job.