summaryrefslogtreecommitdiffstats
path: root/.github/actions/claude-code-runner/action.yml
blob: f68a7914ca374a561b1e73e35db32de51b0c3b5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
name: "Claude Code Runner"
description: "Complete Claude Code execution with authentication, setup, execution, and results handling"
inputs:
  # Authentication inputs
  llmgw-id:
    description: "LLMGW ID for token generation"
    required: true
  llmgw-secret:
    description: "LLMGW secret for token generation"
    required: true
  llmgw-token-url:
    description: "LLMGW token URL for authentication"
    required: true
  github-token-fallback:
    description: "Fallback GitHub token if App token fails"
    required: false
    default: ""

  # Claude configuration
  model:
    description: "Anthropic model to use"
    required: false
    default: "claude-3-5-sonnet-20241022"
  max-turns:
    description: "Maximum number of turns for Claude"
    required: false
    default: "50000"
  timeout-minutes:
    description: "Timeout for Claude action in minutes"
    required: false
    default: "600"
  trigger-phrase:
    description: "Trigger phrase to activate Claude"
    required: false
    default: "@claude"
  assignee-trigger:
    description: "Assignee trigger name"
    required: false
    default: "claude"

  # Environment and setup
  custom-instructions:
    description: "Custom instructions for Claude"
    required: true
  mcp-config:
    description: "MCP server configuration JSON"
    required: false
    default: ""
  allowed-tools:
    description: "Comma-separated list of allowed tools"
    required: false
    default: "Bash,View,GlobTool,GrepTool,BatchTool,Write"
  setup-commands:
    description: "Setup commands to run before Claude (multiline string)"
    required: false
    default: ""
  continue-on-setup-error:
    description: "Continue if setup commands fail"
    required: false
    default: "false"

  # AWS/Bedrock configuration
  use-bedrock:
    description: "Use AWS Bedrock for Claude"
    required: false
    default: "true"
  aws-region:
    description: "AWS region"
    required: false
    default: ""
  bedrock-base-url:
    description: "Anthropic Bedrock base URL"
    required: false
    default: ""
  small-fast-model:
    description: "Small fast model for Anthropic"
    required: false
    default: ""

outputs:
  auth-token:
    description: "Generated authentication token"
    value: ${{ steps.auth-token.outputs.token }}
  github-token:
    description: "Final GitHub token (App or fallback)"
    value: ${{ steps.auth-config.outputs.github-token }}
  token-expires:
    description: "Token expiration time (if available)"
    value: ${{ steps.auth-token.outputs.token-expires }}
  github-app-token-outcome:
    description: "Outcome of GitHub App token generation"
    value: ${{ steps.github-app-token.outcome }}
  claude-outcome:
    description: "Outcome of Claude Code execution"
    value: ${{ steps.claude-action.outcome }}

runs:
  using: "composite"
  steps:
    # Validate environment and inputs
    - name: Validate Environment
      shell: bash
      run: |
        set -euo pipefail

        # Check required secrets
        if [ -z "${{ inputs.llmgw-id }}" ] || [ -z "${{ inputs.llmgw-secret }}" ] || [ -z "${{ inputs.llmgw-token-url }}" ]; then
          echo "::error::Missing required secrets: LLMGW_ID or LLMGW_SECRET or LLMGW_TOKEN_URL"
          exit 1
        fi

        # Install required tools
        command -v jq >/dev/null 2>&1 || { echo "::error::jq is required but not installed"; exit 1; }
        command -v curl >/dev/null 2>&1 || { echo "::error::curl is required but not installed"; exit 1; }

        echo "โœ… Environment validation passed"

    # Generate custom auth token
    - name: Generate Custom Auth Token
      id: auth-token
      shell: bash
      run: |
        set -euo pipefail

        echo "๐Ÿ” Generating authentication token..."

        # Set up error handling
        cleanup() {
          local exit_code=$?
          echo "๐Ÿงน Cleaning up temporary files..."
          rm -f /tmp/token_response.json 2>/dev/null || true
          if [ $exit_code -ne 0 ]; then
            echo "::error::Authentication failed - check your credentials and endpoint"
          fi
          exit $exit_code
        }
        trap cleanup EXIT

        # Generate token with comprehensive error handling (using Basic auth like original)
        HTTP_CODE=$(curl -s -w "%{http_code}" -o /tmp/token_response.json --fail-with-body \
          --max-time 30 \
          --retry 3 \
          --retry-delay 2 \
          --location "${{ inputs.llmgw-token-url }}" \
          --header 'Content-Type: application/x-www-form-urlencoded' \
          --header "Authorization: Basic $(echo -n ${{ inputs.llmgw-id }}:${{ inputs.llmgw-secret }} | base64 -w0)" \
          --data-urlencode 'grant_type=client_credentials' \
          --data-urlencode 'scope=awsanthropic-readwrite azureopenai-readwrite' \
          2>/dev/null)

        # Check HTTP response code
        if [ "$HTTP_CODE" -ne 200 ]; then
          echo "::error::Authentication failed with HTTP code: $HTTP_CODE"
          if [ -f /tmp/token_response.json ]; then
            echo "::error::Response: $(cat /tmp/token_response.json | head -c 200)"
          fi
          exit 1
        fi

        # Extract and validate token
        if [ ! -f /tmp/token_response.json ]; then
          echo "::error::No response file generated"
          exit 1
        fi

        ANTHROPIC_AUTH_TOKEN=$(jq -r '.access_token // empty' /tmp/token_response.json 2>/dev/null)

        # Validate token format and length
        if [ -z "$ANTHROPIC_AUTH_TOKEN" ] || [ "$ANTHROPIC_AUTH_TOKEN" = "null" ]; then
          echo "::error::Failed to extract access token from response"
          exit 1
        fi

        # Basic token validation
        if [ ${#ANTHROPIC_AUTH_TOKEN} -lt 10 ]; then
          echo "::error::Token appears to be too short (${#ANTHROPIC_AUTH_TOKEN} characters)"
          exit 1
        fi

        # CRITICAL: Mask the token BEFORE any output
        echo "::add-mask::$ANTHROPIC_AUTH_TOKEN"

        # Set outputs
        echo "token=$ANTHROPIC_AUTH_TOKEN" >> $GITHUB_OUTPUT

        # Set token expiry if available
        TOKEN_EXPIRES=$(jq -r '.expires_in // empty' /tmp/token_response.json 2>/dev/null)
        if [ -n "$TOKEN_EXPIRES" ]; then
          echo "::add-mask::$TOKEN_EXPIRES"
          echo "token-expires=$TOKEN_EXPIRES" >> $GITHUB_OUTPUT
        fi

        echo "โœ… Authentication token generated and masked successfully"

        # Clean up response file
        rm -f /tmp/token_response.json

    # Configure authentication
    - name: Configure Authentication
      id: auth-config
      shell: bash
      run: |
        set -euo pipefail

        # Use GitHub App token if available, otherwise use GITHUB_TOKEN
        if [ -n "${{ steps.github-app-token.outputs.token }}" ]; then
          echo "github-token=${{ steps.github-app-token.outputs.token }}" >> $GITHUB_OUTPUT
          echo "โœ… Using GitHub App authentication"
        else
          echo "github-token=${{ inputs.github-token-fallback }}" >> $GITHUB_OUTPUT
          echo "โš ๏ธ  Using fallback GITHUB_TOKEN authentication"
        fi

    # Run setup commands if provided
    - name: Run setup commands
      id: setup-commands
      if: inputs.setup-commands != ''
      shell: bash
      continue-on-error: ${{ inputs.continue-on-setup-error == 'true' }}
      run: ${{ inputs.setup-commands }}

    # Security cleanup
    - name: Security Cleanup
      if: always()
      shell: bash
      run: |
        set -euo pipefail

        echo "๐Ÿงน Performing security cleanup..."

        # Clear any temporary files that might contain sensitive data
        find /tmp -name "*token*" -type f -delete 2>/dev/null || true
        find /tmp -name "*auth*" -type f -delete 2>/dev/null || true
        find /tmp -name "*response*" -type f -delete 2>/dev/null || true

        # Clear environment variables (belt and suspenders approach)
        unset ANTHROPIC_API_KEY 2>/dev/null || true
        unset ANTHROPIC_AUTH_TOKEN 2>/dev/null || true

        echo "โœ… Security cleanup completed"

    # Workflow summary
    - name: Generate Workflow Summary
      if: always()
      shell: bash
      run: |
        echo "## Claude Code Runner Summary" >> $GITHUB_STEP_SUMMARY
        echo "" >> $GITHUB_STEP_SUMMARY
        echo "### Authentication" >> $GITHUB_STEP_SUMMARY
        echo "- **Auth Token**: โœ… Generated" >> $GITHUB_STEP_SUMMARY
        echo "- **Token Expiry**: ${{ steps.auth-token.outputs.token-expires || 'Not provided' }}" >> $GITHUB_STEP_SUMMARY
        echo "- **GitHub Token**: ${{ steps.github-app-token.outcome == 'success' && 'โœ… GitHub App' || 'โš ๏ธ Fallback' }}" >> $GITHUB_STEP_SUMMARY
        echo "" >> $GITHUB_STEP_SUMMARY
        echo "### Configuration" >> $GITHUB_STEP_SUMMARY
        echo "- **Model**: ${{ inputs.model }}" >> $GITHUB_STEP_SUMMARY
        echo "- **Max Turns**: ${{ inputs.max-turns }}" >> $GITHUB_STEP_SUMMARY
        echo "- **Timeout**: ${{ inputs.timeout-minutes }} minutes" >> $GITHUB_STEP_SUMMARY
        echo "- **Bedrock**: ${{ inputs.use-bedrock == 'true' && 'โœ… Enabled' || 'โŒ Disabled' }}" >> $GITHUB_STEP_SUMMARY
        if [ "${{ inputs.use-bedrock }}" = "true" ]; then
          echo "- **AWS Region**: ${{ inputs.aws-region || 'Default' }}" >> $GITHUB_STEP_SUMMARY
        fi
        echo "" >> $GITHUB_STEP_SUMMARY
        echo "### Setup" >> $GITHUB_STEP_SUMMARY
        echo "- **Setup Commands**: ${{ inputs.setup-commands != '' && 'โœ… Executed' || 'โญ๏ธ Skipped' }}" >> $GITHUB_STEP_SUMMARY
        if [ "${{ inputs.setup-commands }}" != "" ]; then
          echo "- **Setup Result**: ${{ steps.setup-commands.outcome || 'Unknown' }}" >> $GITHUB_STEP_SUMMARY
        fi
        echo "- **Security Cleanup**: โœ… Completed" >> $GITHUB_STEP_SUMMARY

    # Execute Claude Code Action
    - name: Execute Claude Code Action
      id: claude-action
      uses: anthropics/claude-code-action@beta
      with:
        custom_instructions: ${{ inputs.custom-instructions }}
        mcp_config: ${{ inputs.mcp-config }}
        allowed_tools: ${{ inputs.allowed-tools }}
        trigger_phrase: ${{ inputs.trigger-phrase }}
        assignee_trigger: ${{ inputs.assignee-trigger }}
        timeout_minutes: ${{ inputs.timeout-minutes }}
        github_token: ${{ steps.auth-config.outputs.github-token }}
        use_bedrock: ${{ inputs.use-bedrock }}
        model: ${{ inputs.model }}
        max_turns: ${{ inputs.max-turns }}
        # Use claude_env for custom environment variables
        claude_env: |
          ANTHROPIC_BEDROCK_BASE_URL: ${{ inputs.bedrock-base-url }}
          ANTHROPIC_SMALL_FAST_MODEL: ${{ inputs.small-fast-model }}
          AWS_REGION: ${{ inputs.aws-region }}
          GITHUB_REPOSITORY: ${{ github.repository }}
          GITHUB_EVENT_NAME: ${{ github.event_name }}
          GITHUB_ACTOR: ${{ github.actor }}
          ANTHROPIC_AUTH_TOKEN: ${{ steps.auth-token.outputs.token }}
          DISABLE_TELEMETRY: 1
      continue-on-error: true

    # Handle Claude results
    - name: Handle Claude Results
      if: always()
      shell: bash
      run: |
        set -euo pipefail

        echo "๐Ÿ” Processing Claude action results..."

        # Check if Claude action succeeded
        if [ "${{ steps.claude-action.outcome }}" = "success" ]; then
          echo "โœ… Claude Code action completed successfully"
        elif [ "${{ steps.claude-action.outcome }}" = "failure" ]; then
          echo "โŒ Claude Code action failed"

          # Create error summary
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "### โŒ Claude Execution Failed" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "The Claude Code action encountered an error. Common causes:" >> $GITHUB_STEP_SUMMARY
          echo "- Authentication issues" >> $GITHUB_STEP_SUMMARY
          echo "- Network connectivity problems" >> $GITHUB_STEP_SUMMARY
          echo "- Model availability issues" >> $GITHUB_STEP_SUMMARY
          echo "- Rate limiting" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "Please check the workflow logs for detailed error information." >> $GITHUB_STEP_SUMMARY
        else
          echo "โš ๏ธ  Claude Code action was cancelled or skipped"
        fi

    # Add execution summary
    - name: Add Claude Execution Summary
      if: always()
      shell: bash
      run: |
        echo "" >> $GITHUB_STEP_SUMMARY
        echo "### Claude Execution Details" >> $GITHUB_STEP_SUMMARY
        echo "" >> $GITHUB_STEP_SUMMARY

        # Event information
        echo "#### Event Information" >> $GITHUB_STEP_SUMMARY
        echo "- **Trigger**: ${{ github.event_name }}" >> $GITHUB_STEP_SUMMARY
        echo "- **Repository**: ${{ github.repository }}" >> $GITHUB_STEP_SUMMARY
        echo "- **Actor**: @${{ github.actor }}" >> $GITHUB_STEP_SUMMARY
        echo "" >> $GITHUB_STEP_SUMMARY

        # Execution status
        echo "#### Execution Status" >> $GITHUB_STEP_SUMMARY
        if [ "${{ steps.claude-action.outcome }}" = "success" ]; then
          echo "- **Claude Action**: โœ… Success" >> $GITHUB_STEP_SUMMARY
        elif [ "${{ steps.claude-action.outcome }}" = "failure" ]; then
          echo "- **Claude Action**: โŒ Failed" >> $GITHUB_STEP_SUMMARY
        else
          echo "- **Claude Action**: โš ๏ธ ${{ steps.claude-action.outcome }}" >> $GITHUB_STEP_SUMMARY
        fi
        echo "- **Model Used**: ${{ inputs.model }}" >> $GITHUB_STEP_SUMMARY
        echo "- **Max Turns**: ${{ inputs.max-turns }}" >> $GITHUB_STEP_SUMMARY
        echo "- **Workflow Status**: ${{ job.status }}" >> $GITHUB_STEP_SUMMARY

        # Add timestamp
        echo "" >> $GITHUB_STEP_SUMMARY
        echo "#### Timestamp" >> $GITHUB_STEP_SUMMARY
        echo "- **Completed at**: $(date -u '+%Y-%m-%d %H:%M:%S UTC')" >> $GITHUB_STEP_SUMMARY