-
Notifications
You must be signed in to change notification settings - Fork 26
/
entrypoint.sh
executable file
·318 lines (256 loc) · 8.78 KB
/
entrypoint.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/bin/bash
[ "${DEBUG:-false}" = true ] && set -x
set -euo pipefail
# Vars
# ----
# Script environments
: "${ARCHI_PROJECT_PATH:=${GITHUB_WORKSPACE:-${CI_PROJECT_DIR:-/archi/project}}}"
: "${ARCHI_REPORT_PATH:=/archi/report}"
: "${ARCHI_HTML_REPORT_ENABLED:=true}"
: "${ARCHI_JASPER_REPORT_ENABLED:=false}"
: "${ARCHI_JASPER_REPORT_FORMATS:=PDF,DOCX}"
: "${ARCHI_CSV_REPORT_ENABLED:=false}"
: "${ARCHI_EXPORT_MODEL_ENABLED:=true}"
: "${ARCHI_APP:=com.archimatetool.commandline.app}"
: "${GITHUB_SERVER_URL:=https://github.com}"
: "${GITHUB_PAGES_BRANCH:=gh-pages}"
: "${GIT_SUBTREE_PREFIX:=.archi_report}"
# Tools environments
declare -a _ssh_args=(
-o BatchMode=yes
-o UserKnownHostsFile=/dev/null
-o StrictHostKeyChecking=no
)
GIT_SSH_COMMAND="ssh ${_ssh_args[*]}"
GIT_TERMINAL_PROMPT=0
DISPLAY=:1
export GIT_SSH_COMMAND GIT_TERMINAL_PROMPT DISPLAY
# Regex
_re_url='([\w.@\:/~\-]+)(\.git)(\/)?'
_re_proto_http='(http(s)?(:(\/){0,3}))?'
_re_proto_ssh='((((git|user)@[\w.-]+)|(git|ssh))(:(\/){0,3}))'
# Functions
# ---------
# Run Archi
archi_run() {
local -a _args=()
# Html report
[ "${ARCHI_HTML_REPORT_ENABLED,,}" == true ] &&
_args+=(
--html.createReport
"${ARCHI_HTML_REPORT_PATH:=$ARCHI_REPORT_PATH/html}"
)
# CSV report
[ "${ARCHI_CSV_REPORT_ENABLED,,}" == true ] &&
_args+=(
--csv.export
"${ARCHI_CSV_REPORT_PATH:=$ARCHI_REPORT_PATH/csv}"
)
# Export model
[ "${ARCHI_EXPORT_MODEL_ENABLED,,}" == true ] &&
_args+=(
--saveModel
"${ARCHI_EXPORT_MODEL_PATH:=$ARCHI_REPORT_PATH}/$_project.archimate"
)
# Jasper report
[ "${ARCHI_JASPER_REPORT_ENABLED,,}" == true ] &&
_args+=(
--jasper.createReport
"${ARCHI_JASPER_REPORT_PATH:=$ARCHI_REPORT_PATH/jasper}"
--jasper.format
"$ARCHI_JASPER_REPORT_FORMATS"
--jasper.filename
"$_project"
--jasper.title
"${ARCHI_JASPER_REPORT_TITLE:=$_project}"
)
# Run Archi
xvfb-run \
/opt/Archi/Archi -application "$ARCHI_APP" -consoleLog -nosplash \
--modelrepository.loadModel "$ARCHI_PROJECT_PATH" "${_args[@]}" &&
printf '\n%s\n\n' "Done. Reports saved to $ARCHI_REPORT_PATH"
}
# Check first argument match regex present in second argument
re_match() {
local value="${1:-}" regex="${2:-.*}"
[ -n "$value" ] && grep -Pq "$regex" <<<"$value" && return 0
return 1
}
# Encode url symbols
urlencode() {
local LC_COLLATE=C length="${#1}"
for ((i = 0; i < length; i++)); do
local c="${1:$i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf '%s' "$c" ;;
*) printf '%%%02X' "'$c" ;;
esac
done
}
update_html() {
if [ "${ARCHI_CSV_REPORT_ENABLED,,}" == true ]; then
for item in {elements,properties,relations}; do
_li="<li><a href=\"${item}.csv\" class=\"go\">${item^}</a></li>"
sed "/modal.*i18n-about/i $_li" -i "$ARCHI_REPORT_PATH/index.html"
done
fi
if [ "${ARCHI_JASPER_REPORT_ENABLED,,}" == true ]; then
for item in ${ARCHI_JASPER_REPORT_FORMATS//,/ }; do
_li="<li><a href=\"$_project.${item,,}\" class=\"go\">${item^^}</a></li>"
sed "/modal.*i18n-about/i $_li" -i "$ARCHI_REPORT_PATH/index.html"
done
fi
if [ "${ARCHI_EXPORT_MODEL_ENABLED,,}" == true ]; then
_li="<li><a href=\"$_project.archimate\" class=\"go\">Model</a></li>"
sed "/modal.*i18n-about/i $_li" -i "$ARCHI_REPORT_PATH/index.html"
fi
}
# Git clone wrap
git_clone() { git clone "${1:?Repo url not set}" "$ARCHI_PROJECT_PATH"; }
# GitLab log separator
section_start () {
printf '\e[0Ksection_start:%s:%s[collapsed=true]\r\e[0K\e[1;36m%s\e[0m\n' \
"$(date +%s)" "$1" "${*:2}"
}
section_end () {
printf '\e[0Ksection_end:%s:%s\r\e[0K\n' "$(date +%s)" "$*"
}
# Fail message to stder and exit 1
fail() { printf >&2 '%s\n' "$*"; exit 1; }
# Main
# ----
# Run custom archi command
if [ "$#" -ge 1 ]; then
echo "Execute Archi with _args: $*"
xvfb-run \
/opt/Archi/Archi -application "$ARCHI_APP" -consoleLog -nosplash "$@"
exit 0
fi
# Get project title
if [ -f "$ARCHI_PROJECT_PATH/model/folder.xml" ]; then
_project="$(
grep -Pom1 'name="\K([^"]*)' \
"$ARCHI_PROJECT_PATH/model/folder.xml" | \
head -1
)"
elif [ -n "${GIT_REPOSITORY:-}" ]; then
_project="$(basename -s .git "$GIT_REPOSITORY")"
else
_project='Model'
fi
# Run in GitHub actions
if [ "${GITHUB_ACTIONS:-}" == true ]; then
echo "Run Archi report generation in GitHub actions"
# Prepare vars
: "${GITHUB_REPOSITORY:?Repository name not set}"
GITHUB_TOKEN="$(urlencode "${GITHUB_TOKEN:?Token not specified}")"
# Create repository url with token
_gh_repo="${GITHUB_SERVER_URL//:\/\/*}://" # Protocol
_gh_repo+="x-access-token:$GITHUB_TOKEN@" # Auth
_gh_repo+="${GITHUB_SERVER_URL//*\/\/}/$GITHUB_REPOSITORY.git" # URL
# Set actions specified paths
ARCHI_REPORT_PATH="$ARCHI_PROJECT_PATH/$GIT_SUBTREE_PREFIX"
ARCHI_HTML_REPORT_PATH="$ARCHI_REPORT_PATH"
ARCHI_CSV_REPORT_PATH="$ARCHI_REPORT_PATH"
ARCHI_JASPER_REPORT_PATH="$ARCHI_REPORT_PATH"
ARCHI_EXPORT_MODEL_PATH="$ARCHI_REPORT_PATH"
cd "$ARCHI_PROJECT_PATH" && mkdir -p "$ARCHI_REPORT_PATH"
# Create CNAME for custon domain
[ -n "${GITHUB_PAGES_DOMAIN:-}" ] &&
echo "$GITHUB_PAGES_DOMAIN" > "$ARCHI_REPORT_PATH/CNAME"
# Disable Jekyll
touch "$ARCHI_REPORT_PATH/.nojekyll"
# Change git repo settings
# git remote set-url origin "$_gh_repo"
! git config --get user.name >/dev/null &&
git config --global user.name "${GITHUB_ACTOR:-nobody}"
! git config --get user.email >/dev/null &&
git config --global user.email \
"${GITHUB_ACTOR:-nobody}@users.noreply.${GITHUB_SERVER_URL//*\/\/}"
git config --global --add safe.directory "$ARCHI_REPORT_PATH"
git config --global --add safe.directory "$ARCHI_PROJECT_PATH"
# Create report
archi_run
[ "${ARCHI_HTML_REPORT_ENABLED,,}" == true ] && update_html
# Commit and push subtree
git add --force "$GIT_SUBTREE_PREFIX"
git commit --message "Archimate report ${GITHUB_ACTION:-0}:${GITHUB_JOB:-0}"
_subtree="$(
git subtree split --prefix "$GIT_SUBTREE_PREFIX" "$GITHUB_REF_NAME"
)"
git push origin "$_subtree:$GITHUB_PAGES_BRANCH" --force
exit 0
fi
# Run in GitLab CI
if [ "${GITLAB_CI:-}" == true ]; then
echo "Run Archi report generation in GitLab CI"
section_start 'archi_report' 'Render ArchiMate report'
# Set actions specified paths
ARCHI_REPORT_PATH="$CI_PROJECT_DIR/public"
ARCHI_HTML_REPORT_PATH="$CI_PROJECT_DIR/public"
ARCHI_CSV_REPORT_PATH="$CI_PROJECT_DIR/public"
ARCHI_JASPER_REPORT_PATH="$CI_PROJECT_DIR/public"
ARCHI_EXPORT_MODEL_PATH="$CI_PROJECT_DIR/public"
cd "$ARCHI_PROJECT_PATH" && mkdir -p "$ARCHI_REPORT_PATH"
# Create report
archi_run
[ "${ARCHI_HTML_REPORT_ENABLED,,}" == true ] && update_html
section_end 'Render ArchiMate report complete'
exit 0
fi
# Check and use exist or mounted model
if [ -f "$ARCHI_PROJECT_PATH/model/folder.xml" ]; then
echo "Work with exist model in $ARCHI_PROJECT_PATH directory"
# Try update exist model
{
git -C "$ARCHI_PROJECT_PATH" pull &>/dev/null &&
echo "Use actual state of model $_project."
} || :
# Work with remote git repository model
elif [ -n "${GIT_REPOSITORY:-}" ]; then
# Chek URL is SSH and clone
if re_match "${GIT_REPOSITORY:-}" "^$_re_proto_ssh$_re_url$"; then
echo "Clone model from $GIT_REPOSITORY to $ARCHI_PROJECT_PATH dir with ssh"
git_clone "$GIT_REPOSITORY"
# Check URL is HTTP
elif re_match "${GIT_REPOSITORY:-}" "^$_re_proto_http$_re_url$"; then
_proto="${GIT_REPOSITORY%://*}"
# Use token if set
if [ -n "${GIT_TOKEN:-}" ]; then
# Encode symbols
GIT_TOKEN="$(urlencode "$GIT_TOKEN")"
if re_match "${GIT_REPOSITORY:-}" '^https://github.com/'; then
_auth="x-access-token:$GIT_TOKEN"
else
_auth="oauth2:$GIT_TOKEN"
fi
echo \
"Clone model from $GIT_REPOSITORY to $ARCHI_PROJECT_PATH dir use token"
git_clone "$_proto://$_auth@${GIT_REPOSITORY#*://}"
# Use login and password
elif [ -n "${GIT_USERNAME:-}" ] && [ -n "${GIT_PASSWORD:-}" ]; then
# Encode symbols
_auth="$(urlencode "$GIT_USERNAME"):$(urlencode "$GIT_PASSWORD")"
echo \
"Clone model from $GIT_REPOSITORY to $ARCHI_PROJECT_PATH dir use token"
git_clone "$_proto://$_auth@${GIT_REPOSITORY#*://}"
# Use public repository access
else
echo \
"Clone model from repository $GIT_REPOSITORY to $ARCHI_PROJECT_PATH dir"
git_clone "$GIT_REPOSITORY"
fi
# Git URL not valid
else
fail 'Git repository URL not valid. Plese use http or ssh url'
fi
# Exit. Model not exist
else
fail \
"Plese set http or ssh url to git repositorty in \$GIT_REPOSITORY" \
"variable or mount model to \$ARCHI_PROJECT_PATH ($ARCHI_PROJECT_PATH)" \
'directory.'
fi
# Make report
archi_run
exit 0