-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·106 lines (90 loc) · 2.91 KB
/
deploy.sh
File metadata and controls
executable file
·106 lines (90 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
# Build CraftServer for x86_64-unknown-linux-gnu and deploy to a remote host.
# This script is intended to be run from the root of the repository, like `./deploy.sh`.
set -e
readonly SCRIPT_NAME="$0"
force=''
target_host='vps-server'
quiet=''
skip_build=''
dry_run=''
no_backup=''
profile='release'
print() {
if [[ -z $quiet ]]; then
echo "=> $1"
fi
}
fatal_error() {
echo "fatal: $1"
exit 1
}
print_usage() {
echo "Build CraftServer and deploy it to a remote Linux server"
echo ""
echo "Usage:"
echo " ${SCRIPT_NAME} [-f] [-q] [-s] [-d] [-B] [-H <host>] [-p <profile>]"
echo " ${SCRIPT_NAME} -h"
echo "Options:"
echo " -f: force deploy even if there are uncommitted changes"
echo " -q: quiet mode (less output)"
echo " -s: skip build before deploy"
echo " -d: dry run (don't make changes on remote host)"
echo ' -B: skip backing up remote database to `./backup.sql.gz`'
echo " -H: deploy to <host> over ssh/rsync (default: $target_host)"
echo " -p: build with <profile> (default: $profile)"
echo " -h: print this help message"
exit 1
}
while getopts 'fqsdH:p:Bh' flag; do
case "${flag}" in
f)
force='true' ;;
q)
quiet='true' ;;
s)
print "Skipping build: the most recent artifact will be deployed"
skip_build='true' ;;
d)
print "Dry run: no changes will be made on the remote host"
dry_run='true' ;;
H)
target_host="${OPTARG}" ;;
p)
profile="${OPTARG}" ;;
B)
no_backup='true' ;;
*)
print_usage ;;
esac
done
if [[ -z $force ]] && [[ -n $(git diff --exit-code) || -n $(git diff --cached --exit-code) ]]; then
fatal_error "uncommitted changes (use -f to ignore)"
fi
read -p "Enter a release name to make and push a git tag [optional]: "
if [[ -n $REPLY ]]; then
git tag -a "${REPLY}" -m "Release ${REPLY}"
git push origin "${REPLY}"
fi
read -p "Deploy to '${target_host}' using '${profile}'? (Y/[n]): "
if [[ $REPLY != 'Y' ]]; then
fatal_error "cancelled"
fi
if [[ -z $skip_build ]]; then
print "Building for x86_64-unknown-linux-gnu"
cross build --target=x86_64-unknown-linux-gnu --profile "${profile}" --package craftserver
fi
if [[ -n $dry_run ]]; then
print "Dry run, exiting"
exit 0
fi
if [[ -z $no_backup ]]; then
print "Backing up database"
BACKUP_PATH="/var/craftserver/backup.sql.gz"
ssh -l root "${target_host}" "pg_dump -d craftserver -h localhost -U craftserver -Z 9 > '${BACKUP_PATH}'"
rsync -Pvz "root@${target_host}:${BACKUP_PATH}" "./backup.sql.gz"
fi
print "Deploying to '${target_host}'"
rsync -Pvz "./target/x86_64-unknown-linux-gnu/${profile}/craftserver" "root@${target_host}:/var/craftserver/craftserver"
print "Restarting CraftServer"
ssh -l root "${target_host}" "systemctl restart craftserver"