Skip to content

Lab 3.6: The Butler

Module: 3.6 - CI/CD & Headless Mode | ← SlidesDuration: 30 minutes Sample Project: hackathon-starter

Learning Objectives

By the end of this lab, you will be able to:

  • Use Claude Code in headless mode for automation
  • Create scripts that leverage Claude for repetitive tasks
  • Integrate Claude into CI/CD workflows
  • Automate common development tasks

What is Headless Mode?

Headless mode runs Claude without an interactive session:

bash
claude -p "Your prompt here"

Use it for:

  • Automated code fixes
  • CI/CD integrations
  • Scheduled tasks
  • Scripted workflows

Setup

bash
# Navigate to the sample project
cd sample-projects/hackathon-starter

# Verify Claude works in headless mode
claude -p "What is 2 + 2?"

Tasks

Task 1: Create a Lint Fixer Script (10 min)

Create a script that automatically fixes linting errors.

Step 1: Create the script

Create scripts/fix-lint.sh:

bash
#!/bin/bash
# Automated lint fixer using Claude

# Run linter and capture errors
LINT_OUTPUT=$(npm run lint 2>&1)

if [ $? -eq 0 ]; then
    echo "No linting errors found!"
    exit 0
fi

echo "Linting errors found. Asking Claude to fix..."

# Ask Claude to fix the errors
claude -p "Fix these linting errors in the codebase:

$LINT_OUTPUT

Make the minimum changes necessary to fix each error."

echo "Done. Re-running linter to verify..."
npm run lint

Step 2: Make it executable

bash
chmod +x scripts/fix-lint.sh

Step 3: Test it

bash
./scripts/fix-lint.sh

Success criteria:

  • [ ] Script created
  • [ ] Script runs successfully
  • [ ] Linting errors fixed (if any existed)

Task 2: Create a Dependency Updater (10 min)

Create a script that updates dependencies and runs tests.

Create scripts/update-deps.sh:

bash
#!/bin/bash
# Automated dependency updater

echo "Checking for outdated dependencies..."
npm outdated

echo ""
echo "Updating dependencies..."
npm update

echo ""
echo "Running tests to verify..."
npm test

if [ $? -eq 0 ]; then
    echo "All tests pass! Safe to commit updates."
else
    echo "Tests failed after update. Asking Claude for help..."
    claude -p "npm update broke some tests. Analyze the test failures and suggest fixes."
fi

Test it:

bash
chmod +x scripts/update-deps.sh
./scripts/update-deps.sh

Success criteria:

  • [ ] Script created
  • [ ] Dependencies updated
  • [ ] Tests run automatically

Task 3: Create a PR Review Helper (10 min)

Create a script that prepares a PR summary.

Create scripts/pr-summary.sh:

bash
#!/bin/bash
# Generate PR summary from git diff

if [ -z "$1" ]; then
    BASE_BRANCH="main"
else
    BASE_BRANCH="$1"
fi

echo "Generating PR summary comparing to $BASE_BRANCH..."

DIFF=$(git diff $BASE_BRANCH...HEAD --stat)
FILES_CHANGED=$(git diff $BASE_BRANCH...HEAD --name-only)

claude -p "Generate a pull request summary for these changes:

Files changed:
$FILES_CHANGED

Diff stats:
$DIFF

Create a summary with:
1. One-line description
2. Key changes (bullet points)
3. Testing notes
4. Any risks or concerns"

Test it:

bash
chmod +x scripts/pr-summary.sh
./scripts/pr-summary.sh main

Success criteria:

  • [ ] Script created
  • [ ] Generates meaningful PR summary
  • [ ] Can be used before creating PRs

Integration Ideas

GitHub Actions Example

yaml
# .github/workflows/claude-lint-fix.yml
name: Auto-fix Linting

on:
  pull_request:
    types: [opened, synchronize]

jobs:
  lint-fix:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: '18'

      - name: Install dependencies
        run: npm ci

      - name: Run lint fix script
        env:
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
        run: ./scripts/fix-lint.sh

      - name: Commit fixes
        run: |
          git config user.name "Claude Bot"
          git config user.email "claude@example.com"
          git add -A
          git diff --staged --quiet || git commit -m "Auto-fix linting errors"
          git push

Pre-commit Hook Example

bash
# .git/hooks/pre-commit
#!/bin/bash

# Run tests before allowing commit
npm test
if [ $? -ne 0 ]; then
    echo "Tests failed. Commit blocked."
    echo "Would you like Claude to help fix the issues? (y/n)"
    read answer
    if [ "$answer" = "y" ]; then
        claude -p "The tests are failing. Help me fix them."
    fi
    exit 1
fi

Headless Mode Options

OptionDescriptionExample
-pSingle promptclaude -p "Fix this bug"
--printPrint only, no interactionclaude --print -p "Explain X"
--output-format jsonJSON outputclaude -p "..." --output-format json

Tips for Automation

  1. Keep prompts specific - Vague prompts = unpredictable results
  2. Validate outputs - Always run tests after automated changes
  3. Use guard rails - Check exit codes, verify changes are safe
  4. Log everything - Capture output for debugging

Common Automation Use Cases

  • Lint fixing - Auto-fix code style issues
  • Test failures - Analyze and suggest fixes
  • Dependency updates - Update and verify
  • Documentation - Generate changelogs, PR descriptions
  • Code review - Pre-review before human review
  • Issue triage - Label and categorize issues

Stretch Goals

If you finish early:

  1. Create a commit message generator script
  2. Add error handling to your scripts
  3. Create a changelog generator from git log

Deliverables

At the end of this lab, you should have:

  1. At least 2 automation scripts
  2. Understanding of headless mode
  3. Ideas for CI/CD integration
  4. Experience with automated Claude workflows

Next Steps

After completing this lab, move on to Lab 3.8: End-to-End Modernization - the Final Capstone.

Claude for Coders Training Course