Monday, August 31, 2026

How to write git trigger hooks for CI/CD ?

--https://share.google/aimode/eYfcwK5hykuMhjKLj

To write Git trigger hooks for CI/CD, you must create an executable script inside the .git/hooks/ directory of your repository. Depending on whether you want to prevent bad code from leaving a local machine (Client-side) or automate actual deployments on a server (Server-side), you will use different hook files. [1, 2, 3, 4]
Here is the complete step-by-step process to build and automate your CI/CD workflow using native Git hooks.

1. Understand Hook Placements
Git categorizes hooks into two locations based on where the trigger happens: [1, 2]
  • Client-Side Hooks: Run on your local computer. They are excellent for Local CI tasks like code linting, syntax checking, and running unit tests before code is committed or pushed. [1, 2, 3]
  • Server-Side Hooks: Run on the remote Git server (like a self-hosted Git instance or a bare production repository). They are used for CD (Continuous Deployment) tasks like building Docker images, restarting production services, or firing API webhooks. [1, 2, 3, 4]
2. Set Up a Local CI Hook (Pre-Push Example)
The pre-push hook is the optimal local gatekeeper. It ensures your tests pass before your code ever hits the remote repository. [1]
  1. Navigate to your repository's hidden hooks directory:
    bash
    cd .git/hooks
    
    குறியீட்டைக் கவனமாகப் பயன்படுத்துங்கள்.
  2. Create a file named exactly pre-push (remove any .sample extension if you are modifying a default template):
    bash
    touch pre-push
    
    குறியீட்டைக் கவனமாகப் பயன்படுத்துங்கள்.
  3. Open the file and write your validation script. Ensure you include a shebang (#!/bin/sh or #!/bin/bash) at the very top:
    bash
    #!/bin/bash
    
    echo "🚀 Running local CI pre-push tests..."
    
    # Run your test suite (e.g., Node.js/Python/Go)
    npm test
    
    # Capture the exit status of the test command
    if [ $? -ne 0 ]; then
        echo "❌ Local tests failed! Aborting git push."
        exit 1
    fi
    
    echo "✅ All tests passed. Proceeding with push."
    exit 0
    
    குறியீட்டைக் கவனமாகப் பயன்படுத்துங்கள்.
  4. Make the file executable. Git will ignore the script completely if it lacks execution permissions:
    bash
    chmod +x pre-push
    
    குறியீட்டைக் கவனமாகப் பயன்படுத்துங்கள்.
    [1, 2]
3. Set Up a Server Deployment Hook (Post-Receive Example)
If you manage your own staging or production server, you can set up a bare Git repository on that server. When you push to it, a post-receive hook can immediately deploy the application code. [1, 2]
  1. SSH into your remote server and create a bare repository:
    bash
    git init --bare /var/git/repo.git
    
    குறியீட்டைக் கவனமாகப் பயன்படுத்துங்கள்.
  2. Navigate to the server-side hooks folder:
    bash
    cd /var/git/repo.git/hooks
    
    குறியீட்டைக் கவனமாகப் பயன்படுத்துங்கள்.
  3. Create and edit the post-receive script:
    bash
    touch post-receive
    chmod +x post-receive
    nano post-receive
    
    குறியீட்டைக் கவனமாகப் பயன்படுத்துங்கள்.
  4. Paste a deployment automation script like the following:
    bash
    #!/bin/bash
    
    # Target directory where production files live
    TARGET="/var/www/my-app"
    # Git directory
    GIT_DIR="/var/git/repo.git"
    
    echo "🚀 Remote Git server received push. Deploying code..."
    
    # Checkout the absolute latest code into your production folder
    git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f main
    
    # Run build/restart commands on the production site
    cd $TARGET
    npm install --production
    npm run build
    pm2 restart app-name
    
    echo "🎉 Deployment successfully completed!"
    
    குறியீட்டைக் கவனமாகப் பயன்படுத்துங்கள்.
    [1]
4. Triggering External CI/CD Tools (GitHub, GitLab, Jenkins)
If you are using managed Git hosts like GitHub or GitLab, you cannot write custom scripts directly into their backend file systems. Instead, modern cloud CI/CD systems use webhooks triggered by Git network actions. [1, 2]
If you need to hit an external engine like Jenkins, Harness, or a custom API, you can write a post-receive or post-upload-pack server script that curl-requests an external server: [1]
bash
#!/bin/bash
# Trigger an external Jenkins job via API token
curl -X POST "https://example.com"
குறியீட்டைக் கவனமாகப் பயன்படுத்துங்கள்.

Hook Best Practices
  • Share Hooks via Source Control: Because .git/ folders are ignored by Git, your team won't automatically get your local hooks. Consider changing your project's hook directory to a tracked folder using:
    git config core.hooksPath .githooks
    [1, 2]
  • Avoid Overwhelming Scripts: Local hooks can slow down developers if they take too long to run. Keep client-side pre-commit/pre-push loops snappy; leave heavy-duty multi-environment testing for cloud cloud infrastructure. [1, 2]
Would you like help adapting these hook scripts for a specific programming framework (like Node.js, Python, or Docker) or a particular Git platform (like GitHub Actions, GitLab, or bitbucket)?

No comments: