-
Notifications
You must be signed in to change notification settings - Fork 20
/
deploy-latest.sh
executable file
·153 lines (112 loc) · 3.94 KB
/
deploy-latest.sh
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env bash
# Environment variables:
# 1. METAKGP_WIKI_PATH => Path to the cloned metakgp/metakgp-wiki repository
# 2. SLACK_NOTIFICATIONS_URL => Webhook URL to which notifications will be sent as a Post request
set -xe
# TODO:
# 1. Include the real username (or at least users who are logged in to the server)
# 2. Include the old commit and the commit that is going to be deployed
deploy_message () {
local action=$1
case $action in
"deploy_start")
echo "Deploy begin: User $(whoami) is deploying to metakgp-wiki"
;;
"downtime_start")
echo "Downtime begin"
;;
"downtime_end")
echo "Downtime end"
;;
"deploy_end")
echo "Deploy end"
;;
esac
}
notify_slack () {
message="${1}"
if [[ -n "$SLACK_NOTIFICATIONS_URL" ]];
then
curl -s -H 'content-type: application/json' \
-d "{ \"text\": \"${message}\" }" \
"$SLACK_NOTIFICATIONS_URL"
fi
}
# TODO: Better as parameters to the deploy function?
base_branch="master"
deploy_branch="origin/master"
deploy () {
local go="${1}"
if [[ "$go" == "--go" ]];
then
notify_slack "$(deploy_message deploy_start)"
fi
local source_dir=$(pwd)
echo "START: metakgp-wiki deploy"
local config_path=${METAKGP_WIKI_PATH:-/root/metakgp-wiki}
cd "$config_path"
echo "STEP: Ensure that current branch is $base_branch"
local branch=$(git rev-parse --abbrev-ref HEAD)
[[ "$branch" == "$base_branch" ]]
echo "STEP: Update all branches"
git remote update
echo "STEP: Check if there are any changes that need to be deployed"
git --no-pager diff --exit-code $base_branch > /dev/null
local docker_compose="docker compose"
local docker="docker"
[[ -x "$(which ${docker})" ]]
[[ -x "$(which ${docker_compose})" ]]
echo "STEP: Running backup job"
local docker_compose_override="${docker_compose} -f docker-compose.prod.yml"
local backup_container_exec="${docker_compose_override} exec backup"
${backup_container_exec} ./run_backup.sh 2>/dev/null
echo "STEP: Copy backup and store inside $source_dir/.backups"
mkdir -p "$source_dir/.backups"
# TODO: Strange head commands to get rid of the newlines at the end. Use gawk instead?
backup_archive="$(${backup_container_exec} ls -1 -t /root/backups 2>/dev/null | head -1 | head -c -2)"
backup_container_name=$(${docker} ps --format '{{ .Names }}' | grep backup | head -1)
backup_path="$backup_container_name:/root/backups/$backup_archive"
docker cp "$backup_path" "$source_dir/.backups"
echo "STEP: Current deployed version"
git log --oneline | head -n1
if [[ "$go" != "--go" ]];
then
echo "DRY RUN: Stopping deploy; Run with --go to continue beyond this point"
return 0
fi
echo "STEP: Merge branch and build Docker images"
git merge --ff-only $deploy_branch
local docker_compose_override="${docker_compose} -f docker-compose.prod.yml"
echo "STEP: Build docker images for the new configuration"
${docker_compose_override} build
notify_slack "$(deploy_message downtime_start)"
echo "STEP: Bring all containers down"
${docker_compose_override} down
echo "STEP: All containers are down (Downtime begin) $(date +%s)"
local volume_metakgp="metakgp-wiki_mediawiki-volume"
docker volume rm "$volume_metakgp"
echo "STEP: Bring all containers up"
${docker_compose_override} up -d
echo "STEP: All containers are up (Downtime end) $(date +%s)"
notify_slack "$(deploy_message downtime_end)"
echo "STEP: Run maintenance/update.php to update DB schema, if required"
local php_container_exec="${docker_compose_override} exec mediawiki"
${php_container_exec} /srv/mediawiki/maintenance/run.php update --quick
notify_slack "$(deploy_message deploy_end)"
cat <<EOF
END: Deployed metakgp-wiki
Don't forget to try logging in and editing a page to make sure the wiki is working
| Home page | https://metakgp.org/
| Random page | https://metakgp.org/w/Special:Random
EOF
}
if [[ "$1" == "-h" || "$1" == "--help" ]];
then
echo "./deploy-latest.sh [--go]"
exit 0
fi
source_dir=$(pwd)
deploy $1
exit_code=$?
cd "$source_dir"
exit ${exit_code}