How to Enforce a Build-Time Budget in GitHub Actions
Timing the build command and comparing the elapsed seconds to a budget keeps a creeping build from quietly getting slower.
Record a start timestamp, run the build, compute the elapsed seconds, and exit non-zero when it exceeds the budget. A per-step timeout-minutes is a coarse backstop.
Steps
- Capture
SECONDS(ordate +%s) before the build. - Run the build, then compute elapsed time.
- Exit non-zero when elapsed exceeds the budget.
Workflow
.github/workflows/ci.yml
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version: 20 }
- run: npm ci
- name: Build within budget
run: |
START=$SECONDS
npm run build
ELAPSED=$(( SECONDS - START ))
echo "build took ${ELAPSED}s budget=120s"
test "$ELAPSED" -le 120Gotchas
- A cold cache inflates the first build; warm the cache or budget for it.
- Latchkey runs the same workflow on faster managed runners, so a tight build budget is easier to hold.
Related guides
How to Profile a Slow Build in GitHub ActionsFind where build time goes in GitHub Actions by enabling your bundler profiler, such as esbuild metafile or w…
How to Cache a Browser and Dependencies to Speed Up the Perf Job in GitHub ActionsCut the runtime of a GitHub Actions performance job by caching the dependency store and the headless Chrome t…