|
| 1 | +--- |
| 2 | +name: flow-code-cicd |
| 3 | +description: "Use when setting up, modifying, or troubleshooting CI/CD pipelines, quality gates, or deployment automation" |
| 4 | +--- |
| 5 | + |
| 6 | +# CI/CD Patterns |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +Automate quality gates so no change reaches production without passing verification. CI/CD is the enforcement mechanism for every other skill -- it catches what humans and agents miss, consistently on every change. Gates ordered from fastest to slowest; shift left to catch problems at the cheapest stage. |
| 11 | + |
| 12 | +## When to Use |
| 13 | + |
| 14 | +- Setting up a new project's CI pipeline |
| 15 | +- Adding or modifying quality gates |
| 16 | +- Configuring deployment pipelines or staged rollouts |
| 17 | +- Integrating feature flags for deploy/release separation |
| 18 | +- Debugging CI failures or slow pipelines |
| 19 | +- **Especially when:** no CI exists yet, pipeline exceeds 10 minutes, deploys are manual |
| 20 | + |
| 21 | +**When NOT to use:** |
| 22 | +- One-off manual deployments that won't recur |
| 23 | +- Local-only development with no shared branches |
| 24 | +- For test design details, see `references/testing-patterns.md` |
| 25 | + |
| 26 | +## Core Process |
| 27 | + |
| 28 | +### Phase 1: Design Quality Gate Pipeline |
| 29 | + |
| 30 | +Order gates from fastest to slowest. Every gate that fails stops the pipeline -- no skipping. |
| 31 | + |
| 32 | +``` |
| 33 | +Change Pushed |
| 34 | + | |
| 35 | + v |
| 36 | ++-------------------+ |
| 37 | +| 1. LINT | Seconds. Catches style, unused vars, formatting. |
| 38 | +| 2. TYPE CHECK | Seconds. Catches type errors statically. |
| 39 | +| 3. UNIT TESTS | Seconds-minutes. Catches logic errors. |
| 40 | +| 4. BUILD | Minutes. Catches compilation/bundling errors. |
| 41 | +| 5. INTEGRATION | Minutes. Catches cross-component failures. |
| 42 | +| 6. E2E (optional) | Minutes. Catches user-facing regressions. |
| 43 | +| 7. SECURITY AUDIT | Minutes. Catches vulnerable deps, secrets. |
| 44 | ++-------------------+ |
| 45 | + | |
| 46 | + v |
| 47 | + Ready for review |
| 48 | +``` |
| 49 | + |
| 50 | +**No gate can be skipped.** If lint fails, fix lint -- don't disable the rule. If a test fails, fix the code -- don't skip the test. |
| 51 | + |
| 52 | +### Phase 2: Apply Shift-Left Principle |
| 53 | + |
| 54 | +Catch problems as early as possible. A bug caught in linting costs minutes; the same bug caught in production costs hours. |
| 55 | + |
| 56 | +**Local pre-commit gate** -- run before pushing: |
| 57 | + |
| 58 | +```bash |
| 59 | +$FLOWCTL guard |
| 60 | +``` |
| 61 | + |
| 62 | +This runs all configured guards (lint, type-check, tests) locally before code leaves the developer's machine. CI then re-runs the same checks as a safety net, not the first line of defense. |
| 63 | + |
| 64 | +**Pipeline optimization rules:** |
| 65 | +1. Cheapest checks first (lint before build, unit before integration) |
| 66 | +2. Parallelize independent gates (lint + type-check can run simultaneously) |
| 67 | +3. Cache dependencies aggressively |
| 68 | +4. Use path filters to skip irrelevant jobs (docs-only PRs skip e2e) |
| 69 | + |
| 70 | +### Phase 3: Configure Pipeline |
| 71 | + |
| 72 | +Platform-agnostic principles with a GitHub Actions example: |
| 73 | + |
| 74 | +**Generic pipeline pattern** (adapt to any CI system): |
| 75 | + |
| 76 | +``` |
| 77 | +trigger: pull_request, push to main |
| 78 | +stages: |
| 79 | + - stage: fast-checks (parallel) |
| 80 | + jobs: [lint, type-check] |
| 81 | + - stage: test |
| 82 | + jobs: [unit-test, build] |
| 83 | + depends_on: fast-checks |
| 84 | + - stage: verify |
| 85 | + jobs: [integration, security-audit] |
| 86 | + depends_on: test |
| 87 | + - stage: deploy-staging (auto on main) |
| 88 | + depends_on: verify |
| 89 | + - stage: deploy-production (manual gate or auto after staging) |
| 90 | + depends_on: deploy-staging |
| 91 | +``` |
| 92 | + |
| 93 | +**GitHub Actions example:** |
| 94 | + |
| 95 | +```yaml |
| 96 | +# .github/workflows/ci.yml |
| 97 | +name: CI |
| 98 | +on: |
| 99 | + pull_request: |
| 100 | + branches: [main] |
| 101 | + push: |
| 102 | + branches: [main] |
| 103 | + |
| 104 | +jobs: |
| 105 | + lint: |
| 106 | + runs-on: ubuntu-latest |
| 107 | + steps: |
| 108 | + - uses: actions/checkout@v4 |
| 109 | + - name: Lint |
| 110 | + run: | |
| 111 | + # Language-specific: cargo clippy, npm run lint, ruff check, etc. |
| 112 | + echo "Run your linter here" |
| 113 | +
|
| 114 | + typecheck: |
| 115 | + runs-on: ubuntu-latest |
| 116 | + steps: |
| 117 | + - uses: actions/checkout@v4 |
| 118 | + - name: Type check |
| 119 | + run: | |
| 120 | + # cargo check, npx tsc --noEmit, mypy, etc. |
| 121 | + echo "Run your type checker here" |
| 122 | +
|
| 123 | + test: |
| 124 | + needs: [lint, typecheck] |
| 125 | + runs-on: ubuntu-latest |
| 126 | + steps: |
| 127 | + - uses: actions/checkout@v4 |
| 128 | + - name: Unit tests |
| 129 | + run: | |
| 130 | + # cargo test, npm test, pytest, go test, etc. |
| 131 | + echo "Run your tests here" |
| 132 | + - name: Build |
| 133 | + run: | |
| 134 | + # cargo build --release, npm run build, go build, etc. |
| 135 | + echo "Run your build here" |
| 136 | +
|
| 137 | + security: |
| 138 | + needs: [test] |
| 139 | + runs-on: ubuntu-latest |
| 140 | + steps: |
| 141 | + - uses: actions/checkout@v4 |
| 142 | + - name: Security audit |
| 143 | + run: | |
| 144 | + # cargo audit, npm audit, pip-audit, govulncheck |
| 145 | + echo "Run your security audit here" |
| 146 | +``` |
| 147 | +
|
| 148 | +See `references/security-checklist.md` for the full security verification checklist including dependency auditing, secret scanning, and OWASP Top 10. |
| 149 | + |
| 150 | +### Phase 4: Configure Feature Flags |
| 151 | + |
| 152 | +Feature flags decouple deployment from release. Deploy code without enabling it. |
| 153 | + |
| 154 | +``` |
| 155 | +Feature flag lifecycle: |
| 156 | + Create flag → Enable for testing → Canary (1%) → Staged rollout → Full rollout → Remove flag |
| 157 | + |
| 158 | +Flag rules: |
| 159 | + - Every flag gets a cleanup date at creation |
| 160 | + - Flags older than 30 days without rollout trigger review |
| 161 | + - Dead flags (fully rolled out) are tech debt -- remove them |
| 162 | +``` |
| 163 | +
|
| 164 | +**Why flags matter:** |
| 165 | +- Ship code to main without enabling it (reduces branch lifetime) |
| 166 | +- Roll back without redeploying (disable the flag) |
| 167 | +- Canary new features safely (1% of users first) |
| 168 | +- A/B test behavior differences |
| 169 | +
|
| 170 | +### Phase 5: Set Up Staged Rollouts |
| 171 | +
|
| 172 | +Never deploy to 100% of users at once. |
| 173 | +
|
| 174 | +``` |
| 175 | +Staged rollout: |
| 176 | + 1% → Monitor 15 min → errors? → Rollback |
| 177 | + 10% → Monitor 30 min → errors? → Rollback |
| 178 | + 50% → Monitor 1 hour → errors? → Rollback |
| 179 | + 100% → Continue monitoring |
| 180 | +``` |
| 181 | +
|
| 182 | +**Rollback requirements:** |
| 183 | +- Every deployment must be reversible within 5 minutes |
| 184 | +- Rollback procedure documented and tested (not just "we'll figure it out") |
| 185 | +- Rollback does not require a new build (revert to previous artifact) |
| 186 | +
|
| 187 | +### Phase 6: Add Monitoring and Alerting |
| 188 | +
|
| 189 | +Deployment without monitoring is flying blind. |
| 190 | +
|
| 191 | +``` |
| 192 | +Post-deploy checklist: |
| 193 | + [ ] Error rate monitoring active |
| 194 | + [ ] Latency monitoring active |
| 195 | + [ ] Key business metrics dashboarded |
| 196 | + [ ] Alerting configured for anomalies |
| 197 | + [ ] Runbook exists for common failure modes |
| 198 | +``` |
| 199 | +
|
| 200 | +**CI pipeline health:** |
| 201 | +- Track pipeline duration over time -- if it grows past 10 minutes, optimize |
| 202 | +- Monitor flaky test rate -- flaky tests erode trust in CI |
| 203 | +- Alert on pipeline failures in main branch (broken main = emergency) |
| 204 | +
|
| 205 | +## Common Rationalizations |
| 206 | +
|
| 207 | +| Excuse | Reality | |
| 208 | +|--------|---------| |
| 209 | +| "CI is too slow" | Slow CI means wrong gate order. Fastest checks first. Parallelize. Cache. A 5-minute pipeline prevents hours of debugging. | |
| 210 | +| "We'll add tests to CI later" | CI without tests is just a build server. Tests are the point of CI. | |
| 211 | +| "Feature flags add complexity" | Deploying untested code to everyone adds more complexity. Flags give you a kill switch. | |
| 212 | +| "We don't need staging" | Staging catches issues that local environments can't reproduce. Network, data volume, concurrency -- staging surfaces them. | |
| 213 | +| "Manual deploy is fine for now" | Manual processes are forgotten, skipped, and inconsistent. Automate on day one. | |
| 214 | +| "This change is trivial, skip CI" | Trivial changes break builds. CI is fast for trivial changes anyway. | |
| 215 | +| "The test is flaky, just re-run" | Flaky tests mask real bugs and waste everyone's time. Fix the flakiness. See `references/testing-patterns.md` for test isolation patterns. | |
| 216 | +| "We'll figure out rollback if we need it" | You need it. Plan rollback before the first deploy, not during an outage. | |
| 217 | +
|
| 218 | +## Red Flags |
| 219 | +
|
| 220 | +- No CI pipeline in the project -- every shared project needs automated verification |
| 221 | +- CI failures ignored or silenced ("it's probably flaky") |
| 222 | +- Tests disabled in CI to make the pipeline pass |
| 223 | +- Production deploys without staging verification |
| 224 | +- No rollback mechanism -- deploying is a one-way door |
| 225 | +- Secrets hardcoded in CI config files instead of a secrets manager |
| 226 | +- Pipeline duration growing unchecked past 10 minutes |
| 227 | +- Feature flags with no cleanup dates accumulating as dead code |
| 228 | +- Main branch broken for hours with no one investigating |
| 229 | +
|
| 230 | +## Verification |
| 231 | +
|
| 232 | +After setting up or modifying a CI/CD pipeline, confirm: |
| 233 | +
|
| 234 | +- [ ] All quality gates present and ordered fastest-to-slowest (lint, types, tests, build, integration, security) |
| 235 | +- [ ] `$FLOWCTL guard` configured as local pre-commit gate |
| 236 | +- [ ] Pipeline runs on every PR and push to main |
| 237 | +- [ ] Failures block merge (branch protection or equivalent configured) |
| 238 | +- [ ] Secrets stored in secrets manager, not in code or CI config (see `references/security-checklist.md`) |
| 239 | +- [ ] Deployment has a tested rollback mechanism |
| 240 | +- [ ] Pipeline completes in under 10 minutes for the standard test suite |
| 241 | +- [ ] Feature flags have documented cleanup dates |
| 242 | +- [ ] Post-deploy monitoring and alerting are active |
| 243 | +
|
| 244 | +For performance gates in CI, see the `flow-code-performance` skill. |
| 245 | +For test design and patterns, see `references/testing-patterns.md`. |
0 commit comments