5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Claude Code GitHub Actionsが便利だしBedrockでも使えるし

5
Posted at

Claude Code GitHub Actionsを今日、改めて導入したんですが、めっちゃ便利!

ドキュメントはこちら。

ざっと図にするとこんな感じ。

image.png

じつは、これ、AnthropicのAPIだけじゃなく、AmazonBedrockでも使えます!

知らなかった〜

やり方

  1. AWS側)GitHub の OIDC プロバイダを作る

    aws iam create-open-id-connect-provider \
      --url https://token.actions.githubusercontent.com \
      --client-id-list sts.amazonaws.com
    

    1アカウントにつき1つしか作れないので、注意

  2. AWS側)IAMロールを作る

    信頼ポリシーをJSONで作成

    • <ACCOUNT_ID>は、aws sts get-caller-identity --query Account --output textで確認できます
    • <sub_claim_prefix>:*は、gh api repos/<OWNER>/<REPO>/actions/oidc/customization/subのレスポンスのsub_claim_prefixです(repo:OWNER@9999999/REPO@9999999999 みたいな)
    trust.json
    {
        "Version": "2012-10-17",
        "Statement": [{
            "Effect": "Allow",
            "Principal": {"Federated": "arn:aws:iam::<ACCOUNT_ID>:oidc-provider/token.actions.githubusercontent.com"},
            "Action": "sts:AssumeRoleWithWebIdentity",
            "Condition": {
            "StringEquals": {"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"},
            "StringLike":   {"token.actions.githubusercontent.com:sub": "<sub_claim_prefix>:*"}
            }
        }]
    }
    

    IAMロールを作成

    aws iam create-role --role-name github-claude-code-action \
      --assume-role-policy-document file://trust.json
    

    ポリシーのJSONを作成

    policy.json
    {
        "Version": "2012-10-17",
        "Statement": [{
            "Effect": "Allow",
            "Action": [
            "bedrock:InvokeModel",
            "bedrock:InvokeModelWithResponseStream",
            "bedrock:ListInferenceProfiles",
            "bedrock:GetInferenceProfile"
            ],
            "Resource": "*"
        }]
    }
    

    IAMロールにポリシーをアタッチ

    aws iam put-role-policy --role-name github-claude-code-action \
      --policy-name bedrock-invoke --policy-document file://policy.json
    
  3. GitHub側)IAMロールのARNをGitHubのシークレットに登録する

    <OWNER>/<REPO>を対象のGitHubリポジトリの内容に更新してね

    ARN=$(aws iam get-role --role-name github-claude-code-action --query Role.Arn --output text)
    gh secret set AWS_ROLE_TO_ASSUME --repo <OWNER>/<REPO> --body "$ARN"
    
  4. GitHub側)ActionsのワークフローYAMLを作成する

    リポジトリ内に.github/workflows/claude.ymlとして作成

    .github/workflows/claude.yml
    name: Claude Code
    on:
      issue_comment:
        types: [created]
      pull_request_review_comment:
        types: [created]
      issues:
        types: [opened]
    
    jobs:
      claude:
        if: |
          (github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude')) ||
          (github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude')) ||
          (github.event_name == 'issues' && (contains(github.event.issue.body, '@claude') || contains(github.event.issue.title, '@claude')))
        runs-on: ubuntu-latest
        permissions:
          contents: write
          pull-requests: write
          issues: write
          id-token: write
        steps:
          - uses: actions/checkout@v6
            with:
              fetch-depth: 1
    
          - uses: aws-actions/configure-aws-credentials@v4
            with:
              role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
              aws-region: us-east-1
    
          - uses: anthropics/claude-code-action@v1
            with:
              use_bedrock: "true"
              claude_args: |
                --model global.anthropic.claude-sonnet-5-5
                --max-turns 10
    
  5. GitHub側)Claude GitHub Appをインストールする

    https://github.com/apps/claude を開いて、対象のリポジトリにアプリをインストールします

これで完成デス


Issueの作成、Issueへのコメント、PRへのコメントに「@claude」が含まれる場合にclaudeが動作します。

5
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?