(Continuous Integration & GIT) - how to simply with bash script
<p>In my company I follow the following workflow ( I think it can be defined a kind of 'Continuous Integration' )</p>

<p><strong>Workflow:</strong>  </p>

<ul>
<li>We have 3 fixed branches ( <code>staging</code>/<code>master</code>/<code>production</code>)  </li>
<li>Every push to <code>staging</code> or <code>production</code> automatically deploy to staging/production server.</li>
</ul>

<p>We use git in the following way:<br>
(suppose I'm working on the new functionality 'A')</p>

<ol>
<li>I create a new branch from Master (ex. branch 'A')</li>
<li>If I want to show my changes to the client I merge A to stating and push it.</li>
<li>once finish my work on A I merge back A on Master and push master</li>
<li>to send changes live I merge Master in Production branch and push production</li>
</ol>

<p>I find quite boring/time consuming to <code>git checkout ...</code> <code>git pull ..</code> bla bla every time I have to deploy live/staging my work.</p>

<p>So I have produced the following bash script, the aim is to simply the process of deploying branch A into a single command <code>deploy.sh live A</code></p>

<pre><code>#!/bin/bash
function merge() {  
    SOURCE=$1
    DEST=$2

    echo ''
    echo "--- Merging $SOURCE with $DEST ---" 
    echo "--&gt; Checkout $DEST ..." 
    git checkout $DEST

    echo "--&gt; Pull $DEST ..."
    git pull --ff-only origin $DEST
    if [ $? -ne 0 ]
    then
        echo "ERROR: Could not PULL" 
        return 1
    fi

    echo "--&gt; Merging $SOURCE with $DEST ..."
    # --ff-only trigger errors if merge/pull is not possible 
    git merge --ff-only $SOURCE --no-edit  
    if [ $? -ne 0 ]
    then
        echo "ERROR: Could not MERGE"
        return 1
    fi

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

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

    echo ''
    echo "### START ###"
    echo ''
    echo "--- Pull changes from Master ---"
    git checkout $SOURCE_BRANCH
    git pull --progress --no-edit --no-stat -v --progress origin master


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

    if [ $MODE = "live" ]
    then
        merge $SOURCE_BRANCH 'master'
        status=$?
        if [ $status -ne 0 ]
        then
            echo "ERROR: STEP 2"
            return 1
        fi

        merge 'master' 'production'
        status=$?
        if [ $status -ne 0 ]
        then
            echo "ERROR: STEP 3"
            return 1
        fi
    fi

    echo ''
    echo "### END ###"
    echo ''
}

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
        deploy $MODE $SOURCE_BRANCH
    else
        echo ''
        echo "Error: Branch $SOURCE_BRANCH not found"
    fi
fi
</code></pre>

<p><strong>The question:</strong>  </p>

<p>I'm quite newby both on GIT and BASH scripting. 
So I would like to know if the above workflow/script is ok ? Any recommendation is welcome<br>
( in the same time I'm happy to share this as resource )</p>