A quick and easy way to deploy from git with post-receive hooks

I’ve recently moved over to a custom framework for my main website and wanted deployments into the repositories release branch to be seamless. I could use Jenkins but that would probably be overkill.

To accomplish this, I wrote a quick post-commit post-receive script that will be called by git each time a commit is pushed into my repository. If the branch name matches ‘release’ the commit is extracted into a release folder under the webroot. The webroot directory (in reality a symbolic link) is then updated to point to this new release.

Using symbolic links has the advantage of not leaving your website in an inconsistant state as files are deleted and then re-extracted, it should be an atomic operation.

Here’s the script, put this in your git repository under the hooks subdirectory in a file named ‘post-commit’.
The old revision, new revision and branch name are handily passed into this script :)

Edit: One thing to keep an eye out for is that if you are running PHP5-FPM, when the symbolic link is switched to the new location the fpm seems to be still serving your scripts from the old location. I would love to figure out why this is but a workaround is to call /etc/init.d/php5-fpm restart as the final step in the post-receive script (not ideal!!)

# A post commit hook that takes any updates pushed to the 'release' branch
# and creates a release directory for the new version under the webroot.
# Live site is then symlinked to this new release directory.
# this is the root of the website (a symlink to a release directory)
if [ "$branch" == "release" ]
    # create a release directory to extract files into
    mkdir $target
    echo "Making target directory: $target"
    # create an archive in the webroot of danielbyrne.net
    /usr/bin/git archive master --format zip --output $target/deploy.zip
    echo "unzipping archive..."
    # extract the archive
    unzip -o -q $target/deploy.zip -d $target
    echo "removing deployment archive"
    # remove the archive file
    rm $target/deploy.zip
    echo "switching symbolic link to $target"
    # now switch the live site to point to the new release
    ln -nsf $target $webroot
    echo "done";

6 thoughts on “A quick and easy way to deploy from git with post-receive hooks

  1. Nice ideas, thanks for posting them!

    You have “git archive master” in this script. Should that be “git archive release”?

    Also, can you skip the unzip step with something like “git archive master | tar -x -C /$target”

    I’m just going to try and get your script working on my site now…

  2. Hi There
    Have you figured out a different way than etc/init.d/php5-fpm restart

    -> I don’t have admin perssions when running the deployment script. That will not allow me to restart the fpm


  3. Hi Raphael,

    I think it’s something to do with switching symlinks when apache is serving files out of those directories. I suppose you could ‘mv’ the directory and recreate it when untarring your deployment artefact but it wouldn’t be atomic and there may be downtime while doing this…

    Let me know if you find something that works for you

  4. I also have this problem with php5-fpm and nginx but the problem manifests itself only once in a long while, it seems… I just incurred in this problem on my like 50th deployment… restarting did the trick but certainly we need to avoid the restart. Maybe php5-cgi doesn’t have this problem?..

  5. Can anybody help me convert the script above to windows batch script. My git and apache servers run on windows.

  6. fangyanting20150710
    longchamp pliage pas cher
    burberry scarf
    michael kors outlet
    coach outlet
    michael kors outlet
    longchamp outlet online
    football jerseys
    pandora charms
    soccer jerseys for sale
    christian louboutin shoes
    true religion jeans
    ed hardy clothing outlet
    nike air max uk
    cheap oakley sunglasses
    burberry outlet
    coach factory outlet
    coach factory outlet
    michael kors bags
    gucci uk
    coach outlet online
    beats headphones
    mont blanc
    retro jordans
    air max 2015
    michael kors
    louboutin femme
    michael kors purse
    polo ralph lauren
    chanel handbags
    kate spade handbags
    gucci handbags
    cheap jordans uk
    coach outlet store online
    burberry sale
    michael kors handbags
    michael kors handbags
    michael kors uk
    cheap oakleys
    burberry outlet online
    cheap oakley sunglasses
    polo outlet
    oakley sunglasses wholesale
    chanel online shop
    ralph lauren uk outlet
    coach outlet
    toms outlet store
    coach outlet
    true religion
    abercrombie & fitch
    oakley outlet
    gucci handbags
    jorda femme pas cher
    kate spade bags
    oakley sunglasses outlet
    true religion sale
    true religion jeans outlet
    longchamp pas cher
    oakley sunglasses
    replica watches for sale
    replica watches rolex
    cheap ray ban sunglasses
    chanel bags
    nike blazer pas cher
    pandora jewelry sale
    ray ban outlet
    oakley sunglasses cheap
    kate spade outlet
    kate spade bags
    ray ban glasses
    michael kors handbags
    snapbacks hats
    louboutin pas cher
    cheap jordan shoes
    air jordan uk
    gucci borse
    ray ban outlet
    nike pas cher
    soccer outlet
    longchamp sale
    ed hardy uk clothing
    toms shoes sale
    fitflops shoes
    michael kors outlet online
    pandora charms
    ray ban sunglasses
    true religion
    true religion sale
    mcm bags
    kate spade handbags
    oakley sunglasses sale
    tory burch outlet online
    mcm outlet online
    michael kors outlet
    tn pas cher
    cheap snabacks
    ray bans
    coach factory outlet
    mont blanc pens
    prada outlet
    cheap soccer jerseys
    longchamp pliage
    coach outlet store online
    pandora bracelets
    ralph lauren pas cher
    michael kors
    polo ralph lauren uk
    fitflops sale
    kate spade new york
    coach outlet store online
    tory burch shoes
    oakley sunglasses outlet
    ralph lauren femme
    michael kors handbags
    tory burch handbags
    nike blazer high
    hollister kids
    pandora jewelry stores
    hollister outlet
    cheap oakley sunglasses
    michael kors bags
    coach factory outlet
    ralph lauren sale
    ray-ban sunglasse

    coach outlet
    oakley store
    ray ban wayfarer
    michael kors watches
    kate spade
    abercrombie and fitch
    true religion outlet
    air force pas cher
    michael kors
    cheap jordans free shipping
    air max uk
    jordan femme
    ralph lauren polo shirts
    louis vuitton sito ufficiale
    pandora jewelry outlet
    gucci belt
    tory burch outlet
    soccer shoes for sale
    nike air force pas cher
    prada shoes
    coach outlet store online
    burberry handbags
    ray ban sunglasses
    gucci sito ufficiale
    ray bans
    sac louis vuitton
    louis vuitton borse
    rolex watches for sale
    toms shoes for women
    chaussure louboutin pas cher
    cheap beats by dre
    ed hardy outlet
    chanel outlet
    cheap air max
    cheap ray bans
    ed hardy uk
    sac louis vuitton pas cher
    ray bans

Leave a Reply

Your email address will not be published. Required fields are marked *

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