This repository has been archived by the owner on May 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 178
/
deploy.sh
executable file
·148 lines (109 loc) · 3.76 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
#!/bin/bash
set -eu
HOST="yuca.yunity.org"
TYPE="$1"
REF="${2:-master}"
# optionally available from the environment
SLACK_WEBHOOK_URL="${SLACK_WEBHOOK_URL:-}"
if [ -z "$REF" ]; then
echo "Usage: <type(release|dev|branch)> [<ref>]"
exit 1
fi
if [ "$TYPE" == "release" ]; then
# release
DIR="release"
DEPLOY_ENV="production"
DEPLOY_EMOJI=":rocket:"
URL="https://karrot.world"
APK_URL="https://karrot.world/app.apk"
elif [ "$TYPE" == "dev" ]; then
# dev
if [ "$REF" != "master" ]; then
# technically we could deploy other branches
# but the dir name is setup to be "master"
echo "Error: dev deployment must be of master branch"
echo "(it could be implemented that you can deploy other branches, but is not right now)"
exit 1
fi
DIR="master"
DEPLOY_ENV="development"
DEPLOY_EMOJI=":beer:"
URL="https://dev.karrot.world"
APK_URL="https://dev.karrot.world/app.apk"
STORYBOOK_URL="https://storybook.karrot.world"
DEPLOY_DOCS="true"
elif [ "$TYPE" == "branch" ]; then
if [ -z "$REF" ]; then
echo "Error: you must specify ref for branch releases"
echo "Usage: <type(release|dev|branch)> [<ref>]"
exit 1
fi
# branch deployment
SAFE_DIR="$(echo -n "$REF" | tr -c '[a-zA-Z0-9]_-' '_' | tr "[:upper:]" "[:lower:]")"
DIR="branches/$SAFE_DIR"
DEPLOY_ENV="branch/$REF"
DEPLOY_EMOJI=":construction_worker:"
URL="https://$REF.dev.karrot.world"
APK_URL=
STORYBOOK_URL=
DEPLOY_DOCS="true"
else
echo "Invalid type, must be one of release|dev|branch"
exit 1
fi
ISO_DATE=$(date -Idate)
REPO_URL="https://github.com/yunity/karrot-frontend"
COMMIT_SHA=$(git rev-parse HEAD)
COMMIT_SHA_SHORT=$(git rev-parse --short HEAD)
REF_URL="$REPO_URL/tree/$REF"
COMMIT_URL="$REPO_URL/tree/$COMMIT_SHA"
mkdir -p ~/.ssh
ssh-keyscan -H $HOST >> ~/.ssh/known_hosts
echo "deploying frontend branch [$REF] to [$HOST] in [$DIR] dir"
about_json=$(printf '{
"commitSHA": "%s",
"commitSHAShort": "%s",
"ref": "%s",
"env": "%s",
"apkUrl": "%s",
"date": "%s"
}' "$COMMIT_SHA" "$COMMIT_SHA_SHORT" "$REF" "$DEPLOY_ENV" "$APK_URL" "$ISO_DATE")
echo "$about_json" > dist/about.json
# send it all to the host
rsync -avz --delete dist/ "deploy@$HOST:karrot-frontend/$DIR/"
if [ ! -z "$STORYBOOK_URL" ]; then
echo "$about_json" > storybook-static/about.json
rsync -avz --delete storybook-static/ "deploy@$HOST:karrot-frontend-storybook/$DIR/"
fi
if [ "$DEPLOY_DOCS" == "true" ] && [ -d docs-dist/gitbook ]; then
rsync -avz --delete docs-dist/ "deploy@$HOST:karrot-docs/$DIR/"
fi
if [ ! -z "$SLACK_WEBHOOK_URL" ]; then
WEBPACK_URL="$URL/bundlesize.html"
CIRCLE_WORKFLOW_URL="https://circleci.com/gh/yunity/workflows/karrot-frontend/tree/$REF"
COMMIT_MESSAGE=$(git log -1 --pretty="%s - %an")
ATTACHMENT_TEXT=""
ATTACHMENT_TEXT+=":karrot: <$URL|Visit the site>"
if [ ! -z "$STORYBOOK_URL" ]; then
ATTACHMENT_TEXT+="\n:books: <$STORYBOOK_URL|Visit the storybook>"
fi
ATTACHMENT_TEXT+="\n:webpack: <$WEBPACK_URL|Visit the webpack bundle analyzer>"
ATTACHMENT_TEXT+="\n:circleci: <$CIRCLE_WORKFLOW_URL|Visit CircleCI>"
if [ "$DEPLOY_DOCS" == "true" ] && [ -d docs-dist ]; then
DOCBOOK_URL="https://docs.karrot.world"
ATTACHMENT_TEXT+="\n:page_facing_up: <$DOCBOOK_URL|View docs>"
fi
ATTACHMENT_FOOTER="Using git ref <$REF_URL|$REF>, commit <$COMMIT_URL|$COMMIT_SHA_SHORT> - $COMMIT_MESSAGE"
payload=$(printf '{
"channel": "#karrot-git",
"username": "deploy",
"text": ":sparkles: Successful deployment of :karrot: *karrot* to _%s_ %s",
"attachments": [
{
"text": "%s",
"footer": "%s"
}
]
}' "$DEPLOY_ENV" "$DEPLOY_EMOJI" "$ATTACHMENT_TEXT" "$ATTACHMENT_FOOTER")
curl -X POST --data-urlencode "payload=$payload" "$SLACK_WEBHOOK_URL"
fi