git hooks

15
Git hooks for staging and production Priit Tamboom LRUG meetup November 2010

Upload: skills-matter

Post on 22-Nov-2014

3.999 views

Category:

Documents


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Git hooks

Git hooksfor staging and production

Priit Tamboom

LRUG meetupNovember 2010

Page 2: Git hooks

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

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

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

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

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

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

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

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

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

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

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

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

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

Live demo

Aka eat your own dog food :-)