|
| 1 | +--- |
| 2 | +# ===================================================================== |
| 3 | +# Claude Automatic PR Build Fix Workflow |
| 4 | +# ===================================================================== |
| 5 | +# |
| 6 | +# Automatically attempts to fix PR build failures using Claude. |
| 7 | +# Only runs on pull requests, not direct pushes to main. |
| 8 | +# |
| 9 | +# How it works: |
| 10 | +# 1. Triggers when a PR is opened or updated |
| 11 | +# 2. Waits for the Build CryptoAI workflow to complete |
| 12 | +# 3. If it fails, fetches the failure logs |
| 13 | +# 4. Provides logs to Claude with explicit fix instructions |
| 14 | +# 5. Claude analyzes, fixes, and commits directly to the PR branch |
| 15 | +# 6. Pushes the fixes, which triggers a new build |
| 16 | +# |
| 17 | +# With pull_request trigger, GITHUB_TOKEN has write permissions and can |
| 18 | +# push directly to the PR branch. No Personal Access Token needed! |
| 19 | +# |
| 20 | +# For main branch failures: Trigger manually with workflow_dispatch |
| 21 | +# |
| 22 | +# ===================================================================== |
| 23 | + |
| 24 | +name: 🤖 Claude Fix PR Build |
| 25 | + |
| 26 | +on: |
| 27 | + pull_request: |
| 28 | + types: [opened, synchronize] |
| 29 | + workflow_dispatch: |
| 30 | + |
| 31 | +permissions: |
| 32 | + contents: write |
| 33 | + pull-requests: write |
| 34 | + actions: read |
| 35 | + id-token: write |
| 36 | + |
| 37 | +jobs: |
| 38 | + fix-pr-build: |
| 39 | + name: 🔧 Fix PR build |
| 40 | + runs-on: ubuntu-latest |
| 41 | + timeout-minutes: 15 |
| 42 | + |
| 43 | + steps: |
| 44 | + - name: 📥 Checkout PR branch |
| 45 | + uses: actions/checkout@v4 |
| 46 | + with: |
| 47 | + ref: ${{ github.head_ref }} |
| 48 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 49 | + |
| 50 | + - name: ⏳ Wait for build to complete |
| 51 | + id: wait-build |
| 52 | + env: |
| 53 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 54 | + HEAD_REF: ${{ github.head_ref }} |
| 55 | + HEAD_SHA: ${{ github.event.pull_request.head.sha }} |
| 56 | + run: | |
| 57 | + echo "⏳ Waiting for Build CryptoAI workflow to complete..." |
| 58 | + sleep 10 |
| 59 | +
|
| 60 | + # Wait for the build workflow run for this commit |
| 61 | + for i in {1..30}; do |
| 62 | + RUN_ID=$(gh run list \ |
| 63 | + --workflow "Build CryptoAI 🚀" \ |
| 64 | + --branch "$HEAD_REF" \ |
| 65 | + --json databaseId,status,conclusion,headSha \ |
| 66 | + --jq ".[] | select(.headSha == \"$HEAD_SHA\") | .databaseId" \ |
| 67 | + | head -1) |
| 68 | +
|
| 69 | + if [ -n "$RUN_ID" ]; then |
| 70 | + STATUS=$(gh run view "$RUN_ID" --json status,conclusion --jq '.status') |
| 71 | + if [ "$STATUS" = "completed" ]; then |
| 72 | + CONCLUSION=$(gh run view "$RUN_ID" --json conclusion --jq -r '.conclusion') |
| 73 | + echo "build_run_id=$RUN_ID" >> "$GITHUB_OUTPUT" |
| 74 | + echo "build_conclusion=$CONCLUSION" >> "$GITHUB_OUTPUT" |
| 75 | + echo "✅ Build completed with conclusion: $CONCLUSION" |
| 76 | + break |
| 77 | + fi |
| 78 | + fi |
| 79 | +
|
| 80 | + echo "Still waiting... (attempt $i/30)" |
| 81 | + sleep 10 |
| 82 | + done |
| 83 | +
|
| 84 | + - name: 🔍 Fetch failure logs |
| 85 | + if: steps.wait-build.outputs.build_conclusion == 'failure' |
| 86 | + id: get-logs |
| 87 | + env: |
| 88 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 89 | + RUN_ID: ${{ steps.wait-build.outputs.build_run_id }} |
| 90 | + run: | |
| 91 | + echo "📊 Fetching failure logs from run $RUN_ID" |
| 92 | + gh run view "$RUN_ID" --log-failed > /tmp/failure-logs.txt || true |
| 93 | +
|
| 94 | + if [ -s /tmp/failure-logs.txt ]; then |
| 95 | + LINES=$(wc -l < /tmp/failure-logs.txt) |
| 96 | + echo "✅ Saved failure logs to /tmp/failure-logs.txt ($LINES lines)" |
| 97 | + else |
| 98 | + echo "⚠️ No failure logs retrieved" |
| 99 | + fi |
| 100 | +
|
| 101 | + - name: ⚙️ Configure git |
| 102 | + if: steps.wait-build.outputs.build_conclusion == 'failure' |
| 103 | + run: | |
| 104 | + git config --global user.name "github-actions[bot]" |
| 105 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 106 | +
|
| 107 | + - name: 🤖 Run Claude to fix the build |
| 108 | + if: steps.wait-build.outputs.build_conclusion == 'failure' |
| 109 | + id: claude-fix |
| 110 | + uses: anthropics/claude-code-action@v1 |
| 111 | + with: |
| 112 | + claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }} |
| 113 | + prompt: | |
| 114 | + The build failed on this PR. Fix the issues and commit your changes. |
| 115 | +
|
| 116 | + Failure logs are in /tmp/failure-logs.txt |
| 117 | +
|
| 118 | + Follow .cursor/rules/git-commit-message.mdc for commit message format. |
| 119 | + Follow .cursor/rules/autonomous-development-workflow.mdc for the validation workflow. |
| 120 | +
|
| 121 | + The workflow will push your commit directly to this PR branch. |
| 122 | +
|
| 123 | + claude_args: |
| 124 | + '--permission-mode bypassPermissions --allowed-tools |
| 125 | + "Bash(*),Read(*),Write(*),Edit(*)"' |
| 126 | + |
| 127 | + - name: 📤 Push fixes to PR |
| 128 | + if: steps.wait-build.outputs.build_conclusion == 'failure' |
| 129 | + id: push-fixes |
| 130 | + env: |
| 131 | + HEAD_REF: ${{ github.head_ref }} |
| 132 | + run: | |
| 133 | + # Check for uncommitted changes |
| 134 | + if ! git diff --quiet || ! git diff --cached --quiet; then |
| 135 | + echo "⚠️ Claude made changes but didn't commit" |
| 136 | + git status --short |
| 137 | + exit 0 |
| 138 | + fi |
| 139 | +
|
| 140 | + # Check for unpushed commits |
| 141 | + if [ "$(git rev-list --count "origin/$HEAD_REF..HEAD" 2>/dev/null || echo 0)" -gt 0 ]; then |
| 142 | + echo "✅ Pushing fixes to PR branch" |
| 143 | + git log --oneline "origin/$HEAD_REF..HEAD" |
| 144 | + git push |
| 145 | + echo "✅ Successfully pushed fixes - new build will start automatically" |
| 146 | + else |
| 147 | + echo "ℹ️ No commits were made by Claude" |
| 148 | + fi |
0 commit comments