I'm working on getting into some more advanced usage of git, and I think hooks are the way that I want to go, perhaps somebody can give me some advice here.
My plan is to have a git repository with 3 branches (development, staging, and production). I want commits to each of these 3 branches to trigger a different script post-commit.
Does git have the capability to do this or am I barking up the wrong tree?
Thanks in advance.
The commit-msg hook takes one parameter, which again is the path to a temporary file that contains the commit message written by the developer. If this script exits non-zero, Git aborts the commit process, so you can use it to validate your project state or commit message before allowing a commit to go through.
pre-commit hooks are a mechanism of the version control system git. They let you execute code right before the commit. Confusingly, there is also a Python package called pre-commit which allows you to create and use pre-commit hooks with a way simpler interface.
The commit-msg hook is much like the prepare-commit-msg hook, but it's called after the user enters a commit message. This is an appropriate place to warn developers that their message doesn't adhere to your team's standards. The only argument passed to this hook is the name of the file that contains the message.
in a post-commit hook you could do the following:
if [ `git rev-parse --abbrev-ref HEAD` == "development" ]; then echo "development-script" elif [ `git rev-parse --abbrev-ref HEAD` == "staging" ]; then echo "staging-script" elif [ `git rev-parse --abbrev-ref HEAD` == "production" ]; then echo "production-script" fi
I had written a script for myself to do this functionality.
https://github.com/fotuzlab/githubdump-php
Host this file on your server, preferably repo root and define the url in github webhooks. Change 'allcommits' on line 8 with your branch name and add your code/function at line 18.
You will need separate files and webhooks for all your 3 instances.
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With