Git Hooks and Automation
Learn how to use Git hooks to automate your development workflows.
I
InSkillCoach
· min
Git Hooks and Automation
Learn how to use Git hooks to automate your development workflows.
Git Hooks Basics
Configuration
# Enable hooks
git config core.hooksPath .git/hooks
# Create a pre-commit hook
touch .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
Common Hooks
# pre-commit
#!/bin/sh
# Runs before each commit
echo "Vérification du code..."
# post-commit
#!/bin/sh
# Runs after each commit
echo "Notification envoyée..."
# pre-push
#!/bin/sh
# Runs before each push
echo "Tests en cours..."
# post-merge
#!/bin/sh
# Runs after each merge
echo "Mise à jour des dépendances..."
Practical Examples
Code Checks
# pre-commit
#!/bin/sh
# Check formatting
echo "Vérification du formatage..."
npm run lint
# Run tests
echo "Exécution des tests..."
npm test
# Check types
echo "Vérification des types..."
npm run type-check
Message Validation
# commit-msg
#!/bin/sh
# Check the message format
commit_msg=$(cat "$1")
if ! echo "$commit_msg" | grep -qE "^(feat|fix|docs|style|refactor|test|chore)(\(.+\))?: .+"; then
echo "Format de message invalide"
echo "Utilisez: type(scope): description"
exit 1
fi
Automatic Deployment
# post-push
#!/bin/sh
# Deploy to staging
if [ "$(git rev-parse --abbrev-ref HEAD)" = "develop" ]; then
echo "Déploiement en staging..."
npm run deploy:staging
fi
# Deploy to production
if [ "$(git rev-parse --abbrev-ref HEAD)" = "main" ]; then
echo "Déploiement en production..."
npm run deploy:prod
fi
Husky for Node.js
Installation
# Install Husky
npm install husky --save-dev
# Initialize Husky
npx husky install
# Add a hook
npx husky add .husky/pre-commit "npm test"
Configuration
// package.json
{
"scripts": {
"prepare": "husky install"
},
"husky": {
"hooks": {
"pre-commit": "npm test",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
}
}
Git Hooks with Python
Hook Example
#!/usr/bin/env python3
# pre-commit
import sys
import subprocess
def run_tests():
try:
subprocess.run(['pytest'], check=True)
except subprocess.CalledProcessError:
print("Les tests ont échoué")
sys.exit(1)
def check_formatting():
try:
subprocess.run(['black', '--check', '.'], check=True)
except subprocess.CalledProcessError:
print("Le formatage est incorrect")
sys.exit(1)
if __name__ == '__main__':
run_tests()
check_formatting()
Continuous Integration
GitHub Actions
# .github/workflows/ci.yml
name: CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
- name: Check formatting
run: npm run lint
GitLab CI
# .gitlab-ci.yml
stages:
- test
- deploy
test:
stage: test
script:
- npm install
- npm test
- npm run lint
deploy:
stage: deploy
script:
- npm run deploy
only:
- main
Best Practices
-
Security
- Check dependencies
- Scan the code
- Validate secrets
- Protect branches
-
Performance
- Lightweight hooks
- Fast tests
- Result caching
- Parallelization
-
Maintenance
- Clear documentation
- Hook testing
- Error handling
- Detailed logs
Conclusion
Key takeaways:
- Process automation
- Code validation
- Automatic deployment
- Continuous integration
Recommendations:
- Choose the right hooks
- Automate your tests
- Secure your workflows
- Follow best practices
- Keep learning Git
About InSkillCoach
Training and technology expert
Coach specializing in advanced technologies and AI, backed by GNeurone Inc.
Certifications:
- AWS Certified Solutions Architect – Professional
- Google Cloud certifications
- Microsoft Certified: DevOps Engineer Expert
- Certified Kubernetes Administrator (CKA)
- CompTIA Security+
1.9k
346
Comments
Comments are powered by GitHub Discussions
Sign in with GitHub to join the discussion