Writing Git Hooks in Node.js
At work we use the post-receive git hook to automatically deploy websites on a number of our servers. Among a few other things the hook is used to recreate cached pages when templates are modified. Lately, we’ve added some additional tasks to the post-receive hook and it is becoming more complicated. So when we wanted to add automatic minification of the CSS and JavaScript resources to the hook, we made the decision to convert it to Node.js.
We choose Node.js because we were planning to use two minifiers that required Node.js anyway, and we are looking into the possibility of using Node.js for future projects, so this hook was a good, low risk, place to work with Node.js and see how it will fit in at our business.
Getting Data from Git
If you’ve written git hooks before you know that any information passed to the hook is passed via stdin and not as parameters to the script. To get the data that git passes to the hook you need to include and use the readline node module.
Install readline and save it with your git hook project:
Include readline and parse the data from stdin:
Testing your Hook on the Command Line
I prefer to test my scripts on the command line. I also find it helpful to be able to run the deployment process manually should something fail. You can make it possible to run the hook on the command line by accepting command line parameters in your Node.js script. You can do this by adding and requiring commander.
When I started this project I took inspiration from Git Hooks for the Front End Developer by Bradly Gore.
Leave a Comment