Bash script to simplify Git deployment workflow
Use exit codes directly

Instead of this:


merge $SOURCE_BRANCH 'staging'
status=$?
if [ $status -ne 0 ]
then
  echo "ERROR: STEP 1" &gt;&amp;2
  exit 1
fi



You can use exit codes directly in conditions, like this:

if ! merge $SOURCE_BRANCH 'staging'
then
  echo "ERROR: STEP 1" &gt;&amp;2
  exit 1
fi


Use it this way everywhere.

Handle errors consistently

The error handling is inconsistent throughout the script:


Sometimes you exit 1, sometimes you return 1 in functions. It would be better to do consistently
Sometimes you print the error message on stdout, sometimes on stderr. It would be better to print it consistently on stderr


Style

To print a blank line you can simply echo without a parameter.

You can drop return 0 as the last statement of a function. The exit code of the last  statement will be used as the exit code of the function.

A semicolon is unnecessary at the end of the line.

Long comments at the end of a line are generally hard to read. Move these to the previous line.

The indentation is not consistent. The code inside the functions looks nice, do the same way for the code outside.

Ex. ( edited by @WonderLand)

#!/bin/bash

function merge() {  
    SOURCE=$1
    DEST=$2

    echo
    echo "---&gt; Merging $SOURCE with $DEST" 

    echo
    echo "---&gt; ---&gt; Checkout $DEST ..."     
    git checkout $DEST

    echo
    echo "---&gt; ---&gt; Pull $DEST ..."
    if ! git pull --ff-only origin $DEST
    then
     exit 1
    fi

    echo
    echo "---&gt; ---&gt; Merging $SOURCE with $DEST ..." 
    # --ff-only trigger errors if merge/pull is not possible
    if ! git merge --ff-only $SOURCE --no-edit
    then
      exit 1
    fi

    echo
    echo "---&gt; ---&gt; Push $DEST ..."
    git push origin $DEST
}

function deploy() {
    MODE=$1
    SOURCE_BRANCH=$2

    echo     
    echo "---&gt; Pull changes from Master ..."

    if ! git checkout $SOURCE_BRANCH
    then    
        exit 1
    fi

    git pull --progress --no-edit --no-stat -v --progress origin master

    if ! merge $SOURCE_BRANCH 'staging'
    then      
      exit 1
    fi

    if [ $MODE = "live" ]
    then
        if ! merge $SOURCE_BRANCH 'master'
        then          
          exit 1
        fi

        if ! merge 'master' 'production'
        then          
          exit 1
        fi
    fi
}

MODE=$1
SOURCE_BRANCH=$2

if [ -z "$MODE"  -o -z "$SOURCE_BRANCH" ]
then
    echo "Usage:"
    echo ""
    echo  "MODE BRANCH_NAME (MODE: live|staging)"
else
    if git show-ref --verify --quiet "refs/heads/$SOURCE_BRANCH"
    then
        echo
        echo "### START ###"
        echo
        deploy $MODE $SOURCE_BRANCH
        echo
        echo "### END ###"
        echo
    else
        echo
        echo "Error: Branch $SOURCE_BRANCH not found"
    fi
fi