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!!)

#!/bin/bash
#
# 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.
 
oldrev=$1
newrev=$2
branch=$3
 
# this is the root of the website (a symlink to a release directory)
webroot=/var/www/danielbyrne.net/www
 
if [ "$branch" == "release" ]
then
 
    # create a release directory to extract files into
    target=/var/www/danielbyrne.net/releases/$2/
    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";
fi

8 comments

  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

    Regards
    Raphael

  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.
    Thanks.

  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
    longchamp
    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
    louboutin
    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
    louboutin
    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

  7. chenlina20160217
    hollister outlet
    michael kors handbags
    michael kors outlet online
    jordan concords
    gucci outlet
    official coach factory outlet
    louis vuitton handbags
    ralph lauren uk
    canada goose jackets
    toms outlet
    lebron 13
    coach factory outlet
    ray ban sunglasses
    louis vuitton outlet
    polo ralph lauren
    abercrombie and fitch
    cheap nfl jerseys
    michael kors handbags
    coach outlet store
    michael kors outlet online
    kate spade handbags
    michael kors outlet
    michael kors outlet
    jordans for sale
    gucci handbags
    coach outlet online
    hollister outlet
    pandora jewelry
    abercrombie and fitch new york
    fitflops
    coach outlet store online
    cheap ugg boots
    adidas superstars
    ray ban sunglasses
    coach outlet store online
    coach outlet online
    cheap jordans
    nike roshe run women
    michael kors outlet online
    coach outlet
    timberland outlet
    uggs outlet
    tiffany and co
    jordan concords
    coach factory outlet
    polo outlet
    tory burch handbags
    michael kors outlet online
    canada goose jackets
    louis vuitton handbags
    louboutin
    longchamp outlet
    kobe 10
    louis vuitton handbags
    coach outlet store online
    ray ban sunglasses
    ray ban sunglasses outlet
    toms shoes
    louis vuitton outlet
    louboutin pas cher
    cheap air max
    soccer shoes
    cheap jordan shoes
    coach factory outlet
    uggs outlet
    jordan retro
    nike free runs
    cheap air jordans
    ralph lauren uk
    marc jacobs handbags
    michael kors outlet
    ugg outlet
    true religion outlet
    p90x
    celine outlet
    michael kors handbags
    michael kors outlet
    ray bans
    ralph lauren outlet
    louis vuitton outlet online
    toms wedges
    louis vuitton outlet
    christian louboutin outlet
    ralph lauren outlet
    air force 1
    louis vuitton outlet
    kobe 11
    mulberry handbags
    louis vuitton outlet
    air max uk
    toms shoes
    air max 95
    ugg boots
    michael kors outlet clearance
    louis vuitton
    ugg sale
    chanel handbags
    louis vuitton outlet stores
    polo ralph lauren outlet
    cheap jordans
    uggs on sale
    cheap uggs
    nike air max
    hollister
    coach outlet
    oakley sunglasses
    louis vuitton handbags
    coach outlet
    canada goose
    abercrombie and fitch new york
    timberland outlet
    oakley sunglasses
    ray ban sunglasses
    cheap nfl jerseys
    oakley outlet
    michael kors uk
    air jordans
    michael kors outlet online
    hollister
    chanel handbags
    cheap oakley sunglasses
    vans shoes sale
    louis vuitton handbags
    pandora outlet
    kobe 9
    abercrombie outlet
    lebron james shoes 13
    ugg boots
    ugg outlet store
    gucci shoes
    nike trainers uk
    instyler max
    lebron 11
    michael kors outlet
    louis vuitton outlet
    tory burch outlet online
    canada goose outlet
    chanel bags
    tod’s shoes
    ray ban sunglasses
    cheap ugg boots
    oakkey sunglasses
    michael kors handbags
    nike huarache shoes
    nba jerseys
    michael kors outlet
    oakley vault
    cheap toms
    burberry outlet
    mont blanc legend
    tod’s shoes
    hollister outlet
    celine handbags
    canada goose outlet
    abercrombie & fitch
    michael kors outlet
    louis vuitton bags
    louis vuitton
    ugg boots
    replica rolex watches
    oakley sunglasses
    kobe 9
    true religion outlet
    uggs for women
    louis vuitton handbags
    oakley sunglasses
    the north face
    coach factory outlet
    true religion jeans sale
    kids lebron shoes
    north face outlet
    louis vuitton handbags
    kevin durant basketball shoes
    kate spade
    coach factory outlet
    lebron shoes
    abercrombie and fitch
    beats solo
    uggs outlet
    ray ban sunglasses
    prada handbags
    louis vuitton
    supra shoes
    oakley outlet
    jordan retro
    fitflops sale clearance
    toms outlet
    michael kors outlet online
    christian louboutin shoes
    kate spade handbags
    ugg outlett
    toms shoes
    louis vuitton handbags
    ugg boots
    jordan 11 concord
    true religion jeans
    michael kors outlet
    coach factory outlet online
    canada goose outlet
    replica watches
    abercrombie outlet
    chanel outlet
    louis vuitton outlet
    the north face outlet
    ray ban sunglasses outlet
    coach factory outlet
    ugg outlet
    beats by dr dre
    ray ban sunglasses
    louis vuitton purses
    oakley outlet
    uggs outlet
    michael kors outlet
    louis vuitton handbags
    mihchael kors outlet online
    cheap uggs
    oakley sunglasses cheap
    the north face outlet
    michael kors outlet
    burberry scarf
    michael kors handbags
    canada goose jackets
    hollister clothing
    polo ralph lauren
    chanel bags
    cheap jerseys
    hollister clothing
    christian louboutin outlet
    christian louboutin outlet
    louis vuitton
    gucci outlet
    hollister shirts
    ugg boots
    nike huarache
    coach outlet
    ray ban sunglasses outlet
    true religion jeans outlet
    tods shoes
    louis vuitton outlet
    cheap oakley sunglasses
    louis vuitton
    michael kors outlet online
    ugg outlet
    tods outlet online
    ray ban sunglasses
    louis vuitton
    micheael kors outlet
    ray ban wayfarer
    ralph lauren uk
    oakley sunglasses
    kobe bryant shoes
    michael kors outlet online
    running shoes
    michael kors handbags
    michael kors outlet
    abercrombie and fitch
    louis vuitton outlet
    coach outlet store online
    ralph lauren outlet
    cheap nfl jerseys
    giuseppe zanotti
    uggs on sale
    air max 90
    louis vuitton outlet
    louis vuitton handbags
    swarovski outlet
    true religion outlet
    michael kors outlet online
    polo ralph lauren
    michael kors handbags
    ugg boots sale
    canada goose coats
    michael kors outlet stores
    chanel outlet
    ed hardy clothing
    abercrombie outlet
    jordan retro 11
    cheap ray ban sunglasses
    adidas originals
    tory burch outlet
    cheap jerseys
    cheap oakley sunglasses
    coach factory outlet
    replica watches
    christian louboutin shoes
    uggs outlet
    r

  8. coach outlet online
    michael kors handbags
    michael kors outlet online
    mcm outlet
    adidas outlet store
    hermes belt
    mbt shoes
    michael kors handbags clearance
    tiffany outlet
    calvin klein outlet
    nike air max 90
    michael kors handbags
    coach outlet online
    hermes outlet
    ray ban sunglasses
    kobe shoes
    lebron james shoes
    michael kors outlet
    michael kors handbags
    louboutin pas cher
    louis vuitton bags cheap
    michael kors outlet clearance
    louis vuitton handbags
    michael kors factory outlet
    michael kors handbags wholesale
    tiffany jewellery
    true religion outlet
    nike air force 1
    prada shoes
    football shirts
    longchamp handbags
    michael kors outlet
    michael kors online
    louis vuitton outlet
    cheap snapbacks
    mlb jerseys
    snapbacks wholesale
    louis vuitton pas cher
    christian louboutin online
    louis vuitton handbags outlet
    iphone cases
    hermes outlet
    lacoste shirts
    swarovski crystal
    true religion jeans
    air jordan shoes
    longchamp pliage
    true religion canada
    hermes belt for sale
    links of london
    christian louboutin outlet
    michael kors uk outlet
    links of london jewellery
    reebok outlet store
    mbt shoes outlet
    louis vuitton bags
    beats by dre
    swarovski outlet
    tiffany and co
    nike air max
    prada outlet online
    basketball shoes
    adidas wings shoes
    beats by dr dre
    jordan pas cher
    christian louboutin shoes
    michael kors factory outlet
    michael kors clearance
    nike air max 90
    mcm backpack
    mulberry outlet
    louis vuitton handbags
    ralph lauren
    air max 2015
    michael kors outlet
    futbol baratas
    kobe bryants shoes
    louis vuitton outlet
    swarovski crystal
    lebron shoes
    longchamp handbags
    swarovski jewelry
    louis vuitton handbags
    cheap michael kors handbags
    discount michael kors handbags
    air max 90
    nike air max
    pandora jewelry
    michael kors online outlet
    cheap nfl jerseys
    michael kors outlet store
    christian louboutin outlet
    air force 1 shoes
    nike air force 1
    louis vuitton handbags
    juicy couture tracksuit
    swarovski outlet
    longchamp solde
    adidas outlet
    coach outlet
    hermes birkin bag
    cheap jordan shoes
    ray ban outlet
    true religion jeans
    lebron shoes
    thomas sabo uk
    burberry outlet online
    cheap ray ban sunglasses
    birkenstock sandals
    calvin klein underwear
    tiffany outlet
    hermes bags
    louis vuitton bags
    mulberry uk
    ralph lauren pas cher
    michael kors outlet online
    true religion jeans
    soccer jerseys
    michael kors outlet
    michael kors factory store
    kobe shoes
    pandora outlet
    herve leger outlet
    pandora jewelry
    timberland boots
    michael kors wallet sale
    michael kors outlet
    michael kors outlet
    michael kors outlet online
    cheap oakley sunglasses
    cheap nhl jerseys
    louis vuitton outlet online
    fitflop sale
    mulberry outlet store
    ray ban sunglasses
    michael kors handbags outlet
    swarovski crystal
    nfl jersey wholesale
    michael kors bags
    asics,asics israel,asics shoes,asics running shoes,asics israel,asics gel,asics running,asics gel nimbus,asics gel kayano
    nike air huarache
    nike tn pas cher
    nike blazer pas cher
    longchamp pas cher
    ralph lauren shirts
    kobe bryant shoes
    nike air max 90
    michael kors outlet
    michael kors uk
    michael kors outlet
    nike roshe run
    nike huarache
    true religion jeans
    true religion outlet
    true religion jeans
    cheap mlb jerseys
    prada handbags
    ray ban sunglasses
    true religion outlet
    michael kors outlet
    cheap jordans
    reebok trainers
    michael kors handbags
    swarovski crystal
    michael kors outlet
    michael kors outlet store
    michael kors outlet online
    rolex uk
    rolex watches
    timberland shoes
    louis vuitton outlet
    nba jerseys
    timberland boots
    louis vuitton outlet store
    cheap jordans
    christian louboutin uk
    fitflop clearance
    camisetas futbol baratas
    ray-ban sunglasses
    polo shirts
    toms outlet
    michael kors outlet online
    nfl jerseys wholesale
    louis vuitton outlet
    ray ban sunglasses
    toms outlet
    swarovski outlet
    asics
    michael kors handbags
    nhl jerseys
    ralph lauren polo
    cheap jordan shoes
    cheap football shirts
    coach outlet store
    toms shoes
    longchamp pliage
    jordan shoes
    air jordan shoes for sale
    nike air huarache
    mulberry outlet
    ray ban sunglasses
    lebron james shoes
    soccer jerseys wholesale
    longchamp outlet
    jordan shoes 2015
    prada outlet
    juicy couture outlet
    cheap nfl jerseys
    michael kors canada
    adidas trainers
    true religion jeans outlet
    herve leger dresses
    prada sneakers
    coach outlet canada
    coach outlet
    michael kors factory outlet
    prada outlet
    hermes birkin
    thomas sabo outlet
    rolex watches for sale
    michael kors outlet online
    ralph lauren,polo ralph lauren,ralph lauren outlet,ralph lauren italia,ralph lauren sito ufficiale
    michael kors sale
    michael kors outlet
    longchamp handbags outlet
    toms outlet store
    nfl jerseys
    michael kors outlet
    ralph lauren outlet
    rolex watches
    adidas wings
    cheap jordans
    michael kors outlet
    michael kors handbags
    nike mercurial
    basketball shoes,basketball sneakers,lebron james shoes,sports shoes,kobe bryant shoes,kobe sneakers,nike basketball shoes,running shoes,mens sport shoes,nike shoes
    cheap nba jerseys
    louis vuitton handbags outlet
    michael kors outlet
    louis vuitton outlet store
    michael kors wallet
    michael kors outlet
    soccer jerseys
    lacoste pas cher
    jordan shoes
    beats by dre
    pandora outlet
    lacoste polo shirts
    michael kors outlet online
    michael kors outlet sale
    michael kors handbags clearance
    michael kors handbags sale
    michael kors wholesale
    nike roshe
    nike huarache
    chaussure louboutin
    mont blanc pens
    beats headphones
    cheap soccer jerseys
    ray-ban sunglasses
    cheap nfl jersey
    cheap mlb jerseys
    michael kors uk
    oakley sunglasses
    michael kors handbags
    mlb jerseys
    hermes outlet store

Leave a Reply

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