Skip to content

Instantly share code, notes, and snippets.

@mmcev106
Last active August 29, 2015 14:14
Show Gist options
  • Save mmcev106/8ea26d105e8a5bbdf267 to your computer and use it in GitHub Desktop.
Save mmcev106/8ea26d105e8a5bbdf267 to your computer and use it in GitHub Desktop.
A basic deploy script that uses two git repo clones and symlinks to deploy with zero downtime.
#!/bin/bash
set -e # Fail on non-zero exit code
set -u # Fail on undefined variables
deploymentDir=$1
symlink=$2
dirA=$3
dirB=$4
branchName=$5
cd "$deploymentDir"
if [ "$dirA" = `readlink "$symlink"` ]; then
currentDir=$dirA
alternateDir=$dirB
else
currentDir=$dirB
alternateDir=$dirA
fi
cd "$currentDir"
liveCommit=`git rev-parse HEAD`
cd ../"$alternateDir"
git pull
latestCommit=`git rev-parse HEAD`
if [ "$latestCommit" == "$liveCommit" ]; then
echo There are no new commits to deploy, exiting...
exit
fi
echo New commits found, deploying them...
composer install
git tag "$branchName"-`date +%Y-%m-%d_%H-%M-%S`
git push --tags
cd ..
ln -s "$alternateDir" "$symlink"-new
if [ `uname` = 'Darwin' ]; then
# This is not atomic, but we don't care for now since we're not hosting on OSX.
rm "$symlink"
mv "$symlink"-new "$symlink"
else
mv -T "$symlink"-new "$symlink" # This is atomic on Linux.
fi
#!/bin/bash
set -e # Fail on non-zero exit code
set -u # Fail on undefined variables
testDir='deploy.sh-test-dir'
rm -rf $testDir
mkdir $testDir
cd $testDir
mkdir repo
mkdir deployment
cd repo
git init
echo '{}' > composer.json
echo 'commit 1' > test.txt
git add ./
git commit -m 'commit 1'
cd ../deployment
git clone ../repo repo-a
git clone ../repo repo-b
ln -s repo-a live
cd ../repo
echo 'commit 2' > test.txt
git add ./
git commit -m 'commit 1'
cd ../..
./deploy.sh $testDir/deployment live repo-a repo-b master
echo
if [ "`cat $testDir/deployment/live/test.txt`" != 'commit 2' ]; then
echo 'Test Failed: The latest commit was not deployed!'
exit
fi
if [ "`cat $testDir/deployment/repo-a/test.txt`" != 'commit 1' ]; then
echo 'Test Failed: The previous repo version was not rotated as expected!'
cat $testDir/deployment/repo-b/test.txt
exit
fi
rm -rf $testDir
echo Tests completed successfully!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment