This repository has been archived by the owner on Dec 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdeploy
137 lines (104 loc) · 3.21 KB
/
deploy
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
#!/bin/sh
set -e
OP_PRODUCTION="<TO FILL>"
OP_STAGING="<TO FILL>"
SLACK_CHANNEL="<TO FILL>"
OP_TOKEN=$(cat /root/.opsession)
cd /root/project
PACKAGE_VERSION=$(cat package.json \
| grep version \
| head -1 \
| awk -F: '{ print $2 }' \
| sed 's/[", ]//g')
PACKAGE_NAME=$(cat package.json \
| grep name \
| head -1 \
| awk -F: '{ print $2 }' \
| sed 's/[", ]//g')
function abort {
echo "Aborting push"
exit
}
read -p "🚀 You are about to push the version $PACKAGE_VERSION Are you sure (y/n)? " -r
if [[ ! $REPLY =~ ^[Yy]$ ]] ;then
abort
fi
read -p "🔎 Do you want to push on staging or prod (default to staging) (staging/production)? " -r
PUSH_ENV_REPLY=${REPLY:-staging}
OP_ITEM="$OP_STAGING"
if [[ $PUSH_ENV_REPLY = "production" ]] ;then
OP_ITEM="$OP_PRODUCTION"
fi
echo ""
echo "🤫 Fetching info from 1pass for ${OP_ITEM}."
TOKEN=$(get_token)
get_op_item() {
eval "op get item $OP_ITEM --fields username,remotepath,host,port --session $OP_TOKEN"
}
OP_ITEM_RESULT=$(get_op_item)
HOST=$(echo $OP_ITEM_RESULT | jq -r ".host")
REMOTE_PATH=$(echo $OP_ITEM_RESULT | jq -r ".remotepath")
USERNAME=$(echo $OP_ITEM_RESULT | jq -r ".username")
PORT=$(echo $OP_ITEM_RESULT | jq -r ".port")
echo ""
echo "You are about to push on $HOST on port $PORT"
echo "You are using user $USERNAME"
echo "Remote path is $REMOTE_PATH"
echo ""
read -p "🚩 Is the above correct (y/n)? " -r
if [[ ! $REPLY =~ ^[Yy]$ ]] ;then
abort
fi
function _file {
FINAL_REMOTE_PATH=$(dirname $REMOTE_PATH/$1)
CMD="rsync -a -e 'ssh -p $PORT' $1$2 $USERNAME@$HOST:$FINAL_REMOTE_PATH";
eval $CMD
}
function _folder {
CMD="rsync -a --delete -e 'ssh -p $PORT' $1$2 $USERNAME@$HOST:$REMOTE_PATH/$1";
eval $CMD
}
npm i
npm run build
echo "🗄 Backing up the database."
eval "ssh -p $PORT $USERNAME@$HOST $REMOTE_PATH/craft db/backup"
echo "😴 Shutting down the website."
eval "ssh -p $PORT $USERNAME@$HOST $REMOTE_PATH/craft off"
echo "📡 Syncing code with servers."
_folder config/
_folder modules/
_folder templates/
_folder translations/
_folder web/assets/fonts/
_folder web/assets/img/
_file web/assets/js/$PACKAGE_NAME.min.js
_file web/assets/js/$PACKAGE_NAME.es5.min.js
_file web/assets/css/$PACKAGE_NAME.min.css
_file web/favicon.ico
_file js.json
_file css.json
_file build.json
_file package.json
_file composer.json
_file composer.lock
echo "File sync done."
echo "🎼 Recompiling auto-load files"
eval "ssh -p $PORT $USERNAME@$HOST $REMOTE_PATH/composer dump-autoload -a"
echo "🗑 Flushing Craft's cache."
eval "ssh -p $PORT $USERNAME@$HOST $REMOTE_PATH/craft cache/flush-all"
echo "☁️ Flushing Cloudflare's cache."
eval "ssh -p $PORT $USERNAME@$HOST $REMOTE_PATH/craft cloudflare/purge/purge-all"
echo "🌞 Waking up the website."
eval "ssh -p $PORT $USERNAME@$HOST $REMOTE_PATH/craft on"
echo "🔥 Version $PACKAGE_VERSION is now online."
if [[ ! -z "$SLACK_CHANNEL" ]]; then
echo "🔔 Notifing in slack"
OP_SLACK_WEBHOOK_RESULT=$(op get item $OP_SLACK_WEBHOOK --fields url --session $OP_TOKEN)
curl --location --request POST $OP_SLACK_WEBHOOK_RESULT \
--header 'Content-Type: application/json' \
--data-raw "{
'channel': '$SLACK_CHANNEL',
'text': '<!channel> v.$PACKAGE_VERSION est en ligne sur $PUSH_ENV_REPLY :fire:'
}
"
fi