コンテンツにスキップ

Claude Code 完全ガイド

Claude Codeスケジュール実行完全ガイド - GitHub Actions Scheduledで自動化

この記事の対象者

  • Claude Codeを定期実行したい中級者開発者

この記事のポイント

  1. GitHub Actions scheduledの基本設定方法
  2. Claude Code APIを使った自動化スクリプトの実装
  3. 実際に動作するワークフローファイルのコード

問題の核心

cronでClaude Codeを定期実行しようとすると、認証管理やエラーハンドリングが複雑になり、メンテナンスが困難になります。GitHub Actions scheduledなら、シークレット管理、ログ管理、エラー通知が統合され、現代的なCI/CDパイプラインで安全に自動化できます。

解決方法

方法A: 公式 GitHub Action(推奨)

Anthropic公式の anthropics/claude-code-action を使う方法が最も簡単です。

.github/workflows/claude-scheduled.ymlを作成:

name: Claude Code Scheduled
on:
  schedule:
    - cron: '0 9 * * 1-5'  # 平日午前9時(UTC)
  workflow_dispatch:  # 手動実行用

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: "リポジトリを分析し、改善点をIssueにまとめてください"
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}

方法B: Python API直接呼び出し

より細かい制御が必要な場合は、Anthropic Python SDKを使います。

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": "今日のタスクを分析してください"}]
    )
    return response.content

if __name__ == "__main__":
    result = run_daily_analysis()
    print(result)

ワークフローファイル:

name: Claude Code Scheduled
on:
  schedule:
    - cron: '0 9 * * 1-5'
  workflow_dispatch:  # 手動実行用
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

ステップ3: シークレット設定とテスト実行

GitHubリポジトリの Settings > Secrets and variables > Actions で ANTHROPIC_API_KEY を登録し、workflow_dispatchで手動実行して動作確認します。

よくあるトラブルと対処法

症状原因解決策
API認証エラーシークレット設定不備Settings > Secrets > ActionsでAPIキー確認
実行されないcronタイムゾーン問題UTC時間で設定(JST-9時間)
ログが見えないprint文が出力されないprint(result, flush=True)を使用
詳細設定(上級者向け・クリックで展開) ## 高度なスケジュール設定
on:
  schedule:
    # 毎週月曜9時にレポート生成
    - cron: '0 0 * * 1'
    # 毎日12時にチェック
    - 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 }}"}

次のステップ