Last active
September 9, 2023 05:41
-
-
Save amattn/ce12a89ceebe402e464ef3c43147f930 to your computer and use it in GitHub Desktop.
This file contains 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/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="[email protected]" | |
;; | |
test) | |
ADDRESS="[email protected]" | |
;; | |
staging) | |
ADDRESS="[email protected]" | |
;; | |
prod) | |
ADDRESS="[email protected]" | |
;; | |
*) | |
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