Claude Code Base Action

repository·main·Indexed 21 days ago

https://github.com/anthropics/claude-code-base-action

A GitHub Action that allows running Claude Code within CI/CD workflows to build custom automation. It supports direct prompts or prompt files, custom system prompts, conversation turn limits, and Model Context Protocol (MCP) server configuration. Authentication is available via Anthropic API keys, Workload Identity Federation (GitHub OIDC), Amazon Bedrock, and Google Vertex AI.

Tokens
9.5K
Snippets
21
Records
32
Agent score
75%

What's inside claude-code-base-action

  1. Understand the Claude Code Base Action trust model

    main

    The claude-code-base-action is a thin wrapper that installs and runs Claude Code. It does not enforce trust boundaries. Claude Code will read project-level configuration (like .claude/, CLAUDE.md, or .mcp.json) from the working directory.

    Security Warning: The caller is responsible for ensuring the working directory and prompt are trusted. If your workflow processes untrusted input (e.g., from issues or fork pull requests), do not use this action. Instead, use anthropics/claude-code-action, which provides actor permission checks and protects project configuration.

  2. Configure Claude Code settings

    main

    You can configure Claude Code settings using either a path to a JSON file or an inline JSON string via the settings input. Supported options include model, env, permissions, hooks, and includeCoAuthoredBy.

    Note: The enableAllProjectMcpServers setting is automatically set to true by this action to ensure MCP server compatibility.

    # Option 1: Using a settings file
    - name: Run Claude Code with settings file
      uses: anthropics/claude-code-base-action@beta
      with:
        prompt: "Your prompt here"
        settings: "path/to/settings.json"
        allowed_tools: "Bash(git:*),View,GlobTool,GrepTool,BatchTool"
        anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
    
    # Option 2: Using inline settings
    - name: Run Claude Code with inline settings
      uses: anthropics/claude-code-base-action@beta
      with:
        prompt: "Your prompt here"
        settings: |
          {
            "model": "claude-opus-4-1-20250805",
            "env": {
              "DEBUG": "true"
            },
            "permissions": {
              "allow": ["Bash", "Read"],
              "deny": ["WebFetch"]
            }
          }
        allowed_tools: "Bash(git:*),View,GlobTool,GrepTool,BatchTool"
        anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
  3. Run Claude Code with a prompt

    main

    You can trigger Claude Code by providing a direct string via the prompt input or by pointing to a text file via prompt_file. You must provide exactly one of these.

    # Using a direct prompt
    - name: Run Claude Code with direct prompt
      uses: anthropics/claude-code-base-action@beta
      with:
        prompt: "Your prompt here"
        allowed_tools: "Bash(git:*),View,GlobTool,GrepTool,BatchTool"
        anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
    
    # Or using a prompt from a file
    - name: Run Claude Code with prompt file
      uses: anthropics/claude-code-base-action@beta
      with:
        prompt_file: "/path/to/prompt.txt"
        allowed_tools: "Bash(git:*),View,GlobTool,GrepTool,BatchTool"
        anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
  4. Configure MCP servers

    main

    Model Context Protocol (MCP) configuration can be provided via a path to a JSON file (mcp_config) or as an inline JSON string. The configuration must follow the standard MCP format containing an mcpServers object.

    # Using inline MCP config
    - name: Run Claude Code with inline MCP config
      uses: anthropics/claude-code-base-action@beta
      with:
        prompt: "Your prompt here"
        mcp_config: |
          {
            "mcpServers": {
              "server-name": {
                "command": "node",
                "args": ["./server.js"],
                "env": {
                  "API_KEY": "your-api-key"
                }
              }
            }
          }
        allowed_tools: "Bash(git:*),View,mcp__server-name__custom_tool"
        anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
  5. Configure Claude Code system prompts and conversation limits

    main

    You can control the behavior of the Claude Code agent using the following inputs:

    • system_prompt: Overrides the default system prompt.
    • append_system_prompt: Appends instructions to the existing system prompt.
    • max_turns: Limits the number of conversation turns to prevent infinite loops or excessive cost.
    # Limit conversation to 5 turns
    - name: Run Claude Code with limited turns
      uses: anthropics/claude-code-base-action@beta
      with:
        prompt: "Your prompt here"
        max_turns: "5"
        anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
    
    # Use a custom system prompt
    - name: Run Claude Code with custom system prompt
      uses: anthropics/claude-code-base-action@beta
      with:
        prompt: "Build a REST API"
        system_prompt: "You are a senior backend engineer."
        anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
  6. Securely configure the Anthropic API key

    main

    Never hardcode API keys in your workflow files. Instead, use GitHub Actions secrets to manage sensitive credentials.

    1. Add your key as a repository secret named ANTHROPIC_API_KEY in your GitHub repository settings.
    2. Reference the secret in your workflow using the ${{ secrets.ANTHROPIC_API_KEY }} syntax.
    # ✅ CORRECT - Uses GitHub secrets
    anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
  7. Authenticate via Workload Identity Federation

    main

    Instead of using static API keys or OAuth tokens, you can use Workload Identity Federation to authenticate via GitHub OIDC. This requires the id-token: write permission on the job.

    Note: Do not provide anthropic_api_key or claude_code_oauth_token when using federation, as static credentials take precedence and will prevent federation from working.

    permissions:
      contents: read
      id-token: write
    
    steps:
      - name: Run Claude Code with workload identity federation
        uses: anthropics/claude-code-base-action@beta
        with:
          prompt: "Your prompt here"
          anthropic_federation_rule_id: fdrl_xxxxxxxxxxxx
          anthropic_organization_id: 00000000-0000-0000-0000-000000000000
          anthropic_service_account_id: svac_xxxxxxxxxxxx
  8. Pass custom environment variables via claude_env

    main

    To provide Claude with environment-specific configuration (e.g., API URLs, database credentials, or feature flags) during its execution, use the claude_env input. This input accepts a YAML multiline string of key-value pairs. These variables are available to Claude at runtime.

    - name: Deploy with custom environment
      uses: anthropics/claude-code-base-action@beta
      with:
        prompt: "Deploy the application to the staging environment"
        claude_env: |
          ENVIRONMENT: staging
          API_BASE_URL: https://api-staging.example.com
          DATABASE_URL: ${{ secrets.STAGING_DB_URL }}
          DEBUG: true
          LOG_LEVEL: debug
        allowed_tools: "Bash(git:*),View,GlobTool,GrepTool,BatchTool"
        anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
  9. Authenticate with Cloud Providers (Bedrock and Vertex AI)

    main

    The action supports authentication via Amazon Bedrock and Google Vertex AI using OIDC.

    Amazon Bedrock

    • Requires OIDC authentication (e.g., via aws-actions/configure-aws-credentials).
    • Set use_bedrock: "true".
    • Use provider-specific model names (e.g., anthropic.claude-3-7-sonnet-20250219-v1:0).
    • The API endpoint is constructed using AWS_REGION. You can override it with ANTHROPIC_BEDROCK_BASE_URL.

    Google Vertex AI

    • Requires OIDC authentication (e.g., via google-github-actions/auth).
    • Set use_vertex: "true".
    • Use provider-specific model names (e.g., claude-3-7-sonnet@20250219).
    # Example: Bedrock Configuration
    - name: Run Claude Code with Bedrock
      uses: anthropics/claude-code-base-action@beta
      with:
        prompt: "Your prompt here"
        model: "anthropic.claude-3-7-sonnet-20250219-v1:0"
        use_bedrock: "true"
    
    # Example: Vertex AI Configuration
    - name: Run Claude Code with Vertex AI
      uses: anthropics/claude-code-base-action@beta
      with:
        prompt: "Your prompt here"
        model: "claude-3-7-sonnet@20250219"
        use_vertex: "true"
  10. Configure Node.js version via NODE_VERSION

    main

    You can specify the Node.js version used by the action by setting the NODE_VERSION environment variable. The default is 18.x.

    - name: Run Claude Code with Node.js 20
      uses: anthropics/claude-code-base-action@beta
      env:
        NODE_VERSION: "20.x"
      with:
        prompt: "Your prompt here"
        anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
  11. How Claude Code processes user requests and slash commands

    main

    The action supports two modes of prompt delivery based on the presence of a specific file in the same directory as your prompt file:

    1. Simple Prompt: If no user request file is present, the content of the prompt file is used as a single string.
    2. Multi-block Message (Slash Command Support): If a file named claude-user-request.txt exists in the same directory as your prompt file, the action creates a multi-block SDKUserMessage. This combines the prompt file (acting as instructions/context) with the content of claude-user-request.txt (the actual user request). This mode is required to enable the processing of slash commands within the CLI/SDK context.

    Requirement: To use slash commands, ensure claude-user-request.txt is co-located with your prompt file.

    # Directory Structure Example
    /path/to/prompts/
      ├── my-prompt.txt
      └── claude-user-request.txt  <-- Required for slash commands
  12. Configure MCP servers via mcp-config

    main

    You can provide Model Context Protocol (MCP) configurations using the --mcp-config flag within the claudeArgs string. The parser supports two modes:

    1. Inline JSON: Provide a JSON string containing an mcpServers object. Multiple inline JSON configs will have their mcpServers merged.
    2. File Path: Provide a path to a configuration file.

    If you provide both inline JSON and a file path, the action attempts to merge the inline JSON objects. Note that if a file path is provided, the action's ability to merge it with inline JSON at parse-time is limited; ensure your file path includes all necessary servers if you are relying on a file-based config.

    # Example of passing multiple MCP configs in claudeArgs
    claudeArgs: '--mcp-config "{\"mcpServers\": {\"server1\": {\"command\": \"...\"}}}" --mcp-config /path/to/config.json'