--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
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]- Navigate to your repository's hidden hooks directory:bash
cd .git/hooksகுறியீட்டைக் கவனமாகப் பயன்படுத்துங்கள். - Create a file named exactly
pre-push(remove any.sampleextension if you are modifying a default template):bashtouch pre-pushகுறியீட்டைக் கவனமாகப் பயன்படுத்துங்கள். - Open the file and write your validation script. Ensure you include a shebang (
#!/bin/shor#!/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குறியீட்டைக் கவனமாகப் பயன்படுத்துங்கள்.
3. Set Up a Server Deployment Hook (Post-Receive Example)
- SSH into your remote server and create a bare repository:bash
git init --bare /var/git/repo.gitகுறியீட்டைக் கவனமாகப் பயன்படுத்துங்கள். - Navigate to the server-side hooks folder:bash
cd /var/git/repo.git/hooksகுறியீட்டைக் கவனமாகப் பயன்படுத்துங்கள். - Create and edit the
post-receivescript:bashtouch post-receive chmod +x post-receive nano post-receiveகுறியீட்டைக் கவனமாகப் பயன்படுத்துங்கள். - Paste a deployment automation script like the following:[1]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!"குறியீட்டைக் கவனமாகப் பயன்படுத்துங்கள்.
4. Triggering External CI/CD Tools (GitHub, GitLab, Jenkins)
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
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:
Post a Comment