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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


+ 7 = fourteen



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
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.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close