|
| 1 | +# GitHub Actions — cost & deploy playbook |
| 2 | + |
| 3 | +Copy these patterns into product monorepos. Goal: keep private-repo |
| 4 | +**billable minutes under the free ~2,000/month** (Windows ×2, macOS ×10). |
| 5 | + |
| 6 | +## Defaults every deploy workflow should have |
| 7 | + |
| 8 | +| Control | Why | |
| 9 | +|---------|-----| |
| 10 | +| `paths:` on `push` | Unrelated monorepo edits must not rebuild docker | |
| 11 | +| Never `packages/**` | Prefer explicit `packages/<name>/**` per consumer | |
| 12 | +| No `pull_request` on deploy | PR CI lives in `review.yml`; deploy = main only | |
| 13 | +| `concurrency` + `cancel-in-progress: true` | Rapid main pushes: only latest SHA finishes | |
| 14 | +| `docker/build-push-action` + `cache-from/to: type=gha,scope=<img>` | Warm builds cut 40–60% of docker time | |
| 15 | +| `permissions.actions: write` | Required to write GHA docker cache | |
| 16 | +| `timeout-minutes` on light jobs | Bound runaway runners (e.g. release-please) | |
| 17 | + |
| 18 | +## Path fan-out rules |
| 19 | + |
| 20 | +```yaml |
| 21 | +# BAD — one package edit rebuilds api + web + worker |
| 22 | +paths: |
| 23 | + - "packages/**" |
| 24 | + |
| 25 | +# GOOD — each service lists only packages it COPYs / imports |
| 26 | +# api/worker (python workspace dep): |
| 27 | +paths: |
| 28 | + - "apps/api/**" |
| 29 | + - "packages/core/**" |
| 30 | +# web (bun workspace tokens/i18n only): |
| 31 | +paths: |
| 32 | + - "apps/web/**" |
| 33 | + - "packages/design-tokens/**" |
| 34 | + - "packages/i18n/**" |
| 35 | +``` |
| 36 | +
|
| 37 | +If the Dockerfile context is `apps/<svc>` only and does **not** COPY monorepo |
| 38 | +`packages/`, do not list packages at all. |
| 39 | + |
| 40 | +## Docker build snippet (canonical) |
| 41 | + |
| 42 | +```yaml |
| 43 | +permissions: |
| 44 | + contents: read |
| 45 | + id-token: write |
| 46 | + actions: write |
| 47 | +
|
| 48 | +concurrency: |
| 49 | + group: deploy-<svc> |
| 50 | + cancel-in-progress: true |
| 51 | +
|
| 52 | +steps: |
| 53 | + - uses: docker/setup-buildx-action@v4 |
| 54 | + - uses: docker/build-push-action@v6 |
| 55 | + with: |
| 56 | + context: apps/api # or monorepo root if Dockerfile needs packages/* |
| 57 | + file: apps/api/Dockerfile |
| 58 | + push: true |
| 59 | + platforms: linux/amd64 |
| 60 | + tags: ${{ env.IMAGE }}:${{ github.sha }} |
| 61 | + cache-from: type=gha,scope=<unique-image-name> |
| 62 | + cache-to: type=gha,mode=max,scope=<unique-image-name> |
| 63 | +``` |
| 64 | + |
| 65 | +Use a **unique `scope` per image** so api/web/worker caches do not clobber each other. |
| 66 | + |
| 67 | +## release-please |
| 68 | + |
| 69 | +```yaml |
| 70 | +on: |
| 71 | + push: |
| 72 | + branches: [main] |
| 73 | + paths-ignore: |
| 74 | + - "**.md" |
| 75 | + - "docs/**" |
| 76 | +concurrency: |
| 77 | + group: release-please |
| 78 | + cancel-in-progress: true |
| 79 | +``` |
| 80 | + |
| 81 | +## Measure usage |
| 82 | + |
| 83 | +```bash |
| 84 | +# Org monthly Actions usage (enhanced billing) |
| 85 | +gh api "orgs/<org>/settings/billing/usage?year=YYYY&month=M" \ |
| 86 | + | jq '[.usageItems[] |
| 87 | + | select(.product=="actions" and .unitType=="Minutes") |
| 88 | + | {repo: .repositoryName, sku, qty: .quantity}]' |
| 89 | +``` |
| 90 | + |
| 91 | +Public repos usually do not count against the free private minutes quota; |
| 92 | +private Linux minutes count 1×. |
| 93 | + |
| 94 | +## What not to do |
| 95 | + |
| 96 | +- Auto-deploy on every PR (test in `review.yml`, deploy on main/tag only) |
| 97 | +- macOS/Windows matrix on every PR (10× / 2× billable) |
| 98 | +- `cancel-in-progress: false` on deploy when agents push many main commits |
| 99 | +- Bare `docker build && docker push` without GHA cache in monorepos that ship often |
0 commit comments