Guides / Reuse config with commands, executors and parameters
Reuse config with commands, executors and parameters
Configs repeat themselves: every job checks out the code and installs the same tools, and many jobs name the same machine.
This guide moves the repeated parts into an executor and a command, then makes one job serve two purposes with a parameter.
Prerequisites
Section titled “Prerequisites”- A config with at least two jobs. The starting point here:
jobs: lint: runs-on: ubuntu-26.04-4core steps: - uses: actions/checkout@v5 - uses: actions/setup-node@v4 with: node-version: 22 - run: npm ci - run: npm run lint test: runs-on: ubuntu-26.04-4core steps: - uses: actions/checkout@v5 - uses: actions/setup-node@v4 with: node-version: 22 - run: npm ci - run: npm test
workflows: ci: on: pull_request: {} jobs: - lint - test-
Name the machine once with an executor
Section titled “Name the machine once with an executor”Add an
executors:section and point each job at it withexecutor:in place ofruns-on::.hatchrun/config.yml executors:node:runs-on: ubuntu-26.04-4corejobs:lint:executor: nodesteps:- run: npm run lintA job takes
executororruns-on, never both. To move every job to a bigger machine later, change one line. -
Move the shared steps into a command
Section titled “Move the shared steps into a command”A command is a named list of steps. Add
commands:with asetupcommand, and call it as a step by its name:.hatchrun/config.yml executors:node:runs-on: ubuntu-26.04-4corecommands:setup:steps:- uses: actions/checkout@v5- uses: actions/setup-node@v4with:node-version: 22- run: npm cijobs:lint:executor: nodesteps:- setup- run: npm run linttest:executor: nodesteps:- setup- run: npm testhatchrun replaces
- setupwith the command’s three steps when it plans the run. -
Give the command a parameter
Section titled “Give the command a parameter”Some jobs need another Node version. Declare a
nodeparameter with a default, and read it as${{ parameters.node }}:.hatchrun/config.yml commands:setup:parameters:node:type: integerdefault: 22description: Node.js major versionsteps:- uses: actions/checkout@v5- uses: actions/setup-node@v4with:node-version: ${{ parameters.node }}- run: npm cijobs:lint:runs-on: ubuntu-lateststeps:- setup- run: npm run lintlegacy:runs-on: ubuntu-lateststeps:- setup: { node: 20 }- run: npm test- setupuses the default.- setup: { node: 20 }passes a value. A parameter without adefaultis required, and every call must pass it. -
Make a job take a parameter
Section titled “Make a job take a parameter”Jobs take parameters the same way. The workflow passes the value on the entry, so one job runs twice with different values. Give the second entry a
name, since two entries cannot share an id:.hatchrun/config.yml executors:node:runs-on: ubuntu-26.04-4corecommands:setup:steps:- uses: actions/checkout@v5- run: npm cijobs:test:executor: nodeparameters:suite:type: enumenum: [unit, e2e]default: unitsteps:- setup- run: npm run test:${{ parameters.suite }}workflows:ci:on:pull_request: {}jobs:- test- test:name: e2esuite: e2eThe run plans two jobs,
testrunningnpm run test:unitande2erunningnpm run test:e2e. -
Validate
Section titled “Validate”Terminal window hatchrun validate
Parameter types
Section titled “Parameter types”type |
Accepts | Example default |
|---|---|---|
string |
A string, number or boolean | default: release |
boolean |
true or false |
default: false |
integer |
A whole number | default: 3 |
enum |
One of the strings in enum: |
default: unit |
A value that holds a ${{ }} expression passes any type check. It is
checked when it runs.
Rules for parameters
Section titled “Rules for parameters”- Only a whole
${{ parameters.NAME }}is replaced. Something like${{ parameters.suite == 'e2e' }}is an error, because parameters are gone by the time expressions run. - A string that is exactly
${{ parameters.NAME }}keeps the value’s type. Inside longer text the value becomes text. - A job parameter cannot be called
name,requires,type,branches,tagsormatrix. Workflow entries use those keys. - A command cannot be called after a step key such as
run,cacheorenv. - Commands can call commands, up to 64 levels deep, and never themselves.
Troubleshooting
Section titled “Troubleshooting”Next steps
Section titled “Next steps”- Build a matrix runs one job over many values without listing each entry.
- Configuration reference: parameters