Bhesh Sejawal Bhesh Sejawal - 5 months ago 77
Git Question

Trigger bash script when creating tag OR branch in git

I want make auto deployment as following:


  1. Deploy to QC environment on every code push

  2. Deploy to Production Environment only on creating new tag



First one is already done using git hooks and now i want to implement second one. Can you help on it?

Answer Source

Solved

In githook post-receive write script like:

process_ref() {
    oldrev=$(git rev-parse $1)
        newrev=$(git rev-parse $2)
        refname="$3"
    arr=(${refname//// })
    name="${arr[2]}"
    case "$refname" in
        refs/tags/*)
        if expr "$oldrev" : '00*$' >/dev/null
            then
            echo "tag name $name"
            sh trigger-script-on-creating-tag.sh        
        fi
        ;;
        refs/heads/*)
            echo "branch name $name"
            sh trigger-on-creating-branch-or-pushed-code.sh
        ;;
        *)
        echo "other"
        exit 1
    esac        
}

while read REF; do process_ref $REF; done
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download