-
Notifications
You must be signed in to change notification settings - Fork 2
/
deploy.sh
executable file
·162 lines (126 loc) · 5.8 KB
/
deploy.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
154
155
156
157
158
159
160
161
162
set -ex
export TERM=xterm-color
RED="\033[31m"
GREEN="\033[32m"
BLUE="\033[34m"
ENDCOLOR="\033[0m"
hosts_file="$GITHUB_WORKSPACE/.github/hosts.yml"
APP_NAME=$(echo "$GITHUB_REPOSITORY" | sed 's:.*/::' )
setup_frappe() {
mkdir -p "${HOME}/${APP_NAME}"
rsync -azh "${GITHUB_WORKSPACE}/" "${HOME}/${APP_NAME}"
RSYNC_STATUS=$?
if ! [ $RSYNC_STATUS -eq 0 ]; then
echo -e "${RED}RSYNC: FAILED${ENDCOLOR}"
exit 1
fi
echo -e "${BLUE}RSYNC: FINISHED${ENDCOLOR}"
cd "$HOME"
if [[ -z "$FRAPPE_BRANCH" ]]; then
FRAPPE_BRANCH="version-14"
fi
bench init --frappe-branch "$FRAPPE_BRANCH" --skip-redis-config-generation --no-procfile --no-backups --skip-assets bench
cd bench
bench get-app --skip-assets --resolve-deps "${HOME}/${APP_NAME}"
BUILD_STATUS=$?
if ! [ $BUILD_STATUS -eq 0 ]; then
echo -e "${RED} $APP_NAME BUILD: FAILED${ENDCOLOR}"
exit 1
fi
echo -e "${BLUE} ${APP_NAME} BUILD: FINISHED${ENDCOLOR}"
mkdir -p "${HOME}/release"
rsync -azh apps "${HOME}/release/"
RSYNC_STATUS=$?
if ! [ $RSYNC_STATUS -eq 0 ]; then
echo -e "${RED}RSYNC: FAILED${ENDCOLOR}"
exit 1
fi
echo -e "${BLUE}RSYNC: FINISHED${ENDCOLOR}"
}
remote_execute() {
cmd=$(echo "$1")
ssh "${REMOTE_USER}@${REMOTE_HOST}" "cd $REMOTE_PATH && $cmd"
}
remote_frappe_branch_handle() {
frappe_available=$(remote_execute "[[ -d 'apps/frappe' ]] && echo true")
if ! [ "$frappe_available" == "true" ]; then
# not available then install frappe with the given branch
remote_execute "bench get-app --branch $FRAPPE_BRANCH frappe"
else
# check if the branch is same
frappe_current_branch=$(remote_execute "cd apps/frappe && git branch --show-current")
echo -e "${BLUE}Server Frappe Branch: $frappe_current_branch ${ENDCOLOR}"
if ! [[ "$frappe_current_branch" == "$FRAPPE_BRANCH" ]]; then
remote_execute "bench switch-to-branch --upgrade $FRAPPE_BRANCH frappe"
remote_execute "bench update --apps frappe"
fi
fi
}
remote_deploy_frappe() {
branch=$(echo "$GITHUB_REF" | awk 'BEGIN {FS="/"} ; {print $NF}')
REMOTE_HOST=$(shyaml get-value "${branch}.hostname" < "$hosts_file")
REMOTE_USER=$(shyaml get-value "${branch}.user" < "$hosts_file")
REMOTE_SITE=$(shyaml get-value "${branch}.site_name" < "$hosts_file")
REMOTE_PATH=$(shyaml get-value "${branch}.deploy_path" < "$hosts_file")
ssh-keyscan -H "$REMOTE_HOST" >>/home/frappe/.ssh/known_hosts
# copy apps to releases folder
REMOTE_FOLDER_NAME=$(date +'%d-%m-%y--%H-%M-%S')
remote_execute "mkdir -p releases/${REMOTE_FOLDER_NAME}"
#skip everything related to frappe
rsync -azh --exclude 'frappe' "${HOME}/release/apps/" "${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PATH}/releases/${REMOTE_FOLDER_NAME}/"
RSYNC_STATUS=$?
if ! [ $RSYNC_STATUS -eq 0 ]; then
echo -e "${RED}RSYNC: FAILED${ENDCOLOR}"
exit 1
fi
echo -e "${BLUE}RSYNC: FINISHED${ENDCOLOR}"
# give priority to REMOTE_SITE
# use REMOTE_HOST as fallback
# check if site exist
SITE_EXIST_STRING=$(remote_execute "bench --site ${REMOTE_SITE} scheduler status")
if [[ "$SITE_EXIST_STRING" == *"does not exist!"* ]]; then
echo -e "${RED} Site: ${REMOTE_SITE} does not exist !!${ENDCOLOR}"
exit 1
fi
# getting apps list
APPS_BENCH_LIST=$(remote_execute "ls -1 apps")
BENCH_LIST_OUTPUT=$(remote_execute "bench --site ${REMOTE_SITE} list-apps -f json")
APPS_SITE_LIST=$(echo "$BENCH_LIST_OUTPUT" | jq -cr ".\"${REMOTE_SITE}\" | .[]")
remote_frappe_branch_handle
# site maintenance mode -> on
remote_execute "bench --site ${REMOTE_SITE} set-maintenance-mode on"
for app in $(remote_execute "ls -1 releases/${REMOTE_FOLDER_NAME}"); do
echo -e "Handling app - $app"
#check if the app is installed in bench
#
REGEX_MATCH=\\b$app\\b
if ! [[ "$APPS_BENCH_LIST" =~ $REGEX_MATCH ]]; then
echo -e "Installing $app in bench"
#install the app into bench
remote_execute "bench get-app ${REMOTE_PATH}/releases/${REMOTE_FOLDER_NAME}/$app"
fi
#check if the app is installed in site
if [[ "$APPS_SITE_LIST" =~ $REGEX_MATCH ]]; then
echo -e "Updating $app in $REMOTE_PATH"
remote_execute "rm -rf apps/$app"
remote_execute "cp -r ${REMOTE_PATH}/releases/${REMOTE_FOLDER_NAME}/$app ${REMOTE_PATH}/apps/"
remote_execute "bench build --force --production --app $app"
remote_execute "bench --site ${REMOTE_SITE} migrate"
else
echo -e "Installing $app in $REMOTE_PATH"
remote_execute "bench build --force --production --app $app"
remote_execute "bench --site ${REMOTE_SITE} install-app $app"
remote_execute "bench --site ${REMOTE_SITE} migrate"
fi
# remove node_modules from each apps
remote_execute "rm -rf ${REMOTE_PATH}/releases/${REMOTE_FOLDER_NAME}/${app}/node_modules ${REMOTE_PATH}/releases/${REMOTE_FOLDER_NAME}/${app}/yarn.lock"
echo -e "${BLUE}CLEANUP-> REMOVED: ${REMOTE_PATH}/releases/${REMOTE_FOLDER_NAME}/${app}/node_modules ${REMOTE_PATH}/releases/${REMOTE_FOLDER_NAME}/${app}/yarn.lock${ENDCOLOR}"
done
# site maintenance mode -> on
remote_execute "bench --site ${REMOTE_SITE} set-maintenance-mode off"
# bench restart
remote_execute "bench restart"
#rsync -rv $GIHUB_WORKSPACE
}
setup_frappe
remote_deploy_frappe