SlideShare a Scribd company logo
Git hooks
for staging and production
Priit Tamboom
LRUG meetup
November 2010
Outline
What are hooks?
Scope of using hooks for deployment
Simple scenario of using git hooks
Improved scenario of using git hooks
Everyday tips
... and live demo
What are hooks?
Git hooks are just plain scripts what will be fired off
when 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 or
ability 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 coder
you 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 init
git 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-update
chmod +x .git/hooks/post-update
# edit .git/hooks/post-update
#!/bin/sh
cd ..
unset GIT_DIR
git 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 github
Github fires up our cijoe integration server
Cijoe fires '.git/hooks/after-reset' hook
what pulls new code to cijoe and staging repo
Cijoe 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 site
http://toroid.org/ams/git-website-howto
Pro Git: Git Hooks
http://progit.org/book/ch7-3.html
Live demo
Aka eat your own dog food :-)

More Related Content

Git hooks

  • 1. Git hooks for staging and production Priit Tamboom LRUG meetup November 2010
  • 2. Outline What are hooks? Scope of using hooks for deployment Simple scenario of using git hooks Improved scenario of using git hooks Everyday tips ... and live demo
  • 3. What are hooks? Git hooks are just plain scripts what will be fired off when certain action occurs. Hooks are located at your repo .git/hooks directory.
  • 4. 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 or ability to push hooks between repos.
  • 5. 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?
  • 6. Simple scenario As a ruby coder you got an amazing app to maintain and you have one production server. Let's use git for deployment in order to keep overhead down.
  • 7. Simple: Setting server side up # using ssh alias 'myserver' ssh myserver mkdir example && cd example git init git config receive.denycurrentbranch ignore # Now your repo is ready to receive new code, nice!
  • 8. Simple: Adding a script to server repo # make it executable cp .git/hooks/post-update.sample .git/hooks/post-update chmod +x .git/hooks/post-update # edit .git/hooks/post-update #!/bin/sh cd .. unset GIT_DIR git reset --hard HEAD # Now repo HEAD will be on latest commit
  • 9. 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!
  • 10. 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!
  • 11. 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
  • 12. Our current project setup STAGING: Scope: We do push into staging multiple times a day. We push new shiny code to github Github fires up our cijoe integration server Cijoe fires '.git/hooks/after-reset' hook what pulls new code to cijoe and staging repo Cijoe fires '.git/hooks/build-worked', what sends emails out and now involved people can checkout new stuff on staging.
  • 13. 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.
  • 14. References Using Git to manage a web site http://toroid.org/ams/git-website-howto Pro Git: Git Hooks http://progit.org/book/ch7-3.html
  • 15. Live demo Aka eat your own dog food :-)