Module 5: Advanced TechniquesLesson 5 of 6
0%
Lesson 5ยท4 min

Hooks and Automation

Automate tasks with Claude Code hooks

Hooks and Automation

Hooks let you automate actions based on events. Run tasks automatically!

What Are Hooks?

Hooks are scripts that run when something happens:

  • pre-commit: Before you commit code
  • pre-push: Before pushing to GitHub
  • post-save: After Claude saves a file
  • on-error: When something fails

Setting Up Git Hooks

Terminal
$ mkdir -p .husky
$ npx husky init
โ–Œ

Pre-commit Hook

Terminal
$ cat .husky/pre-commit
#!/bin/sh
npm run lint
npm test
โ–Œ

Now tests run automatically before every commit!

Tip

Pre-commit hooks catch bugs before they enter your codebase.

Claude Code Hooks

Configure hooks in .claude/hooks.json:

Terminal
$ cat .claude/hooks.json
{
"hooks": {
"after-edit": {
"description": "After Claude edits a file",
"actions": [
"Run prettier on the edited file",
"If it's a component, verify it exports correctly"
]
},
"before-commit": {
"description": "Before committing changes",
"actions": [
"Run npm test",
"Check for console.log statements",
"Verify no TODO comments in committed code"
]
}
}
}
โ–Œ

npm Scripts

Combine scripts in package.json:

json
{
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "test": "jest",
    "lint": "eslint .",
    "format": "prettier --write .",
    "check": "npm run lint && npm test",
    "deploy": "npm run check && vercel --prod"
  }
}
Terminal
# One command to check everything
$ npm run check
# One command to deploy
$ npm run deploy
โ–Œ

Aussie Note

Automation is like having a robot assistant check your work. Set it up once, benefit forever! ๐Ÿค–

CI/CD with GitHub Actions

Terminal
$ mkdir -p .github/workflows
$ cat .github/workflows/ci.yml
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- run: npm install
- run: npm test
โ–Œ

Now GitHub runs tests automatically on every push!

Having Claude Set Up Automation

Terminal
> Set up GitHub Actions to run tests on every PR
Claude: I'll create a CI workflow:
๐Ÿ“„ Create: .github/workflows/ci.yml
The workflow will:
1. Run on push and pull_request
2. Install dependencies
3. Run linting
4. Run tests
5. Report status on the PR
Proceed? [Y/n]
โ–Œ

Common Automation Patterns

TriggerAutomation
PR openedRun tests, check types
Push to mainDeploy to staging
Tag createdDeploy to production
Schedule (cron)Run security scan
Issue createdLabel by keywords

Warning

Start with simple automation. Add more as you get comfortable!

Key Takeaways

  • Hooks automate tasks on events
  • Use pre-commit hooks to catch bugs early
  • npm scripts combine common commands
  • GitHub Actions provide CI/CD
  • Start simple, expand over time