About Jakub Nabrdalik

Jakub is a software developer: programmer, architect, analyst, team leader, project manager and coach at TouK. Infected with eXtreme Programming and Software Craftsmanship. Test driving and pair programming web and enterprise applications since 2005 in several languages.

Bash’ing your git deployment

Chuck Norris deploys after every commit. Smart men deploy after every successful build on their Continuous Integration server. Educated men, deploy code directly from their distributed version control systems. I, being neither, had to write my deployment script in bash.

We’re using git and while doing so I wanted us to:

  • deploy from working copy, but…
  • make sure that you can deploy only if you committed everything
  • make sure that you can deploy only if you pushed everything upstream
  • tag the deployed hash
  • display changelog (all the commits between two last tags)

Here are some BASH procedures I wrote on the way, if you need them:

make sure that you can deploy only if you committed everything
 

verifyEverythingIsCommited() {
    gitCommitStatus=$(git status --porcelain)
    if [ '$gitCommitStatus' != '' ]; then
        echo 'You have uncommited files.'
        echo 'Your git status:'
        echo $gitCommitStatus
        echo 'Sorry. Rules are rules. Aborting!'
        exit 1
    fi
}

 
make sure that you can deploy only if you pushed everything upstream
 

verifyEverythingIsPushedToOrigin() {
    gitPushStatus=$(git cherry -v)
    if [ '$gitPushStatus' != '' ]; then
        echo 'You have local commits that were NOT pushed.'
        echo 'Your 'git cherry -v' status:'
        echo $gitPushStatus
        echo 'Sorry. Rules are rules. Aborting!'
        exit 1
    fi
}

 
tag the deployed hash

Notice: my script takes first parameter as the name of the server to deploy to (this is $1 passed to this procedure). Also notice, that ‘git push’ without the ‘–tags’ does not push your tags.

tagLastCommit() {
    d=$(date '+%y-%m-%d_%H-%M-%S')
    git tag '$1_$d'
    git push --tags
}

This creates nice looking tags like these:

preprod_12-01-11_15-16-24

prod_12-01-12_10-51-33

test_12-01-11_15-11-10

test_12-01-11_15-53-42

display changelog (all the commits between two last tags)
 

printChangelog() {
    echo 'This is changelog since last deploy. Send it to the client.'
    twoLastHashesInOneLine=$(git show-ref --tags -s | tail -n 2 | tr '\\n' '-');
    twoLastHashesInOneLineWithThreeDots=${twoLastHashesInOneLine/-/...};
    twoLastHashesInOneLineWithThreeDotsNoMinusAtTheEnd=$(echo $twoLastHashesInOneLineWithThreeDots | sed 's/-$//');
    git log --pretty=oneline --no-merges --abbrev-commit  $twoLastHashesInOneLineWithThreeDotsNoMinusAtTheEnd
}

The last command gives you a nice log like this:

e755c63 deploy: fix for showing changelog from two first tags instead of two last ones

926eb02 pringing changelog between last two tags on deployment

34478b2 added git tagging to deploy
 

Reference: Bash’ing your git deployment from our JCG partner Jakub Nabrdalik at the Solid Craft blog.

Related Whitepaper:

Software Architecture

This guide will introduce you to the world of Software Architecture!

This 162 page guide will cover topics within the field of software architecture including: software architecture as a solution balancing the concerns of different stakeholders, quality assurance, methods to describe and evaluate architectures, the influence of architecture on reuse, and the life cycle of a system and its architecture. This guide concludes with a comparison between the professions of software architect and software engineer.

Get it Now!  

Leave a Reply


8 − three =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books