Test whether a git pull is needed from within a batch script

Just a quick hack I did to avoid having to sync a couple of scripts unnecessarily when deploying my load balancers. Underlying idea stolen from a post by Neil Mayhew on Stackoverflow.

Shell session script:

#!/bin/bash
UPSTREAM=${1:-'@{u}'}
LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse "$UPSTREAM")
BASE=$(git merge-base @ "$UPSTREAM")
if [ $LOCAL = $REMOTE ]; then
    GIT_STATUS=nochange
elif [ $LOCAL = $BASE ]; then
    GIT_STATUS=changed
    git pull
fi

Ansible playbook:

---
vars:
-   version_status: "{{ lookup ('env', 'GIT_STATUS') }}"
-   tasks:
    -   name: Update HAProxy scripts
        copy:
            src: "{{ config_root }}/etc/haproxy/scripts"
            dest: "/etc/haproxy"
        when: version_status=="changed"

Leave a Reply