Git for deployment

The goal

 

  • Take care of code, environment and db changes
  • 1 step Support one step deployment for multiple instances of the same software version (eg from 1.1 to 1.2)
  • 1 step deployment for multiple instances of different software version(eg from 1.1 to 1.3 and from 1.2 to 1.3)
  • 1 step deployment to specific versions for specific hosts 

The basic idea

  • Use git hooks to deploy
  • Branches or Multiple remotes to filter

Git hooks

  • They live in: repo/hooks
  • Simply executable files
  • The ones we are interested in the server side hooks

Branches vs multiple remotes

Example

#initial directory setup
mkdir master-repo
mkdir dev-repo
mkdir running-instance1
mkdir running-instance2

#bare repositories can be cloned, pushed too and store extra metadata
cd master-repo
git init --bare
cd ..

# checkout the first bare repo
cd dev-repo
git clone ../master-repo
cd ..


# Let's create a simple hook:
#In your bare repo in the hooks directory put these lines in a file named "post-recieve"
#After that make the file executable
#!/bin/sh
git --work-tree=<absolute path to master repo> --git-dir=<absolute path to repo> checkout -f
git --work-tree=<absolute path to master repo> --git-dir=<absolute path to repo> checkout -f

Example

#now create a file and add it from the dev branch:
touch a
git add .
git commit .

#Now all we have to do is push it
git push origin master

#and the other two directories should contain the project (as long as the directories in the hook file are ok and the hook script is executable)

Example

#hook with filtering by branch:
#!/bin/sh

while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    if [ "master" = "$branch" ]; then
        # Do something
    fi  
done

Git for deployment

By Zlatin Stanimirov

Git for deployment

  • 744