Skip to content

Instantly share code, notes, and snippets.

@amattn

amattn/deploy.sh

Last active Jan 18, 2021
Embed
What would you like to do?
#!/bin/sh
set -o nounset
set -o errexit
usage(){
echo "Usage: $0 X.Y.Z dev|test|staging|prod"
echo ""
echo "example: ./deploy.sh 1.1.3 prod"
echo "example: ./deploy.sh 1.2.0 dev"
}
ADDRESS="INVALID_ADDRESS"
if [ $# -lt 2 ]
then
echo "not enough arguments supplied"
usage
exit 1
elif [ $# -gt 2 ]
then
echo "too many arguments supplied"
usage
exit 2
fi
SERVER_ENV=$2
case $SERVER_ENV in
help | -h | --help)
usage
exit 0
;;
dev)
ADDRESS="USER@DEV.YOURINSTANCE.COM"
;;
test)
ADDRESS="USER@TEST.YOURINSTANCE.COM"
;;
staging)
ADDRESS="USER@STAGING.YOURINSTANCE.COM"
;;
prod)
ADDRESS="USER@YOURINSTANCE.COM"
;;
*)
echo "ERROR: unknown server environment \"$SERVER_ENV\""
usage
exit 3
;;
esac
VERSION=$1
SRC_DIR="_staging/my_app-${VERSION}/."
# service files stuff
echo "******************"
echo "SETUP SERVICE FILE"
rsync -zarv \
--include="myapp.service" \
--exclude="*" \
"." "$ADDRESS:/home/USER"
ssh $ADDRESS "cp /home/USER/myapp.service /etc/systemd/system/myapp.service && systemctl daemon-reload && systemctl enable myapp.service"
# stop the process
echo "******************"
echo "STOP SERVER"
ssh $ADDRESS systemctl stop myapp.service
# copy files over
echo "******************"
echo "RSYNC RELEASE"
ssh $ADDRESS mkdir -p /home/USER/myapp
rsync -az --progress --delete --exclude=.DS_Store _staging/my_app-${VERSION}/. $ADDRESS:/home/USER/myapp/.
ssh $ADDRESS chown USER -R /home/USER/myapp
ssh $ADDRESS chgrp USER -R /home/USER/myapp
# migrate
echo "******************"
echo "MIGRATE DB"
ssh $ADDRESS /home/USER/myapp/bin/my_app eval "MyApp.Release.migrate"
# start the process
echo "******************"
echo "RESTART SERVER"
ssh $ADDRESS systemctl start myapp.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment