Copilot Auto-Fix on CI Failure #311
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Copilot Auto-Fix on CI Failure | |
| on: | |
| workflow_run: | |
| workflows: | |
| - Pull Request | |
| types: | |
| - completed | |
| permissions: read-all | |
| jobs: | |
| copilot-fix: | |
| name: Copilot Auto-Fix | |
| runs-on: ubuntu-latest | |
| # Only run for same-repo PRs to avoid running untrusted code with write permissions. | |
| if: > | |
| github.event.workflow_run.conclusion == 'failure' && | |
| github.event.workflow_run.event == 'pull_request' && | |
| github.event.workflow_run.head_repository.full_name == github.repository | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| env: | |
| COPILOT_GITHUB_TOKEN: ${{ secrets.COPILOT_TOKEN }} | |
| steps: | |
| - name: Checkout failing branch | |
| uses: actions/checkout@v7 | |
| with: | |
| ref: ${{ github.event.workflow_run.head_sha }} | |
| fetch-depth: 0 | |
| - name: Set up Node.js | |
| uses: actions/setup-node@v7 | |
| with: | |
| node-version: lts/* | |
| - name: Skip when Copilot token is missing | |
| if: env.COPILOT_GITHUB_TOKEN == '' | |
| run: echo 'COPILOT_TOKEN is not configured; skipping Copilot auto-fix.' | |
| - name: Install Copilot CLI | |
| if: env.COPILOT_GITHUB_TOKEN != '' | |
| run: npm install -g @github/copilot | |
| - name: Run Copilot CLI to fix failing tests | |
| if: env.COPILOT_GITHUB_TOKEN != '' | |
| # COPILOT_TOKEN must be a fine-grained PAT with the "Copilot Requests" permission. | |
| # The repository secret is named COPILOT_TOKEN and is exposed here as COPILOT_GITHUB_TOKEN. | |
| env: | |
| WORKFLOW_NAME: ${{ github.event.workflow_run.name }} | |
| BRANCH_NAME: ${{ github.event.workflow_run.head_branch }} | |
| RUN_URL: ${{ github.event.workflow_run.html_url }} | |
| run: | | |
| copilot -p "The CI workflow '${WORKFLOW_NAME}' is failing on branch '${BRANCH_NAME}' (run: ${RUN_URL}). \ | |
| Run the tests to see exactly what is failing, then fix the source code so all tests pass. \ | |
| Do not modify any test files — only fix the implementation." \ | |
| --allow-tool='write,shell(npm:*),shell(npx:*),shell(yarn:*),shell(git:*)' \ | |
| --no-ask-user | |
| - name: Create PR with Copilot fixes | |
| if: env.COPILOT_GITHUB_TOKEN != '' | |
| uses: peter-evans/create-pull-request@5f6978faf089d4d20b00c7766989d076bb2fc7f1 # v8.1.1 | |
| id: cpr | |
| with: | |
| # PAT is preferred so the fix PR triggers downstream CI workflows. | |
| # Falls back to GITHUB_TOKEN if PAT is not set. | |
| token: ${{ secrets.PAT || secrets.GITHUB_TOKEN }} | |
| commit-message: 'fix(ci): apply Copilot auto-fix for failing tests' | |
| title: 'fix(ci): Copilot auto-fix for failing tests on ${{ github.event.workflow_run.head_branch }}' | |
| labels: | | |
| bot | |
| copilot-fix | |
| branch: copilot-fix/${{ github.event.workflow_run.head_branch }} | |
| body: | | |
| Automated fix generated by GitHub Copilot CLI for failing tests. | |
| **Workflow run:** ${{ github.event.workflow_run.html_url }} | |
| **Branch:** `${{ github.event.workflow_run.head_branch }}` | |
| Please review the changes and merge if they are correct. | |
| base: ${{ github.event.workflow_run.head_branch }} | |
| - name: Post fix-PR link as comment on original PR | |
| if: env.COPILOT_GITHUB_TOKEN != '' && steps.cpr.outputs.pull-request-url != '' | |
| uses: actions/github-script@v9 | |
| with: | |
| script: | | |
| const workflowRun = context.payload.workflow_run; | |
| if (!workflowRun.pull_requests?.length) return; | |
| const prNumber = workflowRun.pull_requests[0].number; | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber, | |
| body: `🤖 Copilot has automatically generated a fix for the failing tests.\n\nFix PR: ${{ steps.cpr.outputs.pull-request-url }}` | |
| }); |