-
Notifications
You must be signed in to change notification settings - Fork 23
/
gh-deploy.sh
276 lines (205 loc) · 9.15 KB
/
gh-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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env bash
# TODO
#===================
# provide a clean way to define/check the "current" version of node (i.e., the one we should execute the publish under/for)
## ideally we should read it from .nvmrc
# for local deployment, instead of using stark-ssh.enc, we should use the GITHUB_API_KEY key passed via --github-api-key=foo
## if present then we could push directly without handling decryption, etc
set -u -e -o pipefail
VERBOSE=false
TRACE=false
DRY_RUN=false
ENFORCE_SHOWCASE_VERSION_CHECK=true
TARGET_BRANCH="gh-pages"
COMMIT_HASH=`git rev-parse --verify HEAD`
TARGET_REPO="github.com/NationalBankBelgium/stark.git"
EXPECTED_REPOSITORY="NationalBankBelgium/stark"
GH_ACTIONS_TAG=${GH_ACTIONS_TAG:-""}
GITHUB_EVENT_NAME=${GITHUB_EVENT_NAME:-""}
COMMIT_AUTHOR_USERNAME="Github-Actions-CI"
COMMIT_AUTHOR_EMAIL="alexis.georges@nbb.be"
STARK_CORE="stark-core"
STARK_UI="stark-ui"
STARK_RBAC="stark-rbac"
SHOWCASE="showcase"
API_DOCS_DIR_NAME="api-docs"
LATEST_DIR_NAME="latest"
#----------------------------------------------
# Uncomment and adapt block below to test locally
#----------------------------------------------
#LOGS_DIR=./.tmp/stark/logs
#mkdir -p ${LOGS_DIR}
#LOGS_FILE=${LOGS_DIR}/build-perf.log
#touch ${LOGS_FILE}
#DRY_RUN=true
#GITHUB_ACTIONS=true
#GITHUB_ACTOR="SuperITMan"
#GITHUB_SHA=${COMMIT_HASH}
#GITHUB_REPOSITORY="NationalBankBelgium/stark" # yes we're always on the correct repo
#ENFORCE_SHOWCASE_VERSION_CHECK=false # allows not have consistency between tag version and showcase version
# Point to a fork or any other repo
#TARGET_REPO="github.com/superitman/stark.git"
# Avoid messing up Git config (even though limited to the current repo)
#COMMIT_AUTHOR_USERNAME="Alexis Georges"
#COMMIT_AUTHOR_EMAIL="alexis.georges@nbb.be"
# For PRs (NOT accepted)
#GITHUB_EVENT_NAME="pull_request"
# For nightly builds (NOT accepted)
#GITHUB_EVENT_TYPE="schedule"
# For releases
#GH_ACTIONS_TAG="barFoo"
#GITHUB_EVENT_NAME="push"
#----------------------------------------------
readonly currentDir=$(cd $(dirname $0); pwd)
source ${currentDir}/scripts/ci/_ghactions-group.sh
source ${currentDir}/util-functions.sh
cd ${currentDir}
logInfo "============================================="
logInfo "Stark docs publish @ github pages"
for ARG in "$@"; do
case "$ARG" in
--dry-run)
logInfo "============================================="
logInfo "Dry run enabled!"
DRY_RUN=true
;;
--verbose)
logInfo "============================================="
logInfo "Verbose mode enabled!"
VERBOSE=true
;;
--trace)
logInfo "============================================="
logInfo "Trace mode enabled!"
TRACE=true
;;
--github-api-key=*)
logInfo "============================================="
logInfo "Github API key provided"
GITHUB_API_KEY=${ARG#--github-api-key=}
;;
*)
echo "Unknown option $ARG."
exit 1
;;
esac
done
logInfo "============================================="
PROJECT_ROOT_DIR=`pwd`
logTrace "PROJECT_ROOT_DIR: ${PROJECT_ROOT_DIR}" 1
ROOT_PACKAGES_DIR=${PROJECT_ROOT_DIR}/dist/packages-dist
logTrace "ROOT_PACKAGES_DIR: ${ROOT_PACKAGES_DIR}" 1
API_DOCS_SOURCE_DIR=${PROJECT_ROOT_DIR}/reports/${API_DOCS_DIR_NAME}
logTrace "API_DOCS_SOURCE_DIR: ${API_DOCS_SOURCE_DIR}"
SHOWCASE_SOURCE_DIR=${PROJECT_ROOT_DIR}/${SHOWCASE}/dist
logTrace "SHOWCASE_SOURCE_DIR: ${SHOWCASE_SOURCE_DIR}"
DOCS_WORK_DIR=${PROJECT_ROOT_DIR}/.tmp/ghpages
logTrace "DOCS_WORK_DIR: ${DOCS_WORK_DIR}" 1
logTrace "Cleaning the docs work directory..." 2
rm -rf ${DOCS_WORK_DIR}
mkdir -p ${DOCS_WORK_DIR}
ghActionsGroupStart "docs publication checks" "no-xtrace"
if [[ ${GITHUB_ACTIONS} == true ]]; then
logInfo "Publishing docs to GH pages";
logInfo "============================================="
# Don't even try if not running against the official repo
# We don't want docs publish to run outside of our own little world
if [[ ${GITHUB_REPOSITORY} != ${EXPECTED_REPOSITORY} ]]; then
logInfo "Skipping release because this is not the main repository.";
exit 0;
fi
if [[ ${GITHUB_EVENT_NAME} == "pull_request" ]]; then
logInfo "Not publishing because this is a build triggered for a pull request" 1
exit 0;
fi
if [[ ${GITHUB_EVENT_NAME} == "schedule" ]]; then
logInfo "Not publishing because this is a build triggered for a nightly build" 1
exit 0;
fi
if [[ ${GH_ACTIONS_TAG} == "" ]]; then
logInfo "Not publishing because this is not a build triggered for a tag" 1
exit 0;
else
logInfo "OK, this build has been triggered for a tag"
fi
else
logInfo "Not publishing because we are not in GitHub Actions. Currently that is the only supported option!"
exit 0
fi
ghActionsGroupEnd "docs publication checks"
ghActionsGroupStart "docs generation" "no-xtrace"
logInfo "Generating API docs"
npm run docs:stark-core:generate
npm run docs:stark-ui:generate
npm run docs:stark-rbac:generate
logTrace "API docs generated successfully" 1
ghActionsGroupEnd "docs generation" "no-xtrace"
ghActionsGroupStart "docs publication" "no-xtrace"
logInfo "Publishing API docs"
logTrace "Determining target folders for api docs" 1
DOCS_VERSION=${GH_ACTIONS_TAG}
SHOWCASE_PACKAGE_VERSION=$(node -p "require('./package.json').version")
#alternative (faster but less safe): SHOWCASE_PACKAGE_VERSION=$(sed -nE 's/^\s*"version": "(.*?)",$/\1/p' package.json)
if [[ ${ENFORCE_SHOWCASE_VERSION_CHECK} == true ]]; then
logTrace "Checking for version consistency between the tag and the showcase" 1
if [[ ${DOCS_VERSION} != ${SHOWCASE_PACKAGE_VERSION} ]]; then
logInfo "Cannot publish the documentation because the showcase version does not match the tagged version (tag name). Please update the showcase!"
exit -1;
fi
fi
logTrace "Version for which we are producing docs: ${DOCS_VERSION}"
if [[ ${GH_ACTIONS_TAG} =~ .*-(alpha|beta|rc).* ]]; then
logTrace "This is a pre-release version, we will publish the docs under the 'next' folder"
LATEST_DIR_NAME="next"
fi
logTrace "Cloning stark's github pages branch to ${DOCS_WORK_DIR}"
AUTHENTICATED_TARGET_REPO=https://${GITHUB_ACTOR}:${GITHUB_API_KEY}@${TARGET_REPO}
git clone --quiet --depth=1 --branch=${TARGET_BRANCH} ${AUTHENTICATED_TARGET_REPO} ${DOCS_WORK_DIR}
API_DOCS_TARGET_DIR_STARK_CORE=${DOCS_WORK_DIR}/${API_DOCS_DIR_NAME}/${STARK_CORE}/${DOCS_VERSION}
API_DOCS_TARGET_DIR_STARK_CORE_LATEST=${DOCS_WORK_DIR}/${API_DOCS_DIR_NAME}/${STARK_CORE}/${LATEST_DIR_NAME}
API_DOCS_TARGET_DIR_STARK_UI=${DOCS_WORK_DIR}/${API_DOCS_DIR_NAME}/${STARK_UI}/${DOCS_VERSION}
API_DOCS_TARGET_DIR_STARK_UI_LATEST=${DOCS_WORK_DIR}/${API_DOCS_DIR_NAME}/${STARK_UI}/${LATEST_DIR_NAME}
API_DOCS_TARGET_DIR_STARK_RBAC=${DOCS_WORK_DIR}/${API_DOCS_DIR_NAME}/${STARK_RBAC}/${DOCS_VERSION}
API_DOCS_TARGET_DIR_STARK_RBAC_LATEST=${DOCS_WORK_DIR}/${API_DOCS_DIR_NAME}/${STARK_RBAC}/${LATEST_DIR_NAME}
SHOWCASE_TARGET_DIR=${DOCS_WORK_DIR}/${SHOWCASE}/${DOCS_VERSION}
SHOWCASE_TARGET_DIR_LATEST=${DOCS_WORK_DIR}/${SHOWCASE}/${LATEST_DIR_NAME}
logTrace "Cleaning all '${LATEST_DIR_NAME}' directories from API docs and showcase..." 2
rm -rf ${API_DOCS_TARGET_DIR_STARK_CORE_LATEST}
mkdir -p ${API_DOCS_TARGET_DIR_STARK_CORE_LATEST}
rm -rf ${API_DOCS_TARGET_DIR_STARK_UI_LATEST}
mkdir -p ${API_DOCS_TARGET_DIR_STARK_UI_LATEST}
rm -rf ${SHOWCASE_TARGET_DIR_LATEST}
mkdir -p ${SHOWCASE_TARGET_DIR_LATEST}
logTrace "Copying the API docs"
syncOptions=(--archive --delete --ignore-errors --quiet --include="**/**") # we overwrite docs if they're present already for this version
logTrace "Copying ${STARK_CORE} API docs"
syncFiles ${API_DOCS_SOURCE_DIR}/${STARK_CORE} ${API_DOCS_TARGET_DIR_STARK_CORE} "${syncOptions[@]}"
syncFiles ${API_DOCS_SOURCE_DIR}/${STARK_CORE} ${API_DOCS_TARGET_DIR_STARK_CORE_LATEST} "${syncOptions[@]}"
logTrace "Copying ${STARK_UI} API docs"
syncFiles ${API_DOCS_SOURCE_DIR}/${STARK_UI} ${API_DOCS_TARGET_DIR_STARK_UI} "${syncOptions[@]}"
syncFiles ${API_DOCS_SOURCE_DIR}/${STARK_UI} ${API_DOCS_TARGET_DIR_STARK_UI_LATEST} "${syncOptions[@]}"
logTrace "Copying ${STARK_RBAC} API docs"
syncFiles ${API_DOCS_SOURCE_DIR}/${STARK_RBAC} ${API_DOCS_TARGET_DIR_STARK_RBAC} "${syncOptions[@]}"
syncFiles ${API_DOCS_SOURCE_DIR}/${STARK_RBAC} ${API_DOCS_TARGET_DIR_STARK_RBAC_LATEST} "${syncOptions[@]}"
logTrace "Copying ${SHOWCASE}"
NODE_REPLACE_URLS="node ${PROJECT_ROOT_DIR}/${SHOWCASE}/ghpages-adapt-bundle-urls.js"
$NODE_REPLACE_URLS "${LATEST_DIR_NAME}"
syncFiles ${SHOWCASE_SOURCE_DIR} ${SHOWCASE_TARGET_DIR_LATEST} "${syncOptions[@]}"
$NODE_REPLACE_URLS "${DOCS_VERSION}" "${LATEST_DIR_NAME}"
syncFiles ${SHOWCASE_SOURCE_DIR} ${SHOWCASE_TARGET_DIR} "${syncOptions[@]}"
unset syncOptions
logInfo "Pushing the docs to GitHub pages"
cd ${DOCS_WORK_DIR}
git add -A &> /dev/null # way too long
if [[ ${GITHUB_ACTIONS} == true ]]; then
logTrace "Configuring Git for GitHub Actions"
git config user.name ${COMMIT_AUTHOR_USERNAME}
git config user.email ${COMMIT_AUTHOR_EMAIL}
fi
git commit --quiet -m "Publishing docs for version: ${DOCS_VERSION}"
git push --quiet --force
cd - > /dev/null
logInfo "Documentation published successfully!"
ghActionsGroupEnd "docs publication"
# Print return arrows as a log separator
ghActionsGroupReturnArrows