|
| 1 | +# GitHub Actions Automation Patterns |
| 2 | + |
| 3 | +**Pattern**: Proactive CI/CD workflows that automate routine development tasks. |
| 4 | + |
| 5 | +**Problem**: Manual processes slow down development - reviewing PRs, creating PRs from issues, merging dependency updates, cleaning up stale issues. Teams spend time on toil instead of building. |
| 6 | + |
| 7 | +**Solution**: GitHub Actions workflows that trigger on repository events and handle routine work automatically. AI reviews code, issues become PRs, safe updates auto-merge, stale issues get cleaned up. |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +## Issue-to-PR Automation |
| 12 | + |
| 13 | +Convert well-defined issues into draft pull requests automatically. |
| 14 | + |
| 15 | +### How It Works |
| 16 | + |
| 17 | +```mermaid |
| 18 | +flowchart TD |
| 19 | + A[Issue Opened] --> B{Well-defined?} |
| 20 | + B -->|No| C[Request Clarification] |
| 21 | + B -->|Yes| D[Analyze Issue] |
| 22 | + D --> E[Self-Review Analysis] |
| 23 | + E --> F[Create Branch] |
| 24 | + F --> G[Create Draft PR] |
| 25 | + G --> H[Link to Issue] |
| 26 | +``` |
| 27 | + |
| 28 | +### Implementation |
| 29 | + |
| 30 | +```yaml |
| 31 | +# .github/workflows/issue-to-pr.yml |
| 32 | +name: Issue to Draft PR |
| 33 | + |
| 34 | +on: |
| 35 | + issues: |
| 36 | + types: [opened, labeled] |
| 37 | + |
| 38 | +permissions: |
| 39 | + contents: write |
| 40 | + pull-requests: write |
| 41 | + issues: write |
| 42 | + |
| 43 | +jobs: |
| 44 | + create-pr: |
| 45 | + if: contains(github.event.label.name, 'ready-for-pr') || github.event.action == 'opened' |
| 46 | + runs-on: ubuntu-latest |
| 47 | + steps: |
| 48 | + - uses: actions/checkout@v4 |
| 49 | + |
| 50 | + - name: Analyze issue |
| 51 | + id: analyze |
| 52 | + uses: anthropics/claude-code-action@v1 |
| 53 | + with: |
| 54 | + anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }} |
| 55 | + prompt: | |
| 56 | + Analyze issue #${{ github.event.issue.number }}: ${{ github.event.issue.title }} |
| 57 | +
|
| 58 | + Issue body: |
| 59 | + ${{ github.event.issue.body }} |
| 60 | +
|
| 61 | + Determine if this issue is actionable: |
| 62 | + 1. Are requirements clear? |
| 63 | + 2. Is scope well-defined? |
| 64 | + 3. Any security concerns? |
| 65 | +
|
| 66 | + Write decision to analysis-decision.txt: |
| 67 | + - "create_pr" if ready for implementation |
| 68 | + - "needs_clarification" if requirements unclear |
| 69 | +
|
| 70 | + Write reasoning to analysis-reasoning.txt (2-3 lines). |
| 71 | +
|
| 72 | + - name: Check decision |
| 73 | + id: check |
| 74 | + run: | |
| 75 | + if [ -f analysis-decision.txt ]; then |
| 76 | + echo "decision=$(cat analysis-decision.txt)" >> $GITHUB_OUTPUT |
| 77 | + fi |
| 78 | +
|
| 79 | + - name: Create draft PR |
| 80 | + if: steps.check.outputs.decision == 'create_pr' |
| 81 | + env: |
| 82 | + GH_TOKEN: ${{ github.token }} |
| 83 | + run: | |
| 84 | + ISSUE_NUM="${{ github.event.issue.number }}" |
| 85 | + BRANCH="feat/issue-${ISSUE_NUM}" |
| 86 | +
|
| 87 | + git checkout -b "$BRANCH" |
| 88 | + git push origin "$BRANCH" |
| 89 | +
|
| 90 | + gh pr create \ |
| 91 | + --draft \ |
| 92 | + --title "[Draft] Issue #${ISSUE_NUM}: ${{ github.event.issue.title }}" \ |
| 93 | + --body "Auto-generated from issue #${ISSUE_NUM}. See issue for requirements." |
| 94 | +``` |
| 95 | +
|
| 96 | +--- |
| 97 | +
|
| 98 | +## PR Auto-Review |
| 99 | +
|
| 100 | +AI-powered code review on every pull request. |
| 101 | +
|
| 102 | +### Workflow |
| 103 | +
|
| 104 | +```mermaid |
| 105 | +flowchart TD |
| 106 | + A[PR Opened/Updated] --> B[AI Reviews Code] |
| 107 | + B --> C[Self-Review Findings] |
| 108 | + C --> D[Post Review Comment] |
| 109 | +``` |
| 110 | + |
| 111 | +### Workflow YAML |
| 112 | + |
| 113 | +```yaml |
| 114 | +# .github/workflows/pr-review.yml |
| 115 | +name: PR Auto-Review |
| 116 | + |
| 117 | +on: |
| 118 | + pull_request: |
| 119 | + types: [opened, synchronize] |
| 120 | + |
| 121 | +permissions: |
| 122 | + contents: read |
| 123 | + pull-requests: write |
| 124 | + |
| 125 | +jobs: |
| 126 | + review: |
| 127 | + runs-on: ubuntu-latest |
| 128 | + steps: |
| 129 | + - uses: actions/checkout@v4 |
| 130 | + |
| 131 | + - name: Review code |
| 132 | + uses: anthropics/claude-code-action@v1 |
| 133 | + with: |
| 134 | + anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }} |
| 135 | + prompt: | |
| 136 | + Review pull request #${{ github.event.pull_request.number }}. |
| 137 | +
|
| 138 | + Focus on: |
| 139 | + - Security issues (input validation, injection, secrets) |
| 140 | + - Bug risks (edge cases, error handling) |
| 141 | + - Code quality (clarity, maintainability) |
| 142 | +
|
| 143 | + Post a review comment with findings. Use format: |
| 144 | + 🔴 CRITICAL: [issue] - must fix |
| 145 | + 🟡 WARNING: [issue] - should consider |
| 146 | + ✅ GOOD: [positive observation] |
| 147 | +
|
| 148 | + Be concise. Only flag issues you're confident about. |
| 149 | +``` |
| 150 | +
|
| 151 | +--- |
| 152 | +
|
| 153 | +## Dependabot Auto-Merge |
| 154 | +
|
| 155 | +Automatically merge low-risk dependency updates. |
| 156 | +
|
| 157 | +### Flow |
| 158 | +
|
| 159 | +```mermaid |
| 160 | +flowchart TD |
| 161 | + A[Dependabot PR] --> B{Patch Version?} |
| 162 | + B -->|No| C[Require Human Review] |
| 163 | + B -->|Yes| D{CI Passes?} |
| 164 | + D -->|No| C |
| 165 | + D -->|Yes| E[Auto-Merge] |
| 166 | +``` |
| 167 | + |
| 168 | +### Auto-Merge YAML |
| 169 | + |
| 170 | +```yaml |
| 171 | +# .github/workflows/dependabot-auto-merge.yml |
| 172 | +name: Dependabot Auto-Merge |
| 173 | + |
| 174 | +on: |
| 175 | + pull_request: |
| 176 | + types: [opened, synchronize] |
| 177 | + |
| 178 | +permissions: |
| 179 | + contents: write |
| 180 | + pull-requests: write |
| 181 | + |
| 182 | +jobs: |
| 183 | + auto-merge: |
| 184 | + if: github.actor == 'dependabot[bot]' |
| 185 | + runs-on: ubuntu-latest |
| 186 | + steps: |
| 187 | + - name: Get Dependabot metadata |
| 188 | + id: metadata |
| 189 | + uses: dependabot/fetch-metadata@v2 |
| 190 | + with: |
| 191 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 192 | + |
| 193 | + - name: Auto-merge patch updates |
| 194 | + if: steps.metadata.outputs.update-type == 'version-update:semver-patch' |
| 195 | + env: |
| 196 | + GH_TOKEN: ${{ github.token }} |
| 197 | + run: | |
| 198 | + gh pr merge "${{ github.event.pull_request.number }}" \ |
| 199 | + --auto \ |
| 200 | + --squash \ |
| 201 | + --delete-branch |
| 202 | +``` |
| 203 | +
|
| 204 | +### Safety Conditions |
| 205 | +
|
| 206 | +Only auto-merge when ALL conditions met: |
| 207 | +
|
| 208 | +- ✅ PR author is `dependabot[bot]` |
| 209 | +- ✅ Update is patch version (x.x.PATCH) |
| 210 | +- ✅ All CI checks pass |
| 211 | +- ✅ No merge conflicts |
| 212 | + |
| 213 | +For minor/major updates: require human review. |
| 214 | + |
| 215 | +--- |
| 216 | + |
| 217 | +## Stale Issue Management |
| 218 | + |
| 219 | +Clean up inactive issues automatically. |
| 220 | + |
| 221 | +### Process |
| 222 | + |
| 223 | +```mermaid |
| 224 | +flowchart TD |
| 225 | + A[Weekly Schedule] --> B[Find Inactive Issues] |
| 226 | + B --> C[Add Stale Label] |
| 227 | + C --> D[Wait 7 Days] |
| 228 | + D --> E{Activity?} |
| 229 | + E -->|Yes| F[Remove Stale Label] |
| 230 | + E -->|No| G[Close Issue] |
| 231 | +``` |
| 232 | + |
| 233 | +### Stale YAML |
| 234 | + |
| 235 | +```yaml |
| 236 | +# .github/workflows/stale-issues.yml |
| 237 | +name: Stale Issue Management |
| 238 | +
|
| 239 | +on: |
| 240 | + schedule: |
| 241 | + - cron: '0 0 * * 0' # Weekly on Sunday midnight UTC |
| 242 | + workflow_dispatch: # Manual trigger |
| 243 | +
|
| 244 | +permissions: |
| 245 | + issues: write |
| 246 | + pull-requests: write |
| 247 | +
|
| 248 | +jobs: |
| 249 | + stale: |
| 250 | + runs-on: ubuntu-latest |
| 251 | + steps: |
| 252 | + - uses: actions/stale@v9 |
| 253 | + with: |
| 254 | + stale-issue-message: | |
| 255 | + This issue has been inactive for 30 days. |
| 256 | + It will be closed in 7 days if there's no activity. |
| 257 | + Comment to keep it open. |
| 258 | + stale-pr-message: | |
| 259 | + This PR has been inactive for 14 days. |
| 260 | + It will be closed in 7 days if there's no activity. |
| 261 | + days-before-stale: 30 |
| 262 | + days-before-close: 7 |
| 263 | + stale-issue-label: 'stale' |
| 264 | + stale-pr-label: 'stale' |
| 265 | + exempt-issue-labels: 'pinned,security,bug' |
| 266 | + exempt-pr-labels: 'pinned,security' |
| 267 | +``` |
| 268 | + |
| 269 | +--- |
| 270 | + |
| 271 | +## Required Secrets |
| 272 | + |
| 273 | +| Secret | Used By | Purpose | |
| 274 | +|--------|---------|---------| |
| 275 | +| `ANTHROPIC_API_KEY` | Issue-to-PR, PR Review | AI analysis | |
| 276 | +| `GITHUB_TOKEN` | All workflows | Repository access (auto-provided) | |
| 277 | + |
| 278 | +--- |
| 279 | + |
| 280 | +## Related Patterns |
| 281 | + |
| 282 | +- [Self-Review Reflection](self-review-reflection.md) - Agent self-review before presenting work |
| 283 | +- [Autonomous Quality Enforcement](autonomous-quality-enforcement.md) - Validation loops |
0 commit comments