Automatically Fix Common CI/CD Failures Without Human Intervention
An autonomous agent that monitors your GitHub repository, detects CI/CD failures, analyzes errors, and automatically fixes common issues without human intervention.
Watches GitHub Actions workflows for failures
Uses pattern matching and AI to understand errors
Automatically applies fixes for common issues
Creates summaries and issues for complex problems
Error: npm ci can only install packages when your package.json and package-lock.json are in sync
Common Cause: Someone updated package.json but forgot to commit the updated package-lock.json
Auto-Fix: Runs npm install to update lock file and commits the change
# The agent automatically runs:
npm install
git add package-lock.json
git commit -m "🤖 Auto-fix: Update package-lock.json to sync with package.json"
git push
Error: Missing: [package] from lock file
Auto-Fix: Installs missing dependencies and updates lock file
# The agent automatically runs:
npm install [missing-package]
git add package-lock.json package.json
git commit -m "🤖 Auto-fix: Install missing dependencies"
git push
Error: Unknown or complex errors
Action: Creates a GitHub issue with error details for manual review
Smart Behavior: The agent only auto-fixes errors it's confident about. Complex or unknown errors are flagged for human review.
The workflow file is already created at .github/workflows/autonomous-ci-fix-agent.yml
# Check if the workflow file exists
ls .github/workflows/autonomous-ci-fix-agent.yml
The workflow needs these permissions (already configured):
contents: write - To commit fixespull-requests: write - To create PRs (if needed)issues: write - To create issues for complex errorsNote: These permissions are already set in the workflow file. No action needed unless you want to modify them.
The agent will trigger automatically when CI workflows fail. To test:
on:
workflow_run:
workflows: ["CI", "Tests", "Build"]
types:
- completed
The agent runs when:
Customization: You can modify the workflows list to monitor different workflow names.
The agent uses pattern matching to identify common errors:
# NPM lock file sync
if grep -q "npm ci.*can only install packages"; then
fix_action="run_npm_install"
fi
# Missing dependencies
if grep -q "Missing:.*from lock file"; then
fix_action="run_npm_install"
fi
How It Works:
npm installpackage-lock.json changedAdd this step to analyze errors with GPT:
- name: Analyze error with OpenAI
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
run: |
ERROR_LOG=$(cat workflow_logs.txt)
RESPONSE=$(curl -s https://api.openai.com/v1/chat/completions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4",
"messages": [{
"role": "system",
"content": "You are a CI/CD error analyzer. Analyze the error and suggest a fix."
}, {
"role": "user",
"content": "Error: '"$ERROR_LOG"'"
}]
}')
echo "analysis=$RESPONSE" >> $GITHUB_OUTPUT
Benefits: More intelligent error analysis, can handle complex errors, suggests better fixes
Cost: ~$0.01-0.10 per analysis
If you have a self-hosted runner with Ollama:
- name: Analyze with Ollama
run: |
ERROR_LOG=$(cat workflow_logs.txt)
ANALYSIS=$(ollama run llama3.2:3b "Analyze this CI error and suggest a fix: $ERROR_LOG")
echo "analysis=$ANALYSIS" >> $GITHUB_OUTPUT
Benefits: Completely free, runs locally, no API costs, private
Requirement: Self-hosted GitHub Actions runner with Ollama installed
- name: Analyze with GitHub Copilot
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const errorLog = fs.readFileSync('workflow_logs.txt', 'utf8');
// Use GitHub API to analyze
// Implementation depends on Copilot API availability
Note: GitHub Copilot API integration may require additional setup. Check GitHub's documentation for current availability.
Edit the workflow to add more patterns:
- name: Analyze error with AI
run: |
# Add your custom patterns
if echo "$ERROR_LOG" | grep -q "Your custom error pattern"; then
echo "error_type=custom_error" >> $GITHUB_OUTPUT
echo "fix_action=custom_fix" >> $GITHUB_OUTPUT
fi
# Detect: "ERROR: Could not find a version that satisfies the requirement"
if echo "$ERROR_LOG" | grep -q "Could not find a version"; then
echo "error_type=python_dependency" >> $GITHUB_OUTPUT
echo "fix_action=update_requirements" >> $GITHUB_OUTPUT
fi
Add new fix steps:
- name: Auto-fix custom error
if: steps.analyze.outputs.error_type == 'custom_error'
run: |
# Your fix commands here
npm run fix-custom-issue
git add .
git commit -m "🤖 Auto-fix: Custom error"
git push
- name: Auto-fix Python dependencies
if: steps.analyze.outputs.error_type == 'python_dependency'
run: |
pip install --upgrade pip
pip install -r requirements.txt
git add requirements.txt
git commit -m "🤖 Auto-fix: Update Python dependencies"
git push
Change which workflows trigger the agent:
on:
workflow_run:
workflows: ["Your-Workflow-Name", "Another-Workflow"]
types:
- completed
Tip: You can monitor all workflows by using workflows: ["*"], but be careful as this will trigger on every workflow failure.
Here's a more advanced version using OpenAI:
name: AI-Powered CI Fix Agent
on:
workflow_run:
workflows: ["CI"]
types:
- completed
jobs:
ai-fix:
if: github.event.workflow_run.conclusion == 'failure'
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- uses: actions/checkout@v4
- name: Get error logs
id: logs
run: |
gh run view ${{ github.event.workflow_run.id }} --log > error.log
echo "error=$(cat error.log | base64 -w 0)" >> $GITHUB_OUTPUT
- name: AI Analysis
id: ai
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
run: |
ERROR=$(echo "${{ steps.logs.outputs.error }}" | base64 -d)
ANALYSIS=$(curl -s https://api.openai.com/v1/chat/completions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4",
"messages": [{
"role": "system",
"content": "Analyze CI errors and return JSON: {\"error_type\": \"...\", \"fix_commands\": [\"...\"], \"confidence\": 0.9}"
}, {
"role": "user",
"content": "'"$ERROR"'"
}]
}' | jq -r '.choices[0].message.content')
echo "analysis=$ANALYSIS" >> $GITHUB_OUTPUT
- name: Apply AI-suggested fix
if: steps.ai.outputs.analysis != ''
run: |
ANALYSIS='${{ steps.ai.outputs.analysis }}'
FIX_COMMANDS=$(echo "$ANALYSIS" | jq -r '.fix_commands[]')
for cmd in $FIX_COMMANDS; do
eval "$cmd"
done
git add .
git commit -m "🤖 AI Auto-fix: ${{ steps.ai.outputs.analysis | jq -r '.error_type' }}"
git push
Security Note: Be very careful when executing AI-suggested commands. Always review the commands before execution, or add a safety check to only execute commands from a whitelist.
Add Slack/Discord notifications:
- name: Notify on fix
if: steps.analyze.outputs.error_type != 'no_logs'
uses: slackapi/slack-github-action@v1
with:
webhook-url: ${{ secrets.SLACK_WEBHOOK }}
payload: |
{
"text": "🤖 Auto-fixed CI error: ${{ steps.analyze.outputs.error_type }}"
}
- name: Send email notification
uses: dawidd6/action-send-mail@v3
with:
server_address: smtp.gmail.com
server_port: 465
username: ${{ secrets.EMAIL_USERNAME }}
password: ${{ secrets.EMAIL_PASSWORD }}
subject: "CI Auto-Fix: ${{ steps.analyze.outputs.error_type }}"
body: "The agent fixed: ${{ steps.analyze.outputs.error_type }}"
to: your-email@example.com
Perfect for: Most use cases, especially if you have a public repository
| Service | Cost per Analysis | Best For |
|---|---|---|
| OpenAI API | ~$0.01-0.10 | Complex error analysis |
| Ollama | Free | Self-hosted runners |
| GitHub Copilot | Included with subscription | GitHub Enterprise users |
Cost Estimate: If you have 10 CI failures per week and use OpenAI API, that's approximately $0.10-1.00 per week, or $5-50 per year.
This agent autonomously fixes CI failures, saving you time and keeping your builds green. Start with the basic pattern matching version, then enhance with AI as needed.