forked from haskell-CI/haskell-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
haskell-ci.sh.zinza
456 lines (364 loc) · 10.9 KB
/
haskell-ci.sh.zinza
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#!/bin/bash
# shellcheck disable=SC2086,SC2016,SC2046
# REGENDATA {{ regendata }}
set -o pipefail
# Mode
##############################################################################
if [ "$1" = "indocker" ]; then
INDOCKER=true
shift
else
INDOCKER=false
fi
# Run configuration
##############################################################################
CFG_CABAL_STORE_CACHE=""
CFG_CABAL_REPO_CACHE=""
CFG_JOBS="{{ unwords jobs }}"
CFG_CABAL_UPDATE=false
SCRIPT_NAME=$(basename "$0")
START_TIME="$(date +'%s')"
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
# Job configuration
##############################################################################
GHC_VERSION="non-existing"
CABAL_VERSION=3.2
HEADHACKAGE=false
# Locale
##############################################################################
export LC_ALL=C.UTF-8
# Utilities
##############################################################################
SGR_RED='\033[1;31m'
SGR_GREEN='\033[1;32m'
SGR_BLUE='\033[1;34m'
SGR_CYAN='\033[1;96m'
SGR_RESET='\033[0m' # No Color
put_info() {
printf "$SGR_CYAN%s$SGR_RESET\n" "### $*"
}
put_error() {
printf "$SGR_RED%s$SGR_RESET\n" "!!! $*"
}
run_cmd() {
local PRETTYCMD="$*"
local PROMPT
if $INDOCKER; then
PROMPT="$(pwd) >>>"
else
PROMPT=">>>"
fi
printf "$SGR_BLUE%s %s$SGR_RESET\n" "$PROMPT" "$PRETTYCMD"
local start_time end_time cmd_duration total_duration
start_time=$(date +'%s')
"$@"
local RET=$?
end_time=$(date +'%s')
cmd_duration=$((end_time - start_time))
total_duration=$((end_time - START_TIME))
cmd_min=$((cmd_duration / 60))
cmd_sec=$((cmd_duration % 60))
total_min=$((total_duration / 60))
total_sec=$((total_duration % 60))
if [ $RET -eq 0 ]; then
printf "$SGR_GREEN%s$SGR_RESET (%dm%02ds; %dm%02ds)\n" "<<< $PRETTYCMD" "$cmd_min" "$cmd_sec" "$total_min" "$total_sec"
else
printf "$SGR_RED%s$SGR_RESET\n" "!!! $PRETTYCMD"
exit 1
fi
}
run_cmd_if() {
local COND=$1
shift
if [ $COND -eq 1 ]; then
run_cmd "$@"
else
local PRETTYCMD="$*"
local PROMPT
PROMPT="$(pwd) (skipping) >>>"
printf "$SGR_BLUE%s %s$SGR_RESET\n" "$PROMPT" "$PRETTYCMD"
fi
}
run_cmd_unchecked() {
local PRETTYCMD="$*"
local PROMPT
if $INDOCKER; then
PROMPT="$(pwd) >>>"
else
PROMPT=">>>"
fi
printf "$SGR_BLUE%s %s$SGR_RESET\n" "$PROMPT" "$PRETTYCMD"
local start_time end_time cmd_duration total_duration cmd_min cmd_sec total_min total_sec
start_time=$(date +'%s')
"$@"
end_time=$(date +'%s')
cmd_duration=$((end_time - start_time))
total_duration=$((end_time - START_TIME))
cmd_min=$((cmd_duration / 60))
cmd_sec=$((cmd_duration % 60))
total_min=$((total_duration / 60))
total_sec=$((total_duration % 60))
printf "$SGR_GREEN%s$SGR_RESET (%dm%02ds; %dm%02ds)\n" "<<< $PRETTYCMD" "$cmd_min" "$cmd_sec" "$total_min" "$total_sec"
}
change_dir() {
local DIR=$1
if [ -d "$DIR" ]; then
printf "$SGR_BLUE%s$SGR_RESET\n" "change directory to $DIR"
cd "$DIR" || exit 1
else
printf "$SGR_RED%s$SGR_RESET\n" "!!! cd $DIR"
exit 1
fi
}
change_dir_if() {
local COND=$1
local DIR=$2
if [ $COND -ne 0 ]; then
change_dir "$DIR"
fi
}
echo_to() {
local DEST=$1
local CONTENTS=$2
echo "$CONTENTS" >> "$DEST"
}
echo_if_to() {
local COND=$1
local DEST=$2
local CONTENTS=$3
if [ $COND -ne 0 ]; then
echo_to "$DEST" "$CONTENTS"
fi
}
install_cabalplan() {
put_info "installing cabal-plan"
if [ ! -e $CABAL_REPOCACHE/downloads/cabal-plan ]; then
curl -L https://github.com/haskell-hvr/cabal-plan/releases/download/v0.6.2.0/cabal-plan-0.6.2.0-x86_64-linux.xz > /tmp/cabal-plan.xz || exit 1
(cd /tmp && echo "de73600b1836d3f55e32d80385acc055fd97f60eaa0ab68a755302685f5d81bc cabal-plan.xz" | sha256sum -c -)|| exit 1
mkdir -p $CABAL_REPOCACHE/downloads
xz -d < /tmp/cabal-plan.xz > $CABAL_REPOCACHE/downloads/cabal-plan || exit 1
chmod a+x $CABAL_REPOCACHE/downloads/cabal-plan || exit 1
fi
mkdir -p $CABAL_DIR/bin || exit 1
ln -s $CABAL_REPOCACHE/downloads/cabal-plan $CABAL_DIR/bin/cabal-plan || exit 1
}
# Help
##############################################################################
show_usage() {
cat <<EOF
./haskell-ci.sh - build & test
Usage: ./haskell-ci.sh [options]
A script to run automated checks locally (using Docker)
Available options:
--jobs JOBS Jobs to run (default: $CFG_JOBS)
--cabal-store-cache PATH Directory to use for cabal-store-cache
--cabal-repo-cache PATH Directory to use for cabal-repo-cache
--skip-cabal-update Skip cabal update (useful with --cabal-repo-cache)
--no-skip-cabal-update
--help Print this message
EOF
}
# getopt
#######################################################################
process_cli_options() {
while [ $# -gt 0 ]; do
arg=$1
case $arg in
--help)
show_usage
exit
;;
--jobs)
CFG_JOBS=$2
shift
shift
;;
--cabal-store-cache)
CFG_CABAL_STORE_CACHE=$2
shift
shift
;;
--cabal-repo-cache)
CFG_CABAL_REPO_CACHE=$2
shift
shift
;;
--skip-cabal-update)
CFG_CABAL_UPDATE=false
shift
;;
--no-skip-cabal-update)
CFG_CABAL_UPDATE=true
shift
;;
*)
echo "Unknown option $arg"
exit 1
esac
done
}
process_indocker_options () {
while [ $# -gt 0 ]; do
arg=$1
case $arg in
--ghc-version)
GHC_VERSION=$2
shift
shift
;;
--cabal-version)
CABAL_VERSION=$2
shift
shift
;;
--start-time)
START_TIME=$2
shift
shift
;;
--cabal-update)
CABAL_UPDATE=$2
shift
shift
;;
*)
echo "Unknown option $arg"
exit 1
esac
done
}
if $INDOCKER; then
process_indocker_options "$@"
else
if [ -f "$XDG_CONFIG_HOME/haskell-ci/bash.config" ]; then
process_cli_options $(cat "$XDG_CONFIG_HOME/haskell-ci/bash.config")
fi
process_cli_options "$@"
put_info "jobs: $CFG_JOBS"
put_info "cabal-store-cache: $CFG_CABAL_STORE_CACHE"
put_info "cabal-repo-cache: $CFG_CABAL_REPO_CACHE"
put_info "cabal-update: $CFG_CABAL_UPDATE"
fi
# Constants
##############################################################################
SRCDIR=/hsci/src
BUILDDIR=/hsci/build
CABAL_DIR="$BUILDDIR/cabal"
CABAL_REPOCACHE=/hsci/cabal-repocache
CABAL_STOREDIR=/hsci/store
# Docker invoke
##############################################################################
# if cache directory is specified, use it.
# Otherwise use another tmpfs host
if [ -z "$CFG_CABAL_STORE_CACHE" ]; then
CABALSTOREARG="--tmpfs $CABAL_STOREDIR:exec"
else
CABALSTOREARG="--volume $CFG_CABAL_STORE_CACHE:$CABAL_STOREDIR"
fi
if [ -z "$CFG_CABAL_REPO_CACHE" ]; then
CABALREPOARG="--tmpfs $CABAL_REPOCACHE:exec"
else
CABALREPOARG="--volume $CFG_CABAL_REPO_CACHE:$CABAL_REPOCACHE"
fi
echo_docker_cmd() {
local GHCVER=$1
# TODO: mount /hsci/src:ro (readonly)
echo docker run \
--tty \
--interactive \
--rm \
--label haskell-ci \
--volume "$(pwd):/hsci/src" \
$CABALSTOREARG \
$CABALREPOARG \
--tmpfs /tmp:exec \
--tmpfs /hsci/build:exec \
--workdir /hsci/build \
"phadej/ghc:$GHCVER-bionic" \
"/bin/bash" "/hsci/src/$SCRIPT_NAME" indocker \
--ghc-version "$GHCVER" \
--cabal-update "$CFG_CABAL_UPDATE" \
--start-time "$START_TIME"
}
# if we are not in docker, loop through jobs
if ! $INDOCKER; then
for JOB in $CFG_JOBS; do
put_info "Running in docker: $JOB"
run_cmd $(echo_docker_cmd "$JOB")
done
run_cmd echo "ALL OK"
exit 0
fi
# Otherwise we are in docker, and the rest of script executes
put_info "In docker"
# Environment
##############################################################################
GHCDIR=/opt/ghc/$GHC_VERSION
HC=$GHCDIR/bin/ghc
HCPKG=$GHCDIR/bin/ghc-pkg
HADDOCK=$GHCDIR/bin/haddock
CABAL=/opt/cabal/$CABAL_VERSION/bin/cabal
CABAL="$CABAL -vnormal+nowrap"
export CABAL_DIR
export CABAL_CONFIG="$BUILDDIR/cabal/config"
PATH="$CABAL_DIR/bin:$PATH"
# HCNUMVER
HCNUMVER=$(${HC} --numeric-version|perl -ne '/^(\d+)\.(\d+)\.(\d+)(\.(\d+))?$/; print(10000 * $1 + 100 * $2 + ($3 == 0 ? $5 != 1 : $3))')
GHCJSARITH=0
put_info "HCNUMVER: $HCNUMVER"
# Args for shorter/nicer commands
if [ {{ testsCond }} -ne 0 ] ; then ARG_TESTS=--enable-tests; else ARG_TESTS=--disable-tests; fi
if [ {{ benchCond }} -ne 0 ] ; then ARG_BENCH=--enable-benchmarks; else ARG_BENCH=--disable-benchmarks; fi
ARG_COMPILER="--ghc --with-compiler=$HC"
put_info "tests/benchmarks: $ARG_TESTS $ARG_BENCH"
# Apt dependencies
##############################################################################
{% if notNull apt %}
run_cmd apt-get update
run_cmd apt-get install -y --no-install-recommends {{ unwords apt }}
{% endif %}
# Cabal config
##############################################################################
mkdir -p $BUILDDIR/cabal
cat > $BUILDDIR/cabal/config <<EOF
remote-build-reporting: anonymous
write-ghc-environment-files: always
remote-repo-cache: $CABAL_REPOCACHE
logs-dir: $CABAL_DIR/logs
world-file: $CABAL_DIR/world
extra-prog-path: $CABAL_DIR/bin
symlink-bindir: $CABAL_DIR/bin
installdir: $CABAL_DIR/bin
build-summary: $CABAL_DIR/logs/build.log
store-dir: $CABAL_STOREDIR
install-dirs user
prefix: $CABAL_DIR
repository hackage.haskell.org
url: http://hackage.haskell.org/
EOF
if $HEADHACKAGE; then
put_error "head.hackage is not implemented"
exit 1
fi
run_cmd cat "$BUILDDIR/cabal/config"
# Version
##############################################################################
put_info "Versions"
run_cmd $HC --version
run_cmd_unchecked $HC --print-project-git-commit-id
run_cmd $CABAL --version
# Build script
##############################################################################
# update cabal index
if $CABAL_UPDATE; then
put_info "Updating Hackage index"
run_cmd $CABAL v2-update -v
fi
# install cabal-plan
install_cabalplan
run_cmd cabal-plan --version
{% for block in blocks %}
{{ block }}
{% endfor %}
# Done
run_cmd echo OK