1
---
2
name: ci-cd-and-automation
3
description: Automates CI/CD pipeline setup. Use when setting up or modifying build and deployment pipelines. Use when you need to automate quality gates, configure test runners in CI, or establish deployment strategies.
4
---
5
6
# CI/CD and Automation
7
8
## Overview
9
10
Automate quality gates so that no change reaches production without passing tests, lint, type checking, and build. CI/CD is the enforcement mechanism for every other skill — it catches what humans and agents miss, and it does so consistently on every single change.
11
12
**Shift Left:** Catch problems as early in the pipeline as possible. A bug caught in linting costs minutes; the same bug caught in production costs hours. Move checks upstream — static analysis before tests, tests before staging, staging before production.
13
14
**Faster is Safer:** Smaller batches and more frequent releases reduce risk, not increase it. A deployment with 3 changes is easier to debug than one with 30. Frequent releases build confidence in the release process itself.
15
16
## When to Use
17
18
- Setting up a new project's CI pipeline
19
- Adding or modifying automated checks
20
- Configuring deployment pipelines
21
- When a change should trigger automated verification
22
- Debugging CI failures
23
24
## The Quality Gate Pipeline
25
26
Every change goes through these gates before merge:
27
28
```
29
Pull Request Opened
30
│
31
▼
32
┌─────────────────┐
33
│ LINT CHECK │ eslint, prettier
34
│ ↓ pass │
35
│ TYPE CHECK │ tsc --noEmit
36
│ ↓ pass │
37
│ UNIT TESTS │ jest/vitest
38
│ ↓ pass │
39
│ BUILD │ npm run build
40
│ ↓ pass │
41
│ INTEGRATION │ API/DB tests
42
│ ↓ pass │
43
│ E2E (optional) │ Playwright/Cypress
44
│ ↓ pass │
45
│ SECURITY AUDIT │ npm audit
46
│ ↓ pass │
47
│ BUNDLE SIZE │ bundlesize check
48
└─────────────────┘
49
│
50
▼
51
Ready for review
52
```
53
54
**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.
55
56
## GitHub Actions Configuration
57
58
### Basic CI Pipeline
59
60
```yaml
61
# .github/workflows/ci.yml
62
name: CI
63
64
on:
65
pull_request:
66
branches: [main]
67
push:
68
branches: [main]
69
70
jobs:
71
quality:
72
runs-on: ubuntu-latest
73
steps:
74
- uses: actions/checkout@v6
75
76
- uses: actions/setup-node@v6
77
with:
78
node-version: '22'
79
cache: 'npm'
80
81
- name: Install dependencies
82
run: npm ci
83
84
- name: Lint
85
run: npm run lint
86
87
- name: Type check
88
run: npx tsc --noEmit
89
90
- name: Test
91
run: npm test -- --coverage
92
93
- name: Build
94
run: npm run build
95
96
- name: Security audit
97
run: npm audit --audit-level=high
98
```
99
100
### With Database Integration Tests
101
102
```yaml
103
integration:
104
runs-on: ubuntu-latest
105
services:
106
postgres:
107
image: postgres:16
108
env:
109
POSTGRES_DB: testdb
110
POSTGRES_USER: ci_user
111
POSTGRES_PASSWORD: ${{ secrets.CI_DB_PASSWORD }}
112
ports:
113
- 5432:5432
114
options: >-
115
--health-cmd pg_isready
116
--health-interval 10s
117
--health-timeout 5s
118
--health-retries 5
119
120
steps:
121
- uses: actions/checkout@v6
122
- uses: actions/setup-node@v6
123
with:
124
node-version: '22'
125
cache: 'npm'
126
- run: npm ci
127
- name: Run migrations
128
run: npx prisma migrate deploy
129
env:
130
DATABASE_URL: postgresql://ci_user:${{ secrets.CI_DB_PASSWORD }}@localhost:5432/testdb
131
- name: Integration tests
132
run: npm run test:integration
133
env:
134
DATABASE_URL: postgresql://ci_user:${{ secrets.CI_DB_PASSWORD }}@localhost:5432/testdb
135
```
136
137
> **Note:** Even for CI-only test databases, use GitHub Secrets for credentials rather than hardcoding values. This builds good habits and prevents accidental reuse of test credentials in other contexts.
138
139
### E2E Tests
140
141
```yaml
142
e2e:
143
runs-on: ubuntu-latest
144
steps:
145
- uses: actions/checkout@v6
146
- uses: actions/setup-node@v6
147
with:
148
node-version: '22'
149
cache: 'npm'
150
- run: npm ci
151
- name: Install Playwright
152
run: npx playwright install --with-deps chromium
153
- name: Build
154
run: npm run build
155
- name: Run E2E tests
156
run: npx playwright test
157
- uses: actions/upload-artifact@v6
158
if: failure()
159
with:
160
name: playwright-report
161
path: playwright-report/
162
```
163
164
## Feeding CI Failures Back to Agents
165
166
The power of CI with AI agents is the feedback loop. When CI fails:
167
168
```
169
CI fails
170
│
171
▼
172
Copy the failure output
173
│
174
▼
175
Feed it to the agent:
176
"The CI pipeline failed with this error:
177
[paste specific error]
178
Fix the issue and verify locally before pushing again."
179
│
180
▼
181
Agent fixes → pushes → CI runs again
182
```
183
184
**Key patterns:**
185
186
```
187
Lint failure → Agent runs `npm run lint -- --fix` and commits
188
Type error → Agent reads the error location and fixes the type
189
Test failure → Agent follows debugging-and-error-recovery skill
190
Build error → Agent checks config and dependencies
191
```
192
193
## Deployment Strategies
194
195
### Preview Deployments
196
197
Every PR gets a preview deployment for manual testing:
198
199
```yaml
200
# Deploy preview on PR (Vercel/Netlify/etc.)
201
deploy-preview:
202
runs-on: ubuntu-latest
203
if: github.event_name == 'pull_request'
204
steps:
205
- uses: actions/checkout@v6
206
- name: Deploy preview
207
run: npx vercel --token=${{ secrets.VERCEL_TOKEN }}
208
```
209
210
### Feature Flags
211
212
Feature flags decouple deployment from release. Deploy incomplete or risky features behind flags so you can:
213
214
- **Ship code without enabling it.** Merge to main early, enable when ready.
215
- **Roll back without redeploying.** Disable the flag instead of reverting code.
216
- **Canary new features.** Enable for 1% of users, then 10%, then 100%.
217
- **Run A/B tests.** Compare behavior with and without the feature.
218
219
```typescript
220
// Simple feature flag pattern
221
if (featureFlags.isEnabled('new-checkout-flow', { userId })) {
222
return renderNewCheckout();
223
}
224
return renderLegacyCheckout();
225
```
226
227
**Flag lifecycle:** Create → Enable for testing → Canary → Full rollout → Remove the flag and dead code. Flags that live forever become technical debt — set a cleanup date when you create them.
228
229
### Staged Rollouts
230
231
```
232
PR merged to main
233
│
234
▼
235
Staging deployment (auto)
236
│ Manual verification
237
▼
238
Production deployment (manual trigger or auto after staging)
239
│
240
▼
241
Monitor for errors (15-minute window)
242
│
243
├── Errors detected → Rollback
244
└── Clean → Done
245
```
246
247
### Rollback Plan
248
249
Every deployment should be reversible:
250
251
```yaml
252
# Manual rollback workflow
253
name: Rollback
254
on:
255
workflow_dispatch:
256
inputs:
257
version:
258
description: 'Version to rollback to'
259
required: true
260
261
jobs:
262
rollback:
263
runs-on: ubuntu-latest
264
steps:
265
- name: Rollback deployment
266
env:
267
VERSION: ${{ inputs.version }}
268
VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
269
run: |
270
# Deploy the specified previous version. Pass the input through env,
271
# never interpolate it into the script (shell injection). The runner
272
# has no Vercel login, so the CLI needs the token explicitly.
273
npx vercel rollback "$VERSION" --token "$VERCEL_TOKEN" --yes
274
```
275
276
## Environment Management
277
278
```
279
.env.example → Committed (template for developers)
280
.env → NOT committed (local development)
281
.env.test → Committed (test environment, no real secrets)
282
CI secrets → Stored in GitHub Secrets / vault
283
Production secrets → Stored in deployment platform / vault
284
```
285
286
CI should never have production secrets. Use separate secrets for CI testing.
287
288
## Automation Beyond CI
289
290
### Dependabot / Renovate
291
292
```yaml
293
# .github/dependabot.yml
294
version: 2
295
updates:
296
- package-ecosystem: npm
297
directory: /
298
schedule:
299
interval: weekly
300
open-pull-requests-limit: 5
301
```
302
303
### Build Cop Role
304
305
Designate someone responsible for keeping CI green. When the build breaks, the Build Cop's job is to fix or revert — not the person whose change caused the break. This prevents broken builds from accumulating while everyone assumes someone else will fix it.
306
307
### PR Checks
308
309
- **Required reviews:** At least 1 approval before merge
310
- **Required status checks:** CI must pass before merge
311
- **Branch protection:** No force-pushes to main
312
- **Auto-merge:** If all checks pass and approved, merge automatically
313
314
## CI Optimization
315
316
When the pipeline exceeds 10 minutes, apply these strategies in order of impact:
317
318
```
319
Slow CI pipeline?
320
├── Cache dependencies
321
│ └── Use the setup-node cache option (or actions/cache) for the package manager cache, not node_modules
322
├── Run jobs in parallel
323
│ └── Split lint, typecheck, test, build into separate parallel jobs
324
├── Only run what changed
325
│ └── Use path filters to skip unrelated jobs (e.g., skip e2e for docs-only PRs)
326
├── Use matrix builds
327
│ └── Shard test suites across multiple runners
328
├── Optimize the test suite
329
│ └── Remove slow tests from the critical path, run them on a schedule instead
330
└── Use larger runners
331
└── GitHub-hosted larger runners or self-hosted for CPU-heavy builds
332
```
333
334
**Example: caching and parallelism**
335
```yaml
336
jobs:
337
lint:
338
runs-on: ubuntu-latest
339
steps:
340
- uses: actions/checkout@v6
341
- uses: actions/setup-node@v6
342
with: { node-version: '22', cache: 'npm' }
343
- run: npm ci
344
- run: npm run lint
345
346
typecheck:
347
runs-on: ubuntu-latest
348
steps:
349
- uses: actions/checkout@v6
350
- uses: actions/setup-node@v6
351
with: { node-version: '22', cache: 'npm' }
352
- run: npm ci
353
- run: npx tsc --noEmit
354
355
test:
356
runs-on: ubuntu-latest
357
steps:
358
- uses: actions/checkout@v6
359
- uses: actions/setup-node@v6
360
with: { node-version: '22', cache: 'npm' }
361
- run: npm ci
362
- run: npm test -- --coverage
363
```
364
365
## Common Rationalizations
366
367
| Rationalization | Reality |
368
|---|---|
369
| "CI is too slow" | Optimize the pipeline (see CI Optimization below), don't skip it. A 5-minute pipeline prevents hours of debugging. |
370
| "This change is trivial, skip CI" | Trivial changes break builds. CI is fast for trivial changes anyway. |
371
| "The test is flaky, just re-run" | Flaky tests mask real bugs and waste everyone's time. Fix the flakiness. |
372
| "We'll add CI later" | Projects without CI accumulate broken states. Set it up on day one. |
373
| "Manual testing is enough" | Manual testing doesn't scale and isn't repeatable. Automate what you can. |
374
375
## Red Flags
376
377
- No CI pipeline in the project
378
- CI failures ignored or silenced
379
- Tests disabled in CI to make the pipeline pass
380
- Production deploys without staging verification
381
- No rollback mechanism
382
- Secrets stored in code or CI config files (not secrets manager)
383
- Long CI times with no optimization effort
384
385
## Verification
386
387
After setting up or modifying CI:
388
389
- [ ] All quality gates are present (lint, types, tests, build, audit)
390
- [ ] Pipeline runs on every PR and push to main
391
- [ ] Failures block merge (branch protection configured)
392
- [ ] CI results feed back into the development loop
393
- [ ] Secrets are stored in the secrets manager, not in code
394
- [ ] Deployment has a rollback mechanism
395
- [ ] Pipeline runs in under 10 minutes for the test suite
396