Skip to content

Instantly share code, notes, and snippets.

@Benoss
Last active May 11, 2016 00:04
Show Gist options
  • Select an option

  • Save Benoss/69eebb254e03cff0c8f6f529b27de296 to your computer and use it in GitHub Desktop.

Select an option

Save Benoss/69eebb254e03cff0c8f6f529b27de296 to your computer and use it in GitHub Desktop.
Example of cron base auto release script using a git remote origin with lock file in case of long release process
#!/bin/bash
# Example of cron base auto release script using a git remote origin with lock file in case of long release process
# * * * * * /path/to/the/file/update_git_and_release.sh
# die on non-zero status
set -e;
# die on undefined variable
set -u;
# Find current directory of the script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# CD to the script directory
cd "$DIR"
LOCK_FILE="/tmp/release.lock"
if [ ! -f $LOCK_FILE ]; then
touch $LOCK_FILE;
chmod 777 $LOCK_FILE;
fi
#The call “exec 200>$LOCK_FILE” will open the file named in $lock for reading, and assign it file handle 200
exec 8>$LOCK_FILE;
#Tells flock to exclusively lock the file referenced by file handle 200 or exit with code 1
if flock -n 8 ;then
# Update the current branch from remote
git fetch
# Check if the current branch is behind the origin
# git status -bz will display "## master...origin/master [behind 15]" for example
# grep -aq is silently looking for the behind string (will the tracked branch name contains behind)
# grep exit is sent to the $BEHIND variable
git status -bz | grep -aq "behind" && BEHIND=1 || BEHIND=0
if [ $BEHIND -eq 1 ]
then
# force pull new code in the directory quietly
git pull -qf
# Execute release script after the pull
./release_script.sh
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment