-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·159 lines (121 loc) · 6.52 KB
/
build.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
#!/bin/bash
# Glowing Bear
# A container running Glowing Bear, a web frontend for the WeeChat IRC client.
#
# Copyright (c) 2023 SGS Serious Gaming & Simulations GmbH
#
# This work is licensed under the terms of the MIT license.
# For a copy, see LICENSE file or <https://opensource.org/licenses/MIT>.
#
# SPDX-License-Identifier: MIT
# License-Filename: LICENSE
set -eu -o pipefail
export LC_ALL=C.UTF-8
[ -v CI_TOOLS ] && [ "$CI_TOOLS" == "SGSGermany" ] \
|| { echo "Invalid build environment: Environment variable 'CI_TOOLS' not set or invalid" >&2; exit 1; }
[ -v CI_TOOLS_PATH ] && [ -d "$CI_TOOLS_PATH" ] \
|| { echo "Invalid build environment: Environment variable 'CI_TOOLS_PATH' not set or invalid" >&2; exit 1; }
[ -x "$(which jq 2>/dev/null)" ] \
|| { echo "Missing build script dependency: jq" >&2; exit 1; }
source "$CI_TOOLS_PATH/helper/common.sh.inc"
source "$CI_TOOLS_PATH/helper/container.sh.inc"
source "$CI_TOOLS_PATH/helper/container-alpine.sh.inc"
source "$CI_TOOLS_PATH/helper/git.sh.inc"
BUILD_DIR="$(CDPATH= cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
source "$BUILD_DIR/container.env"
readarray -t -d' ' TAGS < <(printf '%s' "$TAGS")
echo + "BUILD_CONTAINER=\"\$(buildah from $(quote "$BASE_IMAGE"))\"" >&2
BUILD_CONTAINER="$(buildah from "$BASE_IMAGE")"
echo + "BUILD_MOUNT=\"\$(buildah mount $(quote "$BUILD_CONTAINER"))\"" >&2
BUILD_MOUNT="$(buildah mount "$BUILD_CONTAINER")"
if [ -n "${VERSION:-}" ] && [ -n "${HASH:-}" ]; then
echo + "[[ $(quote "$VERSION") =~ ^([0-9]+\.[0-9]+\.[0-9]+-([a-f0-9]+))([+~-]|$) ]]" >&2
if ! [[ "$VERSION" =~ ^([0-9]+\.[0-9]+\.[0-9]+-([a-f0-9]+))([+~-]|$) ]]; then
echo "Invalid build environment: Environment variable 'VERSION' is invalid: $VERSION" >&2
exit 1
fi
echo + "HASH_SHORT=\"\${BASH_REMATCH[2]}\"" >&2
HASH_SHORT="${BASH_REMATCH[2]}"
echo + "[[ $(quote "$HASH") =~ ^[a-f0-9]{40}[a-f0-9]{24}?$ ]]" >&2
if ! [[ "$HASH" =~ ^[a-f0-9]{40}[a-f0-9]{24}?$ ]]; then
echo "Invalid build environment: Environment variable 'HASH' is invalid: $HASH" >&2
exit 1
fi
echo + "[[ $(quote "$HASH") == $(quote "$HASH_SHORT")* ]]" >&2
if [[ "$HASH" != "$HASH_SHORT"* ]]; then
echo "Invalid build environment: Environment variables 'VERSION' (${VERSION@Q})" \
"and 'HASH' (${HASH@Q}) contradict each other" >&2
exit 1
fi
git_clone "$GIT_REPO" "$HASH" \
"$BUILD_MOUNT/usr/src/glowingbear" "<builder> …/usr/src/glowingbear"
else
git_clone "$GIT_REPO" "$GIT_REF" \
"$BUILD_MOUNT/usr/src/glowingbear" "<builder> …/usr/src/glowingbear"
echo + "HASH=\"\$(git -C '<builder> …/usr/src/glowingbear' rev-parse HEAD)\"" >&2
HASH="$(git -C "$BUILD_MOUNT/usr/src/glowingbear" rev-parse HEAD)"
echo + "HASH_SHORT=\"\$(git -C '<builder> …/usr/src/glowingbear' rev-parse --short HEAD)\"" >&2
HASH_SHORT="$(git -C "$BUILD_MOUNT/usr/src/glowingbear" rev-parse --short HEAD)"
echo + "VERSION=\"\$(jq -re '.version' '<builder> …/usr/src/glowingbear/package.json')-\$HASH_SHORT\"" >&2
VERSION="$(jq -re '.version' "$BUILD_MOUNT/usr/src/glowingbear/package.json")-$HASH_SHORT"
fi
pkg_install "$BUILD_CONTAINER" \
nodejs \
npm@community
cmd buildah run --workingdir "/usr/src/glowingbear" "$BUILD_CONTAINER" -- \
npm install
cmd buildah run --workingdir "/usr/src/glowingbear" "$BUILD_CONTAINER" -- \
npm run build
echo + "CONTAINER=\"\$(buildah from $(quote "$BASE_IMAGE"))\"" >&2
CONTAINER="$(buildah from "$BASE_IMAGE")"
echo + "MOUNT=\"\$(buildah mount $(quote "$CONTAINER"))\"" >&2
MOUNT="$(buildah mount "$CONTAINER")"
user_add "$CONTAINER" glowingbear 65536 "/var/lib/glowingbear"
pkg_install "$CONTAINER" --virtual .run-deps \
rsync
echo + "rsync -v -rl --exclude .gitignore ./src/ …/" >&2
rsync -v -rl --exclude '.gitignore' "$BUILD_DIR/src/" "$MOUNT/"
echo + "rsync -v -rl '<builder> …/usr/src/glowingbear/build/' …/usr/src/glowingbear/glowingbear/" >&2
rsync -v -rl "$BUILD_MOUNT/usr/src/glowingbear/build/" "$MOUNT/usr/src/glowingbear/glowingbear/"
echo + "rm -f …/usr/src/glowingbear/glowingbear/package.json" >&2
rm -f "$MOUNT/usr/src/glowingbear/glowingbear/package.json"
echo + "jq -e --arg VERSION $(quote "$VERSION") '.version = \$VERSION' …/usr/src/glowingbear/glowingbear/manifest.json | sponge …" >&2
jq -e --arg VERSION "$VERSION" '.version = $VERSION' "$MOUNT/usr/src/glowingbear/glowingbear/manifest.json" \
| sponge "$MOUNT/usr/src/glowingbear/glowingbear/manifest.json"
echo + "jq -e --arg VERSION $(quote "$VERSION") '.version = \$VERSION' …/usr/src/glowingbear/glowingbear/manifest.webapp | sponge …" >&2
jq -e --arg VERSION "$VERSION" '.version = $VERSION' "$MOUNT/usr/src/glowingbear/glowingbear/manifest.webapp" \
| sponge "$MOUNT/usr/src/glowingbear/glowingbear/manifest.webapp"
echo + "sed -i -e \"s/>Glowing Bear version [^<]*</>Glowing Bear version \$VERSION</\" …/usr/src/glowingbear/glowingbear/index.html" >&2
sed -i -e "s/>Glowing Bear version [^<]*</>Glowing Bear version $(sed -e 's/[\/&]/\\&/g' <<< "$VERSION")</" \
"$MOUNT/usr/src/glowingbear/glowingbear/index.html"
cmd buildah run "$CONTAINER" -- \
/bin/sh -c "printf '%s=%s\n' \"\$@\" > /usr/src/glowingbear/version_info" -- \
VERSION "$VERSION" \
HASH "$HASH"
cmd buildah run "$CONTAINER" -- \
chown glowingbear:glowingbear "/var/www/html"
cleanup "$CONTAINER"
cmd buildah config \
--env GLOWING_BEAR_VERSION="$VERSION" \
--env GLOWING_BEAR_HASH="$HASH" \
"$CONTAINER"
cmd buildah config \
--volume "/var/www" \
"$CONTAINER"
cmd buildah config \
--workingdir "/var/www/html" \
--entrypoint '[ "/entrypoint.sh" ]' \
--cmd '[ "glowingbear" ]' \
"$CONTAINER"
cmd buildah config \
--annotation org.opencontainers.image.title="Glowing Bear" \
--annotation org.opencontainers.image.description="A container running Glowing Bear, a web frontend for the WeeChat IRC client." \
--annotation org.opencontainers.image.version="$VERSION" \
--annotation org.opencontainers.image.url="https://github.com/SGSGermany/glowingbear" \
--annotation org.opencontainers.image.authors="SGS Serious Gaming & Simulations GmbH" \
--annotation org.opencontainers.image.vendor="SGS Serious Gaming & Simulations GmbH" \
--annotation org.opencontainers.image.licenses="MIT" \
--annotation org.opencontainers.image.base.name="$BASE_IMAGE" \
--annotation org.opencontainers.image.base.digest="$(podman image inspect --format '{{.Digest}}' "$BASE_IMAGE")" \
"$CONTAINER"
con_commit "$CONTAINER" "$IMAGE" "${TAGS[@]}"