Git Hooks

  • We always want to speed up our workflows, right? Of course, automation is the best way to do that.
    
    
  • Git hooks are scripts that Git executes before or after events such as: commit, push, and receive.
    
    
  • In my words, Hooks is like an event trigger, After you did some git operation it'll trigger some events.
    
    
  • There are two types of git Hooks.
    ​
           1.) Client side (Local PC)
    ​       2.) Server side (Remote Server)
    

What is Hooks

  • Git hooks is in .git folder in the project repository
     
  • You can use any scripting language
     
  • Have to make the hooks executable 

    ( Chmod +x file_name )

Where I can Find it ??

  • Built in scripts are mostly shell and perl
     
  • You can use shell script, Ruby, Python. You need to specify which interpreter are we using
               


     

Scripting Language

#!/bin/sh   => For shell Scripts

#!/usr/bin/env python    => For Python

#!/usr/bin/env ruby     => For Ruby
  • They are local to any Git repository and they are not copied to new repository.
     
  • To make the hooks stay update among developers or team it have to be under version control.
     
  • Store hooks in the project directory
     
  • Once it's updated copy those to the local .git/hooks folder

Scope of Hook..

  • applypatch-msg
  • pre-applypatch
  • post-applypatch
  • pre-commit
  • prepare-commit-msg
  • commit-msg
  • post-commit
  • pre-rebase
  • pre-receive
  • update
  • post-receive

Types Of Hooks (.git/hooks)

pre-commit

Pre-commit - Check (Commit Life Cycle)

prepare-commit-msg

commit-msg

post-commit

post-receive Hook

./post-receive 
# check if app is running fine otherwise execute few commands manually 

sudo forever stopall

GIT_WORK_TREE=/home/ec2-user/project

export GIT_WORK_TREE

git checkout -f

cd /home/ec2-user/project

sudo npm install

forever start app.js

Hooks

post-receive git-receive-pack on the remote repo This is run on the remote when pushing after the all refs have been updated. It does not take parameters, but receives info through stdin in the form of "<old-value> <new-value> <ref-name>". Because it is called after the updates, it cannot abort the process.

Thanks

DevOps Day 02 - Git 2.3

By Tarun Sharma

DevOps Day 02 - Git 2.3

Understanding git hooks with EC2

  • 788