Skip to content

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

  1. Build configuration to run Claude Code CLI in GitHub Actions
  2. Implement proper management of environment variables and secrets
  3. 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

StepContentSuccess Metric
1Claude API setup and secret registrationAPI key authentication success
2Workflow YAML constructionAutomated execution with claude --yes
3Error handling and retry implementationAutomatic 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

SymptomCauseImmediate Fix
API key errorSecret name mismatchVerify name in Settings→Secrets
TimeoutProcessing time exceededIncrease timeout_minutes: 20
git push failureInsufficient permissionsAdd permissions: write-all
Advanced Configuration (Optimization) Parallel execution of multiple prompts:
strategy:
  matrix:
    task: [morning, noon, evening]
Conditional optimization:
if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'