mirror of
https://github.com/anthropics/claude-code-action.git
synced 2026-01-22 22:44:13 +08:00
- Clarify that Claude must commit changes even when branch exists - Update both slash command and inline workflow instructions - Make git add and commit commands more explicit
175 lines
7.0 KiB
YAML
175 lines
7.0 KiB
YAML
name: Auto Fix CI Failures (Inline)
|
|
|
|
on:
|
|
workflow_run:
|
|
workflows: ["CI"]
|
|
types:
|
|
- completed
|
|
|
|
permissions:
|
|
contents: write
|
|
pull-requests: write
|
|
actions: read
|
|
issues: write
|
|
|
|
jobs:
|
|
auto-fix:
|
|
if: |
|
|
github.event.workflow_run.conclusion == 'failure' &&
|
|
github.event.workflow_run.name != 'Auto Fix CI Failures' &&
|
|
github.event.workflow_run.name != 'Auto Fix CI Failures (Inline)'
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
with:
|
|
ref: ${{ github.event.workflow_run.head_branch }}
|
|
fetch-depth: 0
|
|
token: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
- name: Setup git
|
|
run: |
|
|
git config --global user.name "claude[bot]"
|
|
git config --global user.email "198276+claude[bot]@users.noreply.github.com"
|
|
|
|
- name: Create fix branch
|
|
id: branch
|
|
run: |
|
|
BRANCH_NAME="claude-auto-fix-ci-${{ github.event.workflow_run.head_branch }}-${{ github.run_id }}"
|
|
git checkout -b "$BRANCH_NAME"
|
|
echo "branch_name=$BRANCH_NAME" >> $GITHUB_OUTPUT
|
|
|
|
- name: Get CI failure details
|
|
id: failure_details
|
|
uses: actions/github-script@v7
|
|
with:
|
|
script: |
|
|
const run = await github.rest.actions.getWorkflowRun({
|
|
owner: context.repo.owner,
|
|
repo: context.repo.repo,
|
|
run_id: ${{ github.event.workflow_run.id }}
|
|
});
|
|
|
|
const jobs = await github.rest.actions.listJobsForWorkflowRun({
|
|
owner: context.repo.owner,
|
|
repo: context.repo.repo,
|
|
run_id: ${{ github.event.workflow_run.id }}
|
|
});
|
|
|
|
const failedJobs = jobs.data.jobs.filter(job => job.conclusion === 'failure');
|
|
|
|
let errorLogs = [];
|
|
for (const job of failedJobs) {
|
|
const logs = await github.rest.actions.downloadJobLogsForWorkflowRun({
|
|
owner: context.repo.owner,
|
|
repo: context.repo.repo,
|
|
job_id: job.id
|
|
});
|
|
errorLogs.push({
|
|
jobName: job.name,
|
|
logs: logs.data
|
|
});
|
|
}
|
|
|
|
return {
|
|
runUrl: run.data.html_url,
|
|
failedJobs: failedJobs.map(j => j.name),
|
|
errorLogs: errorLogs
|
|
};
|
|
|
|
- name: Fix CI failures with Claude
|
|
uses: km-anthropic/claude-code-action@v1-dev
|
|
with:
|
|
prompt: |
|
|
You are tasked with analyzing CI failure logs and fixing the issues. Follow these steps:
|
|
|
|
## Context Provided
|
|
|
|
Failed CI Run: ${{ fromJSON(steps.failure_details.outputs.result).runUrl }}
|
|
Failed Jobs: ${{ join(fromJSON(steps.failure_details.outputs.result).failedJobs, ', ') }}
|
|
|
|
Error logs:
|
|
${{ toJSON(fromJSON(steps.failure_details.outputs.result).errorLogs) }}
|
|
|
|
## Step 1: Analyze the Failure
|
|
|
|
Parse the provided CI failure information to understand:
|
|
- Which jobs failed and why
|
|
- The specific error messages and stack traces
|
|
- Whether failures are test-related, build-related, or linting issues
|
|
|
|
## Step 2: Search and Understand the Codebase
|
|
|
|
Use search tools to locate the failing code:
|
|
- Search for the failing test names or functions
|
|
- Find the source files mentioned in error messages
|
|
- Review related configuration files (package.json, tsconfig.json, etc.)
|
|
|
|
## Step 3: Apply Targeted Fixes
|
|
|
|
Make minimal, focused changes:
|
|
- **For test failures**: Determine if the test or implementation needs fixing
|
|
- **For type errors**: Fix type definitions or correct the code logic
|
|
- **For linting issues**: Apply formatting using the project's tools
|
|
- **For build errors**: Resolve dependency or configuration issues
|
|
- **For missing imports**: Add the necessary imports or install packages
|
|
|
|
Requirements:
|
|
- Only fix the actual CI failures, avoid unrelated changes
|
|
- Follow existing code patterns and conventions
|
|
- Ensure changes are production-ready, not temporary hacks
|
|
- Preserve existing functionality while fixing issues
|
|
|
|
## Step 4: Commit Changes
|
|
|
|
After applying ALL fixes:
|
|
1. Stage all modified files with `git add -A`
|
|
2. Commit with: `git commit -m "Fix CI failures: prettier formatting and syntax errors"`
|
|
3. Important: You MUST commit your changes - the branch already exists
|
|
|
|
## Step 5: Verify Fixes Locally
|
|
|
|
Run available verification commands:
|
|
- Execute the failing tests locally to confirm they pass
|
|
- Run the project's lint command (check package.json for scripts)
|
|
- Run type checking if available
|
|
- Execute any build commands to ensure compilation succeeds
|
|
|
|
## Important Guidelines
|
|
|
|
- Focus exclusively on fixing the reported CI failures
|
|
- Maintain code quality and follow the project's established patterns
|
|
- If a fix requires significant refactoring, document why it's necessary
|
|
- When multiple solutions exist, choose the simplest one that maintains code quality
|
|
|
|
Begin by analyzing the failure details provided above.
|
|
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
|
|
github_token: ${{ secrets.GITHUB_TOKEN }}
|
|
timeout_minutes: "30"
|
|
use_sticky_comment: "true"
|
|
claude_args: "--max-turns 15"
|
|
|
|
- name: Push fix branch
|
|
if: success()
|
|
run: |
|
|
git push origin ${{ steps.branch.outputs.branch_name }}
|
|
|
|
- name: Create pull request comment
|
|
if: success()
|
|
uses: actions/github-script@v7
|
|
with:
|
|
script: |
|
|
const branchName = '${{ steps.branch.outputs.branch_name }}';
|
|
const baseBranch = '${{ github.event.workflow_run.head_branch }}';
|
|
const prUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/compare/${baseBranch}...${branchName}?quick_pull=1`;
|
|
|
|
const issueNumber = ${{ github.event.workflow_run.pull_requests[0]?.number || 'null' }};
|
|
|
|
if (issueNumber) {
|
|
await github.rest.issues.createComment({
|
|
owner: context.repo.owner,
|
|
repo: context.repo.repo,
|
|
issue_number: issueNumber,
|
|
body: `## 🤖 CI Auto-Fix Available\n\nClaude has analyzed the CI failures and prepared fixes.\n\n[**→ Create pull request to fix CI**](${prUrl})\n\n_This fix was generated automatically based on the [failed CI run](${{ fromJSON(steps.failure_details.outputs.result).runUrl }})._`
|
|
});
|
|
} |