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
mkdir -p .agents/skills/headless-claude-code-script-for-ci-claude-p-task-with-exit-codes && curl -sL "https://prompts-gpt.com/prompts/headless-claude-code-script-for-ci-claude-p-task-with-exit-codes-and-output-checks/raw" -o .agents/skills/headless-claude-code-script-for-ci-claude-p-task-with-exit-codes/SKILL.mdAct 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 like this
Should This Be an Agent? Single Agent, Workflow or Plain Script Decision
Scores a task you want to automate on variability, tool needs, error tolerance and cost, then recommends a plain script, a fixed LLM workflow, a single agent or multi-agent, with the cheapest first build.
Claude Code Hook Writer: Auto-Run Lint, Typecheck or Tests After Edits
Produces a PostToolUse hook entry for settings.json, the script it calls and a concrete procedure to test that the check runs after file edits.
Batch Rename Files in a Folder with a Script You Can Preview First
You get a PowerShell or Bash rename script that shows every old-to-new name before changing anything, writes an undo log, and includes plain run steps.
Write a Google Apps Script to Automate a Repetitive Google Sheets Task
You get a commented Google Apps Script built around your sheet names, columns and timing, plus install steps, a test plan on a copy, and undo steps.
Write a Google Apps Script to Automate a Sheets Task with Dry-Run Mode
Get a working Google Apps Script for your Sheets task, with trigger setup, the permission screens you will see, and a dry-run mode that logs changes first.
Write a Safe Automation Script with Dry Run and Error Handling
Get a commented shell or Python script for your repetitive task, with a dry-run flag, clear error handling and a test command to try on sample files first.
- Codex Cloud Environment Setup: Setup Script, Env Vars and a Smoke Test
- Repetitive Task to Zapier, n8n or Make Automation Spec
- Codex Exec CI Fixer: Diagnose, Minimal Fix, Rerun, Stop After 3 Attempts
- Build an Excel Formula from a Plain-English Description of What You Need
- Check If You Can Scrape a Website and Plan a Polite Scraper First
- Combine and Clean Multiple CSV or Excel Files into One Table with Power Query