The Personal Site of...
About Freedom, UNIX, Web Programming, among other stuff

Execute a Git hook by branch

Programming, Sysadmin

GitMany times I’ve been working in several projects when deployment of different webs according to a branch are necessary. Finally I just wrote a tiny Git hook script that helps me out with this matter, I hope it could help you as it does to me.

By example, for branch master I need that every time someone pushes commits to it, changes deploys in /path/to/my/deployment/unstable, and if branch stable is pushed, then changes are deployed in /path/to/my/deployment/stable.

The script I wrote uses the post-update hook, and cut to “parse” the revision name and provide the branch name, then I can use that name to set the path of the repo. After that I check if that directory exists, and then change the GIT_DIR to that path. Finally I change the position to that path and execute a simple git pull specifying the name of the branch.

Feel yourself free to use it. :)

#!/bin/bash
#
# An example hook script to prepare a packed repository for use over
# dumb transports.
#
# To enable this hook, rename this file to "post-update".

BRANCH=`echo $1 | cut -d '/' -f 3`

if [ $BRANCH == 'master' ]; then

  STAGE='unstable'

else

  STAGE=$BRANCH

fi  

STAGE_PATH=/my/stage/path/$STAGE/

if [ -d $STAGE_PATH ]; then

 GIT_DIR=$STAGE_PATH/.git
 cd $STAGE_PATH
 git pull origin $BRANCH

else

 echo 'Stage path for '$STAGE' not found'

fi

exec git update-server-info

 

6 Comments

  1. fitorec says:

    Interesante yo ahorita lo que ando buscando es un buen sistema de ganchos que me ayude a actualizar un sitio con la acción post-pull.

    Respecto al script veo que defines una variable STAGE_PATH, creo que el valor de esta lo puedes hacer dinamicamente y con esto reutilizas mas tu script, típicamente yo suelo ocupar la instrucción:


    PATH_SCRIPT = $(readlink -f "$0" | xargs dirname )

    En el caso especifico de git, puedes ocupar:

    git rev-parse --show-toplevel

  2. fitorec says:

    También puedes ocupar la variable $GIT_DIR.

  3. Gracias por el comentario, muy buen tip, en este caso puse un path absoluto como ejemplo, por que puede haber casos donde el path sea diferente a la ubicación actual.

  4. Sheryl says:

    Wгite morе, thatѕ all I have to say. Litеrally, it seemѕ as thоugh you relied
    оn thе video to make уour point. You
    definitely knοw what youre talking about, whу waste your іntelligenсe on јust pοsting videos to
    your ωeblοg when you could be givіng us somеthing informative to reаԁ?

  5. Deanna says:

    Thаnκ уou for some other wonderful агticle.
    The placе еlsе may anyone gеt thаt typе of infoгmation іn ѕuch
    an ideal means of writing? I have a presentation ѕubѕequent
    wеek, and I аm at the ѕеaгсh
    for such іnformаtion.

  6. Thank уou for the auѕpіciouѕ ωriteup.

    It іn fаct was a amuѕement account
    it. Look aԁνancеd to far adԁed agrеeable
    fгom yοu! Ηowevеr, how cοuld we communicate?

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>