Created
September 4, 2017 10:24
-
-
Save tomasc/3420a5c436c80d468a1b1ad2600366db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
PROJECT_NAME=krabbesholm | |
cd ~/$PROJECT_NAME | |
# ===================================================================== | |
if [ $# -eq 0 ]; then | |
echo "No branch name provided." | |
exit 1 | |
fi | |
BRANCH=$1 | |
case $BRANCH in | |
master) SERVICE='production';; | |
*) SERVICE=$1;; | |
esac | |
# detect currently running color | |
if [ $(docker ps | grep "${SERVICE}_blue" | awk '{print $1}') ]; then | |
CUR_SERVICE="${SERVICE}_blue" | |
NEW_SERVICE="${SERVICE}_green" | |
else | |
CUR_SERVICE="${SERVICE}_green" | |
NEW_SERVICE="${SERVICE}_blue" | |
fi | |
# ===================================================================== | |
# pull latest image for deployed branch | |
docker pull tomasce/"${PROJECT_NAME}_${BRANCH}":latest | |
# precompile assets to shared directory | |
docker-compose run --rm $NEW_SERVICE bin/rails assets:precompile | |
# update running containers with the new service | |
docker-compose up --no-deps -d $NEW_SERVICE | |
# get IP of new conatiner | |
NEW_CONTAINER=$(docker ps | grep "${NEW_SERVICE}" | awk '{print $1}') | |
NEW_CONTAINER_IP=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $NEW_CONTAINER) | |
# wait until new container boots up | |
while ! curl -XGET http://$NEW_CONTAINER_IP:3000; do sleep 1; done | |
# notify appsignal on deploy | |
docker exec $NEW_CONTAINER bash -c 'appsignal notify_of_deploy --revision=$(git describe --always HEAD) --environment=$RAILS_ENV --user=deployer' | |
sleep 5 | |
# stop the currently running color | |
docker-compose stop $CUR_SERVICE | |
# ===================================================================== | |
# remove exited containers | |
docker rm -v $(docker ps -a -q -f status=exited) | |
# remove dangling images | |
docker rmi $(docker images -f "dangling=true" -q) | |
# remove unwanted volumes | |
docker run -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/docker:/var/lib/docker --rm martin/docker-cleanup-volumes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment