How to Add Pre and Post Steps to a Custom Action
The pre and post keys run extra scripts before and after a JavaScript action main, ideal for setup and cleanup.
Add runs.pre and runs.post in action.yml. The runner executes pre before the job step, main during it, and post at the end of the job even if a later step failed.
Steps
- Point
runs.mainat the primary script. - Add
runs.prefor setup that runs before the step. - Add
runs.postfor cleanup that always runs at job end.
action.yml
action.yml
runs:
using: "node20"
pre: "setup.js"
main: "index.js"
post: "cleanup.js"
post-if: "always()"cleanup.js
index.js
const core = require('@actions/core');
core.info('Tearing down the temporary tunnel');Gotchas
preandpostare supported for JavaScript actions, not composite or Docker.- Use
post-ifto control whether cleanup runs on success, failure, or always.
Related guides
How to Write a JavaScript Action in GitHub ActionsCreate a JavaScript action with runs.using node20 and an entry script that uses @actions/core and @actions/gi…
How to Use @actions/exec and the Toolkit in a JavaScript ActionRun shell commands from a JavaScript action with @actions/exec and capture output, using @actions/core for in…
How to Version a Custom Action With Tags and a Major BranchVersion a custom action with a semantic tag per release plus a moving major tag like v1, so consumers can pin…