Skip to content
Latchkey

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 (or date +%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 120

Gotchas

  • 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

Run this faster and cheaper on Latchkey managed runners. Start free →