Home » Software Development » Bash’ing your git deployment

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 our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

 

Leave a Reply

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

*


Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.