Complete Guide to Claude Code Scheduled Execution - Automation with GitHub Actions Scheduled Workflows¶
Target Audience
- Intermediate developers who want to run Claude Code periodically
Key Points¶
- How to configure GitHub Actions scheduled workflows
- Implementing automation scripts using Claude Code API
- Working workflow file code
The Core Problem¶
Attempting to run Claude Code periodically with cron leads to complex authentication management and error handling, making maintenance difficult. GitHub Actions scheduled workflows integrate secret management, log management, and error notifications, enabling safe automation within a modern CI/CD pipeline.
Solution¶
Step 1: Create GitHub Actions Workflow File¶
Create .github/workflows/claude-scheduled.yml and add the following code:
name: Claude Code Scheduled
on:
schedule:
- cron: '0 9 * * 1-5' # Weekdays at 9 AM
jobs:
claude-automation:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run Claude automation
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: python scripts/claude_automation.py
Step 2: Implement Automation Script¶
Create scripts/claude_automation.py and configure basic Claude API calls:
import os
import anthropic
client = anthropic.Anthropic(api_key=os.getenv('ANTHROPIC_API_KEY'))
def run_daily_analysis():
response = client.messages.create(
model="claude-3-sonnet-20240229",
max_tokens=1000,
messages=[{"role": "user", "content": "Please analyze today's tasks"}]
)
return response.content
if __name__ == "__main__":
result = run_daily_analysis()
print(result)
Step 3: Configure Secrets and Test Execution¶
Register ANTHROPICAPIKEY in GitHub repository settings and verify operation with manual trigger:
on:
schedule:
- cron: '0 9 * * 1-5'
workflow_dispatch: # For manual execution
Common Issues and Solutions¶
| Symptom | Cause | Solution |
|---|---|---|
| API authentication error | Secret configuration issue | Check API key in Settings > Secrets > Actions |
| Doesn't execute | Cron timezone issue | Configure in UTC time (JST-9 hours) |
| Logs not visible | print statement not outputting | Use print(result, flush=True) |
Advanced Configuration (For Advanced Users - Click to Expand)
## Advanced Schedule Configurationon:
schedule:
# Generate report every Monday at 9 AM
- cron: '0 0 * * 1'
# Daily check at noon
- cron: '0 3 * * *'
workflow_dispatch:
inputs:
task_type:
description: 'Task type to run'
required: true
default: 'analysis'
type: choice
options:
- analysis
- report
- cleanup
- name: Notify on failure
if: failure()
uses: 8398a7/action-slack@v3
with:
status: failure
webhook_url: ${{ secrets.SLACK_WEBHOOK }}
Next Steps¶
- Learn more advanced automation in Complete Guide to Claude Code Hooks