|
| 1 | +name: "Claude Code Action" |
| 2 | +description: "Run Claude Code in GitHub Actions workflows" |
| 3 | + |
| 4 | +inputs: |
| 5 | + github_token: |
| 6 | + description: "GitHub token with repo and issues permissions" |
| 7 | + required: true |
| 8 | + anthropic_api_key: |
| 9 | + description: "Anthropic API key" |
| 10 | + required: true |
| 11 | + prompt: |
| 12 | + description: "The prompt to send to Claude Code" |
| 13 | + required: false |
| 14 | + default: "" |
| 15 | + prompt_file: |
| 16 | + description: "Path to a file containing the prompt to send to Claude Code" |
| 17 | + required: false |
| 18 | + default: "" |
| 19 | + allowed_tools: |
| 20 | + description: "Comma-separated list of allowed tools for Claude Code to use" |
| 21 | + required: false |
| 22 | + default: "" |
| 23 | + output_file: |
| 24 | + description: "File to save Claude Code output to (optional)" |
| 25 | + required: false |
| 26 | + default: "" |
| 27 | + timeout_minutes: |
| 28 | + description: "Timeout in minutes for Claude Code execution" |
| 29 | + required: false |
| 30 | + default: "10" |
| 31 | + install_github_mcp: |
| 32 | + description: "Whether to install the GitHub MCP server" |
| 33 | + required: false |
| 34 | + default: "false" |
| 35 | + |
| 36 | +runs: |
| 37 | + using: "composite" |
| 38 | + steps: |
| 39 | + - name: Install Claude Code |
| 40 | + shell: bash |
| 41 | + run: npm install -g @anthropic-ai/claude-code |
| 42 | + |
| 43 | + - name: Install GitHub MCP Server |
| 44 | + if: inputs.install_github_mcp == 'true' |
| 45 | + shell: bash |
| 46 | + run: | |
| 47 | + claude mcp add-json github '{ |
| 48 | + "command": "docker", |
| 49 | + "args": [ |
| 50 | + "run", |
| 51 | + "-i", |
| 52 | + "--rm", |
| 53 | + "-e", |
| 54 | + "GITHUB_PERSONAL_ACCESS_TOKEN", |
| 55 | + "ghcr.io/github/github-mcp-server:sha-ff3036d" |
| 56 | + ], |
| 57 | + "env": { |
| 58 | + "GITHUB_PERSONAL_ACCESS_TOKEN": "${{ inputs.GITHUB_TOKEN }}" |
| 59 | + } |
| 60 | + }' |
| 61 | +
|
| 62 | + - name: Prepare Prompt File |
| 63 | + shell: bash |
| 64 | + id: prepare_prompt |
| 65 | + run: | |
| 66 | + if [ -z "${{ inputs.prompt }}" ] && [ -z "${{ inputs.prompt_file }}" ]; then |
| 67 | + echo "::error::Neither 'prompt' nor 'prompt_file' was provided. At least one is required." |
| 68 | + exit 1 |
| 69 | + fi |
| 70 | +
|
| 71 | + if [ ! -z "${{ inputs.prompt_file }}" ]; then |
| 72 | + if [ ! -f "${{ inputs.prompt_file }}" ]; then |
| 73 | + echo "::error::Prompt file '${{ inputs.prompt_file }}' does not exist." |
| 74 | + exit 1 |
| 75 | + fi |
| 76 | + PROMPT_PATH="${{ inputs.prompt_file }}" |
| 77 | + else |
| 78 | + mkdir -p /tmp/claude-action |
| 79 | + PROMPT_PATH="/tmp/claude-action/prompt.txt" |
| 80 | + echo "${{ inputs.prompt }}" > "$PROMPT_PATH" |
| 81 | + fi |
| 82 | +
|
| 83 | + if [ ! -s "$PROMPT_PATH" ]; then |
| 84 | + echo "::error::Prompt is empty. Please provide a non-empty prompt." |
| 85 | + exit 1 |
| 86 | + fi |
| 87 | +
|
| 88 | + echo "PROMPT_PATH=$PROMPT_PATH" >> $GITHUB_ENV |
| 89 | +
|
| 90 | + - name: Run Claude Code |
| 91 | + shell: bash |
| 92 | + id: run_claude |
| 93 | + run: | |
| 94 | + timeout_seconds=$((${{ inputs.timeout_minutes }} * 60)) |
| 95 | +
|
| 96 | + if [ -z "${{ inputs.output_file }}" ]; then |
| 97 | + timeout $timeout_seconds claude \ |
| 98 | + -p \ |
| 99 | + --verbose \ |
| 100 | + --output-format stream-json \ |
| 101 | + "$(cat ${{ env.PROMPT_PATH }})" \ |
| 102 | + ${{ inputs.allowed_tools != '' && format('--allowedTools "{0}"', inputs.allowed_tools) || '' }} |
| 103 | + else |
| 104 | + timeout $timeout_seconds claude \ |
| 105 | + -p \ |
| 106 | + --verbose \ |
| 107 | + --output-format stream-json \ |
| 108 | + "$(cat ${{ env.PROMPT_PATH }})" \ |
| 109 | + ${{ inputs.allowed_tools != '' && format('--allowedTools "{0}"', inputs.allowed_tools) || '' }} | tee output.txt |
| 110 | +
|
| 111 | + jq -s '.' output.txt > output.json |
| 112 | + jq -r '.[-1].result' output.json > "${{ inputs.output_file }}" |
| 113 | + echo "Complete output saved to output.json, final response saved to ${{ inputs.output_file }}" |
| 114 | + fi |
| 115 | + env: |
| 116 | + ANTHROPIC_API_KEY: ${{ inputs.anthropic_api_key }} |
| 117 | + GITHUB_TOKEN: ${{ inputs.github_token }} |
0 commit comments