Building an Automated Article Generation Pipeline with Claude Code CLI in GitHub Actions¶
Target Audience
- Intermediate users who understand the basics of GitHub Actions
Key Points¶
- Build configuration to run Claude Code CLI in GitHub Actions
- Implement proper management of environment variables and secrets
- Integrate error handling and retry strategies
Why This Matters Now¶
As use cases for generative AI in GitHub Actions workflows increase, there's growing demand for automating regular content generation and analysis tasks. Claude Code CLI's non-interactive execution enables fully automated AI-driven workflows.
Solution Steps Overview¶
| Step | Content | Success Metric |
|---|---|---|
| 1 | Claude API setup and secret registration | API key authentication success |
| 2 | Workflow YAML construction | Automated execution with claude --yes |
| 3 | Error handling and retry implementation | Automatic recovery on failure |
Step 1: Claude API Setup and Secret Registration¶
Register the following in your GitHub repository Settings → Secrets:
ANTHROPIC_API_KEY: sk-ant-api03-xxxxx
Configuration for using as environment variable:
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
Step 2: Workflow YAML Construction¶
Basic structure of .github/workflows/auto-content.yml:
name: Auto Content Generation
on:
schedule:
- cron: '0 21 * * *' # Daily at 06:00 JST
workflow_dispatch:
jobs:
generate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run Claude Code CLI
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
npx -y @anthropic-ai/claude-cli@latest \
--yes \
--max-tokens 8192 \
"Generate one technical article from GA4 data"
Step 3: Error Handling and Retry Implementation¶
Implementing retry strategy for robustness:
- name: Generate with retry
uses: nick-fields/retry@v3
with:
timeout_minutes: 10
max_attempts: 3
retry_wait_seconds: 30
command: |
npx -y @anthropic-ai/claude-cli@latest \
--yes --max-tokens 8192 \
--model claude-3-5-sonnet-20241022 \
"${{ env.PROMPT }}"
Common Pitfalls and Solutions¶
| Symptom | Cause | Immediate Fix |
|---|---|---|
| API key error | Secret name mismatch | Verify name in Settings→Secrets |
| Timeout | Processing time exceeded | Increase timeout_minutes: 20 |
| git push failure | Insufficient permissions | Add permissions: write-all |
Advanced Configuration (Optimization)
Parallel execution of multiple prompts:strategy:
matrix:
task: [morning, noon, evening]
if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'