Skip to content

Task Automation with Claude Code

Every Friday you manually update the changelog, bump the version in three files, regenerate the API docs, run the test suite, tag the release, and push. It takes 45 minutes if nothing goes wrong. Last week you forgot to update the version in package.json and the Docker build picked up the old tag. The week before, the changelog missed two PRs because you forgot to check the date range. These are not hard tasks — they are tedious tasks, and tedious tasks are where mistakes live.

  • A Claude Code workflow for automating release processes, code generation, bulk refactors, and project maintenance from the terminal
  • Copy-paste prompts for building custom slash commands, headless scripts, and hook-based automations that run without supervision
  • A practical understanding of when to use Claude Code interactively versus headlessly, and how to chain multiple operations together

Claude Code supports custom commands stored in .claude/commands/ that you invoke with /. This is the simplest and most powerful automation entry point.

After creating the command, invoke it:

/release

Or with an override:

/release major

Claude Code reads the command file, follows the instructions, and executes each step. If the test suite fails at step 4, it stops and reports the failure rather than continuing to tag a broken release.

For automations that run in CI or cron jobs, Claude Code’s headless mode (-p flag) runs without an interactive terminal.

Terminal window
# Generate a daily dependency report
claude -p "Check our package.json for outdated dependencies. List any that are more than one major version behind, any with known vulnerabilities, and any where the latest version has breaking changes. Output a markdown summary." > reports/deps-$(date +%Y-%m-%d).md
# Auto-fix lint errors
claude -p "Run our linter and fix all auto-fixable errors. For errors that cannot be auto-fixed, add a TODO comment with the rule name."
# Generate API docs from source
claude -p "Read all our API route handlers and generate OpenAPI 3.1 specification in YAML format. Include request/response schemas inferred from the TypeScript types, path parameters, query parameters, and error responses." > docs/openapi.yml

Claude Code hooks run automatically at specific lifecycle points. They are defined in .claude/settings.json and execute without prompting.

Hooks are particularly valuable for enforcing team standards. A hook that runs eslint --fix after every file edit means Claude Code’s output always matches your code style without you asking.

Some tasks touch dozens or hundreds of files. Claude Code handles these systematically when given clear instructions.

We are migrating from moment.js to date-fns across our entire codebase. For every file that imports moment: 1) replace the import with the equivalent date-fns functions, 2) update every moment() call to the date-fns equivalent (format, parse, add, subtract, diff, isAfter, isBefore), 3) update date format strings from moment format (YYYY-MM-DD) to date-fns format (yyyy-MM-dd), 4) run the tests for each modified file to verify the change. Give me a summary of all changes at the end.

Claude Code processes files one at a time, running tests after each change. If a test fails, it fixes the issue before moving on, rather than breaking twenty files and leaving you to clean up.

Claude Code integrates with Unix pipes, making it part of your shell toolkit.

Terminal window
# Analyze build errors
npm run build 2>&1 | claude -p "Explain each error, suggest a fix, and apply the fixes to the source files"
# Convert CSV to SQL inserts
cat users.csv | claude -p "Convert this CSV data to SQL INSERT statements for a users table with columns: id, email, name, created_at. Use parameterized values for safety." > seed.sql
# Summarize git changes for standup
git log --oneline --since="yesterday" | claude -p "Summarize these commits as 3-5 bullet points suitable for a standup update. Group related changes."

The piping pattern is powerful because you can chain Claude Code with standard Unix tools. The input is the context, the prompt is the transformation, and the output can be captured or piped further.

Combine Claude Code headless mode with cron for recurring tasks.

Terminal window
# Add to crontab: weekly dependency check every Monday at 9 AM
0 9 * * 1 cd /path/to/project && claude -p "Check for outdated dependencies and security vulnerabilities. If any critical vulnerabilities are found, create a GitHub issue with the details and tag it 'security'." --output-format json >> /var/log/claude-audit.log

For teams already using Claude Code GitHub Actions, the claude-code-action provides a more integrated approach:

.github/workflows/claude-review.yml
name: Claude Review
on:
pull_request:
types: [opened, synchronize]
jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: anthropics/claude-code-action@v1
with:
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
prompt: "Review this PR for security issues, missing tests, and code quality. Be specific about file and line numbers."
claude_args: "--max-turns 5"

Combine multiple automations into a single maintenance command that keeps your project healthy.

Create a custom command at .claude/commands/maintain.md that performs weekly project maintenance: 1) check for and update patch-level dependency versions, 2) regenerate TypeScript types from our database schema, 3) update the API documentation from source code, 4) find and remove unused imports and dead code, 5) check for TODOs older than 30 days and list them, 6) run the full test suite, 7) generate a maintenance report with a summary of changes made. Commit all changes as "chore: weekly maintenance".

Headless mode makes unwanted changes. When running claude -p with auto-accept, Claude might edit files you did not intend. Scope the automation tightly: “Only modify files in the src/api/ directory. Do not touch tests, config, or documentation.” For extra safety, run headless automations in a git worktree or on a branch so you can review changes before merging.

A hook runs on every edit and slows down the session. Hooks that run linting or type-checking on every file edit add latency. Use conditional hooks: “Only run the TypeScript check hook on files that end in .ts or .tsx, and skip files in the node_modules or dist directories.”

The bulk refactor introduces subtle bugs. Large-scale find-and-replace across many files can produce code that compiles but behaves differently. Always include “run the tests for each modified file” in bulk refactor prompts. If your test coverage is low, ask Claude to generate tests first: “Before migrating moment.js to date-fns, generate test cases for every function that uses moment. Then do the migration and verify the tests pass.”

The cron job fails silently. Headless Claude Code returns exit code 0 even when it could not complete the task. Capture the output and check for error indicators: pipe to --output-format json and check the result field, or have the automation write a success marker file and alert if the file is not updated.

Custom commands conflict with built-in commands. If you name a command /review, it might conflict with built-in skills. Check available commands with /help and use descriptive names like /release-prep or /weekly-audit.