git hooks
DESCRIPTION
TRANSCRIPT
![Page 1: Git hooks](https://reader030.vdocument.in/reader030/viewer/2022020720/54745167b4af9fbe0a8b577c/html5/thumbnails/1.jpg)
Git hooksfor staging and production
Priit Tamboom
LRUG meetupNovember 2010
![Page 2: Git hooks](https://reader030.vdocument.in/reader030/viewer/2022020720/54745167b4af9fbe0a8b577c/html5/thumbnails/2.jpg)
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
![Page 3: Git hooks](https://reader030.vdocument.in/reader030/viewer/2022020720/54745167b4af9fbe0a8b577c/html5/thumbnails/3.jpg)
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.
![Page 4: Git hooks](https://reader030.vdocument.in/reader030/viewer/2022020720/54745167b4af9fbe0a8b577c/html5/thumbnails/4.jpg)
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.
![Page 5: Git hooks](https://reader030.vdocument.in/reader030/viewer/2022020720/54745167b4af9fbe0a8b577c/html5/thumbnails/5.jpg)
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?
![Page 6: Git hooks](https://reader030.vdocument.in/reader030/viewer/2022020720/54745167b4af9fbe0a8b577c/html5/thumbnails/6.jpg)
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.
![Page 7: Git hooks](https://reader030.vdocument.in/reader030/viewer/2022020720/54745167b4af9fbe0a8b577c/html5/thumbnails/7.jpg)
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!
![Page 8: Git hooks](https://reader030.vdocument.in/reader030/viewer/2022020720/54745167b4af9fbe0a8b577c/html5/thumbnails/8.jpg)
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
![Page 9: Git hooks](https://reader030.vdocument.in/reader030/viewer/2022020720/54745167b4af9fbe0a8b577c/html5/thumbnails/9.jpg)
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!
![Page 10: Git hooks](https://reader030.vdocument.in/reader030/viewer/2022020720/54745167b4af9fbe0a8b577c/html5/thumbnails/10.jpg)
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!
![Page 11: Git hooks](https://reader030.vdocument.in/reader030/viewer/2022020720/54745167b4af9fbe0a8b577c/html5/thumbnails/11.jpg)
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
![Page 12: Git hooks](https://reader030.vdocument.in/reader030/viewer/2022020720/54745167b4af9fbe0a8b577c/html5/thumbnails/12.jpg)
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.
![Page 13: Git hooks](https://reader030.vdocument.in/reader030/viewer/2022020720/54745167b4af9fbe0a8b577c/html5/thumbnails/13.jpg)
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.
![Page 14: Git hooks](https://reader030.vdocument.in/reader030/viewer/2022020720/54745167b4af9fbe0a8b577c/html5/thumbnails/14.jpg)
References
Using Git to manage a web sitehttp://toroid.org/ams/git-website-howtoPro Git: Git Hookshttp://progit.org/book/ch7-3.html
![Page 15: Git hooks](https://reader030.vdocument.in/reader030/viewer/2022020720/54745167b4af9fbe0a8b577c/html5/thumbnails/15.jpg)
Live demo
Aka eat your own dog food :-)