Download - Git hooks
Git hooksfor staging and production
Priit Tamboom
LRUG meetupNovember 2010
Outline
What are hooks?Scope of using hooks for deployment Simple scenario of using git hooksImproved scenario of using git hooksEveryday tips ... and live demo
What are hooks?
Git hooks are just plain scripts what will be fired offwhen certain action occurs. Hooks are located at your repo .git/hooks directory.
About hooks
There are quite many hooks such as:pre-commit, prepare-commit-msg, applypatch-msg,pre-rebase, pre-receive etc
Today we use only one hook script: post-update
By the way, hooks are not shared between different repos.As far as I know, git does not provide feature to share orability to push hooks between repos.
Scope
IDEAL FOR: small projects (such as one staging and production server)keeping overhead and dependency low for learning and exploring
PROBABLY NOT IDEAL FOR:managing popular app with huge cluster of servers and putting lot of logic into hooks, probably better fit should be using Shef* or Puppet instead.
* Hint-hint: I'm looking forward to Shef talk, any blave here?
Simple scenario
As a ruby coderyou got an amazing app to maintain and you have one production server.
Let's use git for deployment in order to keep overhead down.
Simple: Setting server side up
# using ssh alias 'myserver'
ssh myserver mkdir example && cd example git initgit config receive.denycurrentbranch ignore
# Now your repo is ready to receive new code, nice!
Simple: Adding a script to server repo
# make it executable cp .git/hooks/post-update.sample .git/hooks/post-updatechmod +x .git/hooks/post-update
# edit .git/hooks/post-update #!/bin/sh cd ..unset GIT_DIRgit reset --hard HEAD
# Now repo HEAD will be on latest commit
Simple: Restart app server
# edit again .git/hooks/post-update and add pkill -HUP unicorn_rails # or whatever server you use
# in demo I'll use StaticMatic, so I added: staticmatic build .
# That's it for our server side!
Simple: Client side setup
# add remote repo where 'myserver' is ssh alias git remote add deploy myserver:/home/user/example
# first time pushing up submit also branch, such as master git push deploy master
# second time you can omit master or make alias git push deploy
# and here you go, enjoy!
Tips
Don't mess to much with server repo, so it's HEAD should be clean aka 'git status' should be clean.Keep your .gitignore file updated, so your status is clear
Our current project setup
STAGING: Scope: We do push into staging multiple times a day.
We push new shiny code to githubGithub fires up our cijoe integration serverCijoe fires '.git/hooks/after-reset' hook what pulls new code to cijoe and staging repoCijoe fires '.git/hooks/build-worked', what sends emails out and now involved people can checkout new stuff on staging.
Our current project setup
Scope: We do push into production once or twice a week PRODUCTION without new gems:
Quite close to Simple Senario setup PRODUCTION with new gems:
Quite close to Simple Senario setup, but I went back to install new gems manually aka'bundle install' way.
References
Using Git to manage a web sitehttp://toroid.org/ams/git-website-howtoPro Git: Git Hookshttp://progit.org/book/ch7-3.html
Live demo
Aka eat your own dog food :-)