Sub topic 13
Running setup and cleanup with pre and post
This chapter assumes you can already write a JavaScript action with an action.yml file, and that you know core.getInput and core.setOutput. Here you add code that runs before and after the main step.
What pre and post do
A JavaScript action normally runs one script, set by runs.main. Two optional keys add more scripts to its lifecycle:
preruns at the start of the job, before any step runs.postruns at the end of the job, after all steps have finished.
Use pre for setup that must happen early, such as warming a cache. Use post for cleanup, such as deleting temporary credentials or stopping a background process. Because post runs at the end of the job, it still runs after later steps fail.
Declaring the scripts
Add the keys under runs in action.yml:
name: 'Timed task'
description: 'Reports how long the job took to reach the end'
runs:
using: 'node20'
pre: 'pre.js'
pre-if: runner.os == 'linux'
main: 'index.js'
post: 'post.js'
post-if: success()pre-if and post-if are conditions. Both default to always(). In this example, pre runs only on Linux runners, and post runs only if the job has succeeded so far. Use post-if: always() for cleanup that must happen even after a failure.
Passing data between scripts
Each script runs as a separate process, so variables do not carry over. Use action state instead. core.saveState writes a value to the file named by GITHUB_STATE. core.getState reads it back in a later script of the same action.
// pre.js
const core = require('@actions/core');
core.saveState('startTime', String(Date.now()));
console.log('Setup finished');// post.js
const core = require('@actions/core');
const start = Number(core.getState('startTime'));
const seconds = Math.round((Date.now() - start) / 1000);
console.log(`Cleanup after ${seconds} seconds`);State values are strings, so convert numbers yourself. State belongs to one step of one action. A second use of the same action in the same job has its own state.
Ordering and behavior
The runner executes post scripts in the reverse order of the steps. If your workflow uses action A and then action B, B's post runs first. This matches how nested cleanup usually works.
A post script runs only if its step ran. If the step's own if: condition skipped it, the post script is skipped too.
Container actions
Docker actions have matching keys: pre-entrypoint and post-entrypoint. Each names a script that runs in a container made from the same image as the action. The pre-if and post-if keys work the same way.
runs:
using: 'docker'
image: 'Dockerfile'
pre-entrypoint: '/setup.sh'
entrypoint: '/entrypoint.sh'
post-entrypoint: '/cleanup.sh'Copy these scripts into the image in your Dockerfile. Each one starts its own container, so state saved in one container is not in the filesystem of the next. Pass data through GITHUB_STATE the same way.
Common errors
| Symptom | Likely cause |
|---|---|
| `post` never runs | The step was skipped, or `post-if` evaluated to false |
| `getState` returns an empty string | The key name differs between `saveState` and `getState`, or the state was saved by a different action step |
| Cleanup skipped after a failure | `post-if` is `success()` instead of `always()` |
| Script file not found | The file is not committed, or the path in `action.yml` is wrong |
Exercise
Your action starts a background server in its main script and must stop it even if a later step fails. Which action.yml setting do you use, and how does the post script find the server's process ID?
Show answer
Add post: 'post.js' with post-if: always() under runs. In the main script, call core.saveState('pid', String(pid)). In post.js, call core.getState('pid') to read it and stop the process.