Complete Guide to Claude Code Scheduled Execution - Automate with GitHub Actions Scheduled Workflows¶
Target Audience
- Intermediate developers who want to run Claude Code on a schedule
Key Points¶
- Basic configuration of GitHub Actions scheduled workflows
- Implementation of automation scripts using Claude Code API
- Working workflow file code you can use immediately
The Core Problem¶
When trying to schedule Claude Code execution with cron, authentication management and error handling become complex, making maintenance difficult. With GitHub Actions scheduled workflows, secret management, log management, and error notifications are integrated, enabling safe automation with modern CI/CD pipelines.
Solution¶
Method A: Official GitHub Action (Recommended)¶
The simplest approach is to use Anthropic's official anthropics/claude-code-action.
Create .github/workflows/claude-scheduled.yml:
name: Claude Code Scheduled
on:
schedule:
- cron: '0 9 * * 1-5' # Weekdays at 9 AM (UTC)
workflow_dispatch: # For manual execution
jobs:
claude-automation:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
issues: write
steps:
- uses: actions/checkout@v4
- uses: anthropics/claude-code-action@v1
with:
prompt: "Analyze the repository and summarize improvements in an Issue"
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
Method B: Direct Python API Call¶
For finer-grained control, use the Anthropic Python SDK.
Create scripts/claude_automation.py:
import os
import anthropic
client = anthropic.Anthropic(api_key=os.getenv('ANTHROPIC_API_KEY'))
def run_daily_analysis():
response = client.messages.create(
model="claude-sonnet-4-5-20250929",
max_tokens=1000,
messages=[{"role": "user", "content": "Please analyze today's tasks"}]
)
return response.content
if __name__ == "__main__":
result = run_daily_analysis()
print(result)
Workflow file:
name: Claude Code Scheduled
on:
schedule:
- cron: '0 9 * * 1-5'
workflow_dispatch: # For manual execution
jobs:
claude-automation:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- run: pip install anthropic
- name: Run Claude automation
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: python scripts/claude_automation.py
Step 3: Configure Secrets and Test Execution¶
Register ANTHROPIC_API_KEY in GitHub repository Settings > Secrets and variables > Actions, then use workflow_dispatch to manually trigger and verify.
Common Issues and Solutions¶
| Symptom | Cause | Solution |
|---|---|---|
| API authentication error | Secret configuration issue | Check API key in Settings > Secrets > Actions |
| Not executing | Cron timezone issue | Configure in UTC time (JST -9 hours) |
| Logs not visible | print statements 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: slackapi/slack-github-action@v2
with:
webhook: ${{ secrets.SLACK_WEBHOOK_URL }}
webhook-type: incoming-webhook
payload: |
{"text": "Claude Code scheduled job failed: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"}
Next Steps¶
- Learn more advanced automation with Claude Code Hooks Complete Guide
- Check the official Action at anthropics/claude-code-action repository