Skip to content

Commit d58ecbb

Browse files
scotthavirdclaude
andauthored
fix(ci): skip Claude workflows cleanly when ANTHROPIC_API_KEY is unset (#7)
Both claude.yml (@claude mention responder) and claude-review.yml (auto-PR review) invoke anthropics/claude-code-action@v1, which fails with an opaque environment-validation error when the secret is missing. A fresh fork of this template would then see red CI on every PR until the maintainer added the secret. Refactor both workflows into a two-job pattern: - A check-secret gate job exposes whether ANTHROPIC_API_KEY is set via a job output (since `if: ${{ secrets.X != '' }}` is not allowed at job level). - The downstream responder/review job is gated on that output, so it is skipped (not failed) when the secret is absent. - A ::notice:: annotation tells the maintainer exactly which secret to add and where. Behavior is unchanged when the secret IS set. Refs #6 AI-Tool: claude-code Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 0e4b63c commit d58ecbb

3 files changed

Lines changed: 55 additions & 5 deletions

File tree

.github/workflows/claude-review.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,31 @@ on:
44
pull_request:
55
types: [opened, synchronize]
66

7+
# Two-job pattern: a tiny gate job exposes whether ANTHROPIC_API_KEY is set
8+
# (since `if: ${{ secrets.X != '' }}` is not allowed at job level), and the
9+
# review job is gated on that. When the secret is missing, the review job
10+
# is skipped — not failed — so a fresh fork of this template doesn't get
11+
# red CI before the maintainer adds the key.
712
jobs:
13+
check-secret:
14+
runs-on: ubuntu-latest
15+
outputs:
16+
has_key: ${{ steps.check.outputs.has_key }}
17+
steps:
18+
- id: check
19+
env:
20+
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
21+
run: |
22+
if [ -n "$ANTHROPIC_API_KEY" ]; then
23+
echo "has_key=true" >> "$GITHUB_OUTPUT"
24+
else
25+
echo "has_key=false" >> "$GITHUB_OUTPUT"
26+
echo "::notice::ANTHROPIC_API_KEY is not set in repo Actions secrets — skipping Claude review. Add the secret in Settings → Secrets and variables → Actions to enable."
27+
fi
28+
829
review:
30+
needs: check-secret
31+
if: needs.check-secret.outputs.has_key == 'true'
932
runs-on: ubuntu-latest
1033
permissions:
1134
contents: read

.github/workflows/claude.yml

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,35 @@ on:
1010
pull_request_review:
1111
types: [submitted]
1212

13+
# Two-job pattern: a tiny gate job exposes whether ANTHROPIC_API_KEY is set,
14+
# so the @claude responder is skipped (not failed) when the secret is
15+
# missing. See claude-review.yml for the same pattern.
1316
jobs:
17+
check-secret:
18+
runs-on: ubuntu-latest
19+
outputs:
20+
has_key: ${{ steps.check.outputs.has_key }}
21+
steps:
22+
- id: check
23+
env:
24+
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
25+
run: |
26+
if [ -n "$ANTHROPIC_API_KEY" ]; then
27+
echo "has_key=true" >> "$GITHUB_OUTPUT"
28+
else
29+
echo "has_key=false" >> "$GITHUB_OUTPUT"
30+
echo "::notice::ANTHROPIC_API_KEY is not set in repo Actions secrets — skipping @claude responder. Add the secret in Settings → Secrets and variables → Actions to enable."
31+
fi
32+
1433
claude:
34+
needs: check-secret
1535
if: |
16-
(github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude')) ||
17-
(github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude')) ||
18-
(github.event_name == 'pull_request_review' && contains(github.event.review.body, '@claude')) ||
19-
(github.event_name == 'issues' && (contains(github.event.issue.body, '@claude') || contains(github.event.issue.title, '@claude')))
36+
needs.check-secret.outputs.has_key == 'true' && (
37+
(github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude')) ||
38+
(github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude')) ||
39+
(github.event_name == 'pull_request_review' && contains(github.event.review.body, '@claude')) ||
40+
(github.event_name == 'issues' && (contains(github.event.issue.body, '@claude') || contains(github.event.issue.title, '@claude')))
41+
)
2042
runs-on: ubuntu-latest
2143
permissions:
2244
contents: write

docs/integrations.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ issues, and review comments.
1010
`.github/workflows/claude-review.yml` — runs a review pass on every new
1111
PR or push to an open PR.
1212

13-
Both require `ANTHROPIC_API_KEY` in the repo's Actions secrets. See the
13+
Both require `ANTHROPIC_API_KEY` in the repo's Actions secrets. **If the
14+
secret is not set, both workflows skip cleanly** (a `check-secret` gate
15+
job emits a workflow notice and the responder/review job is marked
16+
*skipped*, not failed) — so a fresh fork doesn't get red CI on every PR
17+
before you've configured the key. Add the secret at *Settings → Secrets
18+
and variables → Actions* to enable. See the
1419
[Claude Code GitHub Actions doc](https://code.claude.com/docs/en/github-actions).
1520

1621
## GitLab CI/CD

0 commit comments

Comments
 (0)