anthropics / anthropics/claude-code-action

Hooks Support in Claude Code GitHub Action

Ouverte
#585 3 commentaires 1 réaction 0 personnes assignées Voir sur GitHub
documentation p3 question
Langage dominant
TypeScript
Étoiles
8.9k
Forks
2.1k
Merge moyen
3 j 9 h
PR mergées (30 j)
10

Description

# Hooks Support in Claude Code GitHub Action

## Summary

I'm trying to understand if Claude Code hooks (as documented in the [hooks reference](https://docs.claude.com/en/docs/claude-code/hooks)) are supported when using the `anthropics/claude-code-action` in GitHub Actions workflows.

## Context

I have a Gradle project and want to ensure that Claude automatically validates builds and tests after making changes. The hooks system seems perfect for this use case, as it would allow Claude to:

1. Automatically run build validation after each file edit (`PostToolUse` hooks)
2. Get feedback on compilation/test failures
3. Automatically fix any issues it introduces
4. Ensure the build stays healthy throughout the process

## Questions

1. **Do hooks work in GitHub Actions?** Are `.claude/settings.json` files with hooks configuration respected when using the GitHub Action?

2. **Can hook scripts execute?** Would hook scripts (e.g., bash scripts in `/scripts/`) be able to run in the GitHub Actions runner environment?

3. **Alternative approaches?** If hooks aren't supported, what's the recommended way to achieve similar build validation behavior in GitHub Actions?

Also, please update the documentation on this repo and in https://docs.claude.com/en/docs/claude-code/github-actions to reflect that.

## Current Workflow

```yaml
name: Claude Code Test

on:
workflow_dispatch:

jobs:
claude:
runs-on: glovo-shared-small-arm
permissions:
contents: write
pull-requests: write
issues: write
id-token: write
actions: read # Required for Claude to read CI results on PRs
steps:
- name: Checkout repository
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
uses: actions/checkout@v4
with:
fetch-depth: 2

- name: Run Claude Code
env:
ANTHROPIC_BASE_URL: ${{ vars.ANTHROPIC_BASE_URL }}
ANTHROPIC_MODEL: ${{ vars.ANTHROPIC_MODEL }}
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
id: claude
uses: anthropics/claude-code-action@v1
with:
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
use_commit_signing: true
claude_args: --model ${{ vars.ANTHROPIC_MODEL }} --allowedTools Bash,Edit,MultiEdit,Read,Task,TodoWrite,WebFetch,WebSearch,Write --permission-mode acceptEdits
prompt: |
Modify the README file to add a funny story about developers and the size of the pull request.

- name: Extract Claude results and cost
id: claude_results
run: |
# Parse Claude's output to extract the result and cost
echo "claude_summary<> $GITHUB_OUTPUT
echo '${{ steps.claude.outputs.result }}' | jq -r '.result // .message // .' >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT

echo "claude_cost<> $GITHUB_OUTPUT
echo '${{ steps.claude.outputs.result }}' | jq -r '.total_cost_usd // "N/A"' >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT

echo "claude_input_tokens<> $GITHUB_OUTPUT
echo '${{ steps.claude.outputs.result }}' | jq -r '.usage.input_tokens // 0' >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT

echo "claude_output_tokens<> $GITHUB_OUTPUT
echo '${{ steps.claude.outputs.result }}' | jq -r '.usage.output_tokens // 0' >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT

echo "claude_cache_creation_tokens<> $GITHUB_OUTPUT
echo '${{ steps.claude.outputs.result }}' | jq -r '.usage.cache_creation_input_tokens // 0' >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT

echo "claude_cache_read_tokens<> $GITHUB_OUTPUT
echo '${{ steps.claude.outputs.result }}' | jq -r '.usage.cache_read_input_tokens // 0' >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT

- name: Remove Claude output.txt if it exists
run: |
if [ -f output.txt ]; then
git rm -f --ignore-unmatch output.txt
rm -f output.txt
fi

- name: Create PR
id: cpr
uses: peter-evans/create-pull-request@v7
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: 'Claude Code test: Adds funny story about developers and the size of the pull request'
committer: Claude Code[bot]
author: Claude Code[bot]
title: 'Claude Code test: Adds funny story about developers and the size of the pull request'
labels: feature-toggle-slayer
body: |
> [!WARNING]
> This pull request was created by Claude Code AI agent, please review carefully and merge or reject as you see fit.

## Summary

${{ steps.claude_results.outputs.claude_summary }}

## Claude Code Execution Details

| Metric | Value |
|--------|--------|
| **Total Cost** | ${{ steps.claude_results.outputs.claude_cost }} USD |
| **Input Tokens** | ${{ steps.claude_results.outputs.claude_input_tokens }} |
| **Output Tokens** | ${{ steps.claude_results.outputs.claude_output_tokens }} |
| **Cache Creation Tokens** | ${{ steps.claude_results.outputs.claude_cache_creation_tokens }} |
| **Cache Read Tokens** | ${{ steps.claude_results.outputs.claude_cache_read_tokens }} |
| **Session ID** | ${{ steps.claude.outputs.session_id || 'N/A' }} |

---

**Workflow run**: [${{ github.run_id }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
```

Thanks for any clarification on this!

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.