Skip to content
Latchkey

How to Run CI on Spot Instances to Cut Cost and Waste

Spot instances sell spare datacenter capacity that would otherwise sit idle, at a large discount.

Spot and preemptible instances use hardware the cloud provider already runs and would otherwise waste. For interruptible CI work they cut cost sharply; the price is that the provider can reclaim the machine, so jobs must tolerate a mid-run stop.

Steps

  • Provision the runner pool on spot or preemptible instances.
  • Keep jobs short and idempotent so a reclaimed machine can be retried cleanly.
  • Retry the queued job automatically when an instance is interrupted.

Retry interrupted work

.github/workflows/ci.yml
jobs:
  build:
    runs-on: [self-hosted, spot]
    steps:
      - uses: actions/checkout@v4
      - uses: nick-fields/retry@v3
        with:
          max_attempts: 2
          timeout_minutes: 20
          command: npm ci && npm run build

Tradeoffs

  • Never put non-interruptible deploys on spot; reserve on-demand capacity for those.
  • Interruptions cost a re-run, so spot pays off only when the discount beats the retry rate.

Related guides

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