|
| 1 | +# Analyze GitHub Actions Build |
| 2 | + |
| 3 | +Analyzes a GitHub Actions build to identify failures and their root causes. By default, examines the latest build on the main branch, but you can specify a different run. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +``` |
| 8 | +/analyze-build [options] |
| 9 | +``` |
| 10 | + |
| 11 | +### Options |
| 12 | + |
| 13 | +- `--run-id <id>`: Analyze a specific workflow run by ID |
| 14 | +- `--branch <name>`: Analyze the latest run from a specific branch (default: main) |
| 15 | +- `--workflow <name>`: Specify which workflow to analyze (default: checks all) |
| 16 | +- `--pr <number>`: Analyze the latest run for a specific PR |
| 17 | + |
| 18 | +## Process |
| 19 | + |
| 20 | +1. **Identify Target Build** |
| 21 | + - Default: Latest workflow run on main branch |
| 22 | + - Or use provided run ID, branch, PR, or workflow name |
| 23 | + |
| 24 | +2. **Gather Build Information** |
| 25 | + - Workflow run status and conclusion |
| 26 | + - Failed jobs and steps |
| 27 | + - Error logs and annotations |
| 28 | + - Timing information |
| 29 | + - Related commits and PRs |
| 30 | + |
| 31 | +3. **Analyze Failures** |
| 32 | + - Parse error messages and stack traces |
| 33 | + - Identify failure patterns |
| 34 | + - Categorize error types |
| 35 | + - Determine root causes |
| 36 | + - Check for flaky test indicators |
| 37 | + |
| 38 | +4. **Generate Report** |
| 39 | + - Executive summary |
| 40 | + - Detailed failure analysis |
| 41 | + - Root cause identification |
| 42 | + - Suggested fixes |
| 43 | + - Historical context (if available) |
| 44 | + |
| 45 | +## Analysis Categories |
| 46 | + |
| 47 | +### 1. Test Failures |
| 48 | + |
| 49 | +- Unit test failures |
| 50 | +- Integration test failures |
| 51 | +- E2E test failures |
| 52 | +- Flaky tests |
| 53 | +- Timeout issues |
| 54 | + |
| 55 | +### 2. Build Errors |
| 56 | + |
| 57 | +- Compilation errors |
| 58 | +- TypeScript errors |
| 59 | +- Dependency resolution issues |
| 60 | +- Missing dependencies |
| 61 | +- Version conflicts |
| 62 | + |
| 63 | +### 3. Linting/Formatting |
| 64 | + |
| 65 | +- ESLint violations |
| 66 | +- Prettier formatting issues |
| 67 | +- Commit message format |
| 68 | +- Markdown linting |
| 69 | +- YAML validation |
| 70 | + |
| 71 | +### 4. Infrastructure Issues |
| 72 | + |
| 73 | +- Runner problems |
| 74 | +- Network failures |
| 75 | +- API rate limits |
| 76 | +- Token/permission issues |
| 77 | +- Resource exhaustion |
| 78 | + |
| 79 | +### 5. Deployment Failures |
| 80 | + |
| 81 | +- Docker build issues |
| 82 | +- Registry push failures |
| 83 | +- Security scan violations |
| 84 | +- Release creation problems |
| 85 | + |
| 86 | +## Implementation Steps |
| 87 | + |
| 88 | +```bash |
| 89 | +# 1. Get the target workflow run |
| 90 | +echo "🔍 Identifying target build..." |
| 91 | + |
| 92 | +# Default: latest run on main |
| 93 | +if [ -z "$RUN_ID" ]; then |
| 94 | + if [ -n "$PR_NUMBER" ]; then |
| 95 | + # Get latest run for PR |
| 96 | + RUN_ID=$(gh pr checks $PR_NUMBER --json statusCheckRollup --jq '.statusCheckRollup[0].workflowRun.databaseId') |
| 97 | + elif [ -n "$WORKFLOW_NAME" ]; then |
| 98 | + # Get latest run for specific workflow |
| 99 | + RUN_ID=$(gh run list --workflow="$WORKFLOW_NAME" --branch="${BRANCH:-main}" --limit=1 --json databaseId --jq '.[0].databaseId') |
| 100 | + else |
| 101 | + # Get latest run on branch |
| 102 | + RUN_ID=$(gh run list --branch="${BRANCH:-main}" --limit=1 --json databaseId --jq '.[0].databaseId') |
| 103 | + fi |
| 104 | +fi |
| 105 | + |
| 106 | +# 2. Fetch run details |
| 107 | +echo "📊 Fetching run details for #$RUN_ID..." |
| 108 | +gh run view $RUN_ID --json status,conclusion,name,workflowName,event,headBranch,headSha |
| 109 | + |
| 110 | +# 3. Get failed jobs |
| 111 | +echo "❌ Identifying failed jobs..." |
| 112 | +gh run view $RUN_ID --json jobs --jq '.jobs[] | select(.conclusion == "failure")' |
| 113 | + |
| 114 | +# 4. Fetch logs for failed jobs |
| 115 | +echo "📝 Analyzing failure logs..." |
| 116 | +gh run view $RUN_ID --log-failed |
| 117 | + |
| 118 | +# 5. Get annotations (errors/warnings) |
| 119 | +echo "📌 Checking annotations..." |
| 120 | +gh api /repos/{owner}/{repo}/check-runs --jq '.check_runs[].output.annotations[]' |
| 121 | +``` |
| 122 | + |
| 123 | +## Report Template |
| 124 | + |
| 125 | +```markdown |
| 126 | +# Build Analysis Report |
| 127 | + |
| 128 | +**Run ID**: #[RUN_ID] |
| 129 | +**Workflow**: [WORKFLOW_NAME] |
| 130 | +**Branch**: [BRANCH] |
| 131 | +**Status**: [STATUS] |
| 132 | +**Duration**: [DURATION] |
| 133 | +**Triggered by**: [TRIGGER_EVENT] |
| 134 | + |
| 135 | +## Executive Summary |
| 136 | + |
| 137 | +[High-level summary of the build status and main issues] |
| 138 | + |
| 139 | +## Failure Analysis |
| 140 | + |
| 141 | +### Failed Jobs |
| 142 | + |
| 143 | +1. **[Job Name]** (Duration: [TIME]) |
| 144 | + - Step: [Failed Step] |
| 145 | + - Exit Code: [CODE] |
| 146 | + - Category: [Test/Build/Lint/Infrastructure/Deployment] |
| 147 | + |
| 148 | +### Root Cause Analysis |
| 149 | + |
| 150 | +#### Primary Cause |
| 151 | + |
| 152 | +[Identified primary root cause with evidence] |
| 153 | + |
| 154 | +#### Contributing Factors |
| 155 | + |
| 156 | +- [Factor 1] |
| 157 | +- [Factor 2] |
| 158 | + |
| 159 | +### Error Details |
| 160 | +``` |
| 161 | + |
| 162 | +[Relevant error output/stack trace] |
| 163 | + |
| 164 | +```` |
| 165 | +
|
| 166 | +## Pattern Recognition |
| 167 | +
|
| 168 | +### Similar Historical Failures |
| 169 | +- [Previous occurrence 1] |
| 170 | +- [Previous occurrence 2] |
| 171 | +
|
| 172 | +### Flaky Test Indicators |
| 173 | +- [Test name if applicable] |
| 174 | +- Failure rate: [X/Y runs] |
| 175 | +
|
| 176 | +## Recommended Actions |
| 177 | +
|
| 178 | +### Immediate Fixes |
| 179 | +1. [Specific action to fix the issue] |
| 180 | +2. [Additional required changes] |
| 181 | +
|
| 182 | +### Long-term Improvements |
| 183 | +- [Suggested process improvement] |
| 184 | +- [Infrastructure enhancement] |
| 185 | +
|
| 186 | +## Additional Context |
| 187 | +
|
| 188 | +### Related PRs/Issues |
| 189 | +- PR #[NUMBER]: [Title] |
| 190 | +- Issue #[NUMBER]: [Title] |
| 191 | +
|
| 192 | +### Recent Changes |
| 193 | +- Commit [SHA]: [Message] |
| 194 | +- Files modified: [List of relevant files] |
| 195 | +
|
| 196 | +## Commands to Reproduce/Fix |
| 197 | +
|
| 198 | +```bash |
| 199 | +# To reproduce locally |
| 200 | +[commands] |
| 201 | +
|
| 202 | +# To fix |
| 203 | +[commands] |
| 204 | +
|
| 205 | +# To re-run the workflow |
| 206 | +gh run rerun $RUN_ID |
| 207 | +```` |
| 208 | + |
| 209 | +```` |
| 210 | +
|
| 211 | +## Common Root Causes and Solutions |
| 212 | +
|
| 213 | +### Test Failures |
| 214 | +
|
| 215 | +**Pattern: `Cannot find module`** |
| 216 | +**Root Cause**: Missing dependency or import path issue |
| 217 | +**Solution**: |
| 218 | +```bash |
| 219 | +pnpm install |
| 220 | +# or check import paths in affected files |
| 221 | +```` |
| 222 | + |
| 223 | +**Pattern:** `Timeout of Xms exceeded` |
| 224 | +**Root Cause**: Test timeout, possible performance issue |
| 225 | +**Solution**: |
| 226 | + |
| 227 | +- Increase timeout in test configuration |
| 228 | +- Investigate performance regression |
| 229 | +- Check for missing async/await |
| 230 | + |
| 231 | +### Build Errors |
| 232 | + |
| 233 | +**Pattern:** `Type error TS[NUMBER]` |
| 234 | +**Root Cause**: TypeScript compilation error |
| 235 | +**Solution**: |
| 236 | + |
| 237 | +```bash |
| 238 | +pnpm typecheck |
| 239 | +# Fix type errors locally before pushing |
| 240 | +``` |
| 241 | + |
| 242 | +**Pattern:** `ELIFECYCLE` |
| 243 | +**Root Cause**: Script execution failure |
| 244 | +**Solution**: |
| 245 | + |
| 246 | +- Check script in package.json |
| 247 | +- Verify all dependencies installed |
| 248 | +- Check Node version compatibility |
| 249 | + |
| 250 | +### Linting Issues |
| 251 | + |
| 252 | +**Pattern:** `Unexpected var, use let or const` |
| 253 | +**Root Cause**: ESLint rule violation |
| 254 | +**Solution**: |
| 255 | + |
| 256 | +```bash |
| 257 | +pnpm lint:fix |
| 258 | +``` |
| 259 | + |
| 260 | +**Pattern:** `Trailing spaces` |
| 261 | +**Root Cause**: Formatting issue |
| 262 | +**Solution**: |
| 263 | + |
| 264 | +```bash |
| 265 | +pnpm format:fix |
| 266 | +``` |
| 267 | + |
| 268 | +### Infrastructure Issues |
| 269 | + |
| 270 | +**Pattern:** `Rate limit exceeded` |
| 271 | +**Root Cause**: GitHub API rate limit |
| 272 | +**Solution**: |
| 273 | + |
| 274 | +- Add delays between API calls |
| 275 | +- Use authentication token |
| 276 | +- Implement caching |
| 277 | + |
| 278 | +**Pattern:** `Permission denied` |
| 279 | +**Root Cause**: Token permissions insufficient |
| 280 | +**Solution**: |
| 281 | + |
| 282 | +- Check workflow permissions |
| 283 | +- Update token scopes |
| 284 | +- Verify repository settings |
| 285 | + |
| 286 | +### Security/Vulnerability Issues |
| 287 | + |
| 288 | +**Pattern:** `CRITICAL vulnerability found` |
| 289 | +**Root Cause**: Dependency vulnerability |
| 290 | +**Solution**: |
| 291 | + |
| 292 | +```bash |
| 293 | +# Update vulnerable dependency |
| 294 | +pnpm update [package] |
| 295 | +# or add to .trivyignore if false positive |
| 296 | +``` |
| 297 | + |
| 298 | +## Example Usage |
| 299 | + |
| 300 | +### Default (latest on main) |
| 301 | + |
| 302 | +``` |
| 303 | +/analyze-build |
| 304 | +``` |
| 305 | + |
| 306 | +### Specific run ID |
| 307 | + |
| 308 | +``` |
| 309 | +/analyze-build --run-id 12345678 |
| 310 | +``` |
| 311 | + |
| 312 | +### Latest on a branch |
| 313 | + |
| 314 | +``` |
| 315 | +/analyze-build --branch feature/new-feature |
| 316 | +``` |
| 317 | + |
| 318 | +### For a specific PR |
| 319 | + |
| 320 | +``` |
| 321 | +/analyze-build --pr 123 |
| 322 | +``` |
| 323 | + |
| 324 | +### Specific workflow |
| 325 | + |
| 326 | +``` |
| 327 | +/analyze-build --workflow "CI" |
| 328 | +``` |
| 329 | + |
| 330 | +## Output Example |
| 331 | + |
| 332 | +``` |
| 333 | +🔍 Analyzing build #12345678... |
| 334 | +
|
| 335 | +Build Analysis Report |
| 336 | +=================== |
| 337 | +
|
| 338 | +Run ID: #12345678 |
| 339 | +Workflow: CI |
| 340 | +Branch: main |
| 341 | +Status: failure ❌ |
| 342 | +Duration: 5m 23s |
| 343 | +
|
| 344 | +Executive Summary: |
| 345 | +The build failed due to TypeScript compilation errors introduced in commit abc123. |
| 346 | +Two test files have type mismatches after the recent API client refactoring. |
| 347 | +
|
| 348 | +Root Cause: |
| 349 | +Missing type exports from src/client/base-client.ts causing import failures in test files. |
| 350 | +
|
| 351 | +Immediate Fix: |
| 352 | +1. Add missing exports to src/client/base-client.ts: |
| 353 | + export type { ClientOptions, ClientResponse } |
| 354 | +
|
| 355 | +2. Run locally to verify: |
| 356 | + pnpm typecheck |
| 357 | +
|
| 358 | +Failed Jobs: |
| 359 | +- typecheck (failed after 45s) |
| 360 | + Error: TS2305: Module '"./base-client"' has no exported member 'ClientOptions' |
| 361 | +
|
| 362 | +Recommended Actions: |
| 363 | +✅ Add missing type exports |
| 364 | +✅ Run pnpm typecheck locally before pushing |
| 365 | +📝 Consider adding pre-push hook to catch type errors |
| 366 | +
|
| 367 | +Re-run command: |
| 368 | +gh run rerun 12345678 |
| 369 | +``` |
| 370 | + |
| 371 | +## Notes |
| 372 | + |
| 373 | +- Requires `gh` CLI to be authenticated |
| 374 | +- Works best with detailed CI logs and annotations |
| 375 | +- Can analyze both passed and failed builds |
| 376 | +- Provides actionable recommendations based on failure patterns |
| 377 | +- Learns from historical patterns in the repository |
0 commit comments