-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy.sh
executable file
Β·178 lines (156 loc) Β· 5.51 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/bash
set -e
PROJECT="plan-a-front"
if [ ! $# -ge 2 ]; then
echo "π Usage: $0 branch/tag goal [--update-nginx-conf] [--update-apache-conf] IRL: $0 feature/introduce_bug prod"
exit 1
fi
# Be aware that if you change next line see line 15 & 117 !
TEMP=$(mktemp -d)
REPOSITORY=$(pwd)
DISTANT_REPO="git@git.unistra.fr:di/plan_a/front.git" #$(git config --get remote.origin.url)
WORKING_DIR="$TEMP/git-clone"
DEST_PATH="/var/www/static/plan_a/"
TEST_HOSTS=("root@django-test2.di.unistra.fr")
TEST_NGINX_CONF="plana-test.app.unistra.fr"
PREPROD_HOSTS=("root@rp-dip-pprd-public.di.unistra.fr")
PREPROD_NGINX_CONF="plana-pprd.app.unistra.fr"
PROD_HOSTS=("root@rp-dip-public-m.di.unistra.fr" "root@rp-dip-public-s.di.unistra.fr")
PROD_NGINX_CONF="etu-campulse.fr"
DEMO_HOSTS=("root@rp-shib3-pprd-1.srv.unistra.fr" "root@rp-shib3-pprd-2.srv.unistra.fr")
DEMO_APACHE_CONF="campulse-demo.unistra.fr"
# Json info file template
TEMPLATE='{"info":{"app_host":"%s","repo_url":"%s","local_user":"%s","tag":"%s","commit_id":"%s"}}'
# Set env
ENVIRONMENT="$2"
# Shall we install nginx config files ?
SETUP_NGINX=true
# Shall we install apache config files ?
SETUP_APACHE=false
# Shall we use sentry ?
# if so sentry-cli is required !!!!
USE_SENTRY=true
# Special conf for demo
if [ $ENVIRONMENT == "demo" ]; then
SETUP_NGINX=false
SETUP_APACHE=true
DEST_PATH="/var/www/static/campulse-demo/"
fi
case "$ENVIRONMENT" in
test)
TARGET=("${TEST_HOSTS[@]}")
HOST="$TEST_NGINX_CONF"
TARGET_NGINX_CONF="$HOST.conf"
SOURCE_ENV_FILE=".env.deploy_test"
;;
preprod)
TARGET=("${PREPROD_HOSTS[@]}")
HOST="$PREPROD_NGINX_CONF"
TARGET_NGINX_CONF="$HOST.conf"
SOURCE_ENV_FILE=".env.deploy_pprd"
;;
prod)
TARGET=("${PROD_HOSTS[@]}")
HOST="$PROD_NGINX_CONF"
TARGET_NGINX_CONF="$HOST.conf"
SOURCE_ENV_FILE=".env.deploy_prod"
;;
demo)
TARGET=("${DEMO_HOSTS[@]}")
HOST="$DEMO_APACHE_CONF"
TARGET_APACHE_CONF="$HOST.conf"
SOURCE_ENV_FILE=".env.deploy_demo"
;;
esac
echo "ENV : $2"
echo "Branch/Tag : $1"
echo "Target : ${TARGET[@]}"
# Shall we install nginx config ?
if [ "$SETUP_NGINX" == true ]; then
for i in "${TARGET[@]}"; do
if ! ssh -q "$i" test -L "/etc/nginx/sites-enabled/$TARGET_NGINX_CONF"; then
echo "π Setup nginx vhost for $i"
scp -r "nginx/$TARGET_NGINX_CONF" "$i:/etc/nginx/sites-available/"
# TODO: using systemctl instead of service ?
ssh -q "$i" ln -s "/etc/nginx/sites-available/$TARGET_NGINX_CONF /etc/nginx/sites-enabled/$TARGET_NGINX_CONF && service nginx reload"
else
for j in "$@"; do
if [ "$j" == "--update-nginx-conf" ]; then
echo "π Update nginx vhost for $i"
scp -r "nginx/$TARGET_NGINX_CONF" "$i:/etc/nginx/sites-available/"
ssh -q "$i" service nginx reload
fi
done
fi
done
fi
# Shall we install apache config ?
if [ "$SETUP_APACHE" == true ]; then
for i in "${TARGET[@]}"; do
if ! ssh -q "$i" test -L "/etc/apache2/sites-enabled/$TARGET_APACHE_CONF"; then
echo "π Setup apache vhost for $i"
scp -r "apache/$TARGET_APACHE_CONF" "$i:/etc/apache2/sites-available/"
ssh -q "$i" a2ensite "/etc/apache2/sites-available/$TARGET_APACHE_CONF"
ssh -q "$i" apachectl -t
TEST_CONF=$?
if [ "$TEST_CONF" == 0 ]; then
echo "β»οΈ Reload Apache"
ssh -q "$i" service apache2 reload
fi
else
for j in "$@"; do
if [ "$j" == "--update-apache-conf" ]; then
echo "π Update apache vhost for $i"
scp -r "apache/$TARGET_APACHE_CONF" "$i:/etc/apache2/sites-available/"
ssh -q "$i" apachectl -t
TEST_CONF=$?
if [ "$TEST_CONF" == 0 ]; then
echo "β»οΈ Reload Apache"
ssh -q "$i" service apache2 reload
fi
fi
done
fi
done
fi
cd "$TEMP"
echo "π Cloning repository on target tag/branch"
git clone -b "$1" --single-branch "$REPOSITORY" "$WORKING_DIR"
cd "$WORKING_DIR"
echo
# Get last commit id
echo "π Installing npm dependencies"
npm ci
echo "π¦ Packaging stuff"
# shellcheck source=/dev/null
. "${SOURCE_ENV_FILE}"
npm run build:$ENVIRONMENT
PROJECT_VERSION=$(git describe --always --tags)
# Create info file for app
echo $(printf "$TEMPLATE" "$HOST" "$DISTANT_REPO" "$(whoami)" "$1" "$PROJECT_VERSION") > "dist/${PROJECT}_info.json"
echo "π Deploying files"
for i in "${TARGET[@]}"; do
echo "Scp files to $i"
ssh -q "$i" mkdir -p $DEST_PATH
rsync -avzhe ssh --progress --delete "dist/" "$i:$DEST_PATH"
done
# SENTRY
# Sentry needs gitlab repository to be origin
if [ "$USE_SENTRY" == true ]; then
echo "π§ Manipulating git distant repositories"
git remote remove origin
git remote add origin "$DISTANT_REPO"
PROJECT_VERSION=$(git describe --always --tags)
# Create a release
echo "π Telling about $PROJECT_VERSION to Sentry"
sentry-cli releases new -p "$PROJECT" "$PROJECT_VERSION"
# Associate commits with the release
echo "π€ Associating commits to version"
sentry-cli releases set-commits --auto "$PROJECT_VERSION"
# Declare deployment
echo "π Telling Sentry that we are deploying $PROJECT_VERSION in $ENVIRONMENT"
sentry-cli releases deploys "$PROJECT_VERSION" new -e "$ENVIRONMENT"
fi
# Clean working dir
cd .. && rm -rf "$WORKING_DIR"
echo "π $PROJECT $PROJECT_VERSION successfully deployed"