Created
April 30, 2024 08:31
-
-
Save julik/b61b5cacf13e3b15d0f10b90491e41f5 to your computer and use it in GitHub Desktop.
The Deployinator™©
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
#!/usr/bin/env bash | |
set -e | |
# This script will build a Docker image from the current state of the working tree. | |
# It will tag it with the short Git sha, and then push it to Quay.io under our company | |
# account. Afterwards the image can be picked up from other hosts to deploy. | |
export DEPLOY_HOST=<someapp>.<my-domain> | |
export REV=`git log -1 --pretty=%h` | |
echo "Buildind Docker image" | |
docker build . --build-arg APP_REVISION=$REV --build-arg RACK_ENV=production -t <someapp>:$REV | |
echo "Copying image to $DEPLOY_HOST" | |
docker save <someapp>:$REV | ssh -C root@$DEPLOY_HOST docker load | |
# The _data directory of the docker volume must be set to world-writable so that | |
# writing to it is possible from within the container. Not the most secure option | |
# but it does the job. Commented out because it was done from the terminal. | |
ssh root@$DEPLOY_HOST chmod a+r+w+x /var/lib/docker/volumes/<someapp>-sites/_data | |
ssh root@$DEPLOY_HOST chmod a+r+w+x /var/lib/docker/volumes/<someapp>-tmp/_data | |
echo "Running migrations" | |
docker -H ssh://root@$DEPLOY_HOST run \ | |
--rm \ | |
--network="<someapp>-net" \ | |
-v <someapp>-sites:/webapp/sites:Z \ | |
<someapp>:$REV \ | |
/bin/sh -c 'cd /webapp; bundle exec rake db:migrate' | |
echo "Stopping the currently running worker container" | |
docker -H ssh://root@$DEPLOY_HOST stop <someapp>-app || true | |
docker -H ssh://root@$DEPLOY_HOST rm <someapp>-app || true | |
# The thing needs to run with --init in case we need to fork subprocesses | |
echo "Starting the new app container" | |
docker -H ssh://root@$DEPLOY_HOST run \ | |
--name="<someapp>-app" \ | |
--detach --restart="unless-stopped" \ | |
--network="<someapp>-net" \ | |
-v <someapp>-sites:/webapp/sites:Z \ | |
-v <someapp>-tmp:/webapp/tmp:Z \ | |
--init \ | |
-p "9292:9292" \ | |
-e MALLOC_ARENA_MAX=2 \ | |
-e APP_REVISION=$REV \ | |
<someapp>:$REV | |
echo "Deleting the old <someapp> images" | |
DELETE_AFTER_AMOUNT=5 | |
IMAGES_TO_DELETE=`docker -H ssh://root@$DEPLOY_HOST images -q <someapp> | awk 'NR>'$DELETE_AFTER_AMOUNT' { print $0 }'` | |
if [[ $IMAGES_TO_DELETE != "" ]]; then | |
echo "Found old docker images, will clean up:" | |
echo | |
# show full list of images that are more than for ex. 5 (1st line is column name line, therefore 5 + 1) and the column name line itself | |
docker -H ssh://root@$DEPLOY_HOST images <someapp> | awk 'NR==1; NR>'$DELETE_AFTER_AMOUNT+1' { print $0 }' | |
# force delete the images | |
docker -H ssh://root@$DEPLOY_HOST rmi -f $IMAGES_TO_DELETE | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment