Headless Claude Code Script for CI: claude -p Task With Exit Codes and Output Checks

Writes a CI step that runs Claude Code non-interactively with claude -p, scoped tools, a turn limit and JSON output, and fails safely on errors, with a dry-run mode and secrets kept out of the repo.

You getthe workflow file in a code block, a table of each flag and why it is set, then three ways the step can fail and how it handles each. Flags change between releases, so tell me to…

The prompt

Prompt
Act as a CI engineer who runs Claude Code headless. Write a CI step that runs claude -p for this job: {task for Claude to do in CI}. If I have not said which CI system (GitHub Actions, GitLab, other) and what triggers it, ask me first.

Requirements:
1. Call claude -p with a short, scoped prompt, --output-format json, a low --max-turns, and --allowedTools limited to what the task needs. Default to read-only tools (Read, Grep, Glob). Add Edit or specific Bash commands only if the task must change files, and name each one.
2. Read ANTHROPIC_API_KEY from the CI secret store, never from the repo, and never print it.
3. Check the exit code. On a non-zero exit or JSON that does not parse, fail the step with a clear message.
4. Fail if is_error is true; use the result field only after checking it is not empty.
5. Add a DRY_RUN input that prints what would change without writing files, commenting or pushing.
6. Set a job timeout. If it is GitHub Actions, say in one line whether the official anthropics/claude-code-action would be simpler for this job.

Output format: the workflow file in a code block, a table of each flag and why it is set, then three ways the step can fail and how it handles each. Flags change between releases, so tell me to check them against claude --help and the current Claude Code docs.

Paste into Claude Code or Claude and describe the job, for example triaging lint failures or labeling new issues. Test with DRY_RUN on a branch before letting it write.

Keep exploring

More in Claude Code