prometheus/scripts/compress_assets.sh
Julien 9126bbbed0 makefile: Add support for skipping UI build when prebuilt assets are provided
This commit introduces the ability to skip the UI build in the Makefile by
providing prebuilt UI assets, addressing the needs of users who may not have npm
installed or who do not want to go through the front-end build process.

To achieve this, we added the `PREBUILT_ASSETS_STATIC_DIR` environment variable.
If this variable is set, the Makefile will skip the UI build and related tasks,
such as bundling npm licenses. Instead, it will use the prebuilt assets from
the specified directory.

We already publish prebuilt UI assets as part of our release artifacts
(e.g., `prometheus-web-ui-3.0.0-beta.0.tar.gz`). Users can download this tarball,
extract it, and point the `PREBUILT_ASSETS_STATIC_DIR` to the extracted folder.
This reduces build complexity, especially for users who don't have a development
environment for front-end builds.

For example, you can use the command:
`make PREBUILT_ASSETS_STATIC_DIR=static build`
where `static` refers to the directory containing the prebuilt UI files.
This change simplifies the build process while still allowing users to use a
prebuilt UI if desired.

This solution is particularly useful for users who don't need to modify the UI
and prefer to use the prebuilt version that we provide with each release.

Signed-off-by: Julien <roidelapluie@o11y.eu>
2024-09-12 12:20:24 +02:00

36 lines
1 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# compress static assets
set -euo pipefail
export STATIC_DIR=static
PREBUILT_ASSETS_STATIC_DIR=${PREBUILT_ASSETS_STATIC_DIR:-}
if [ -n "$PREBUILT_ASSETS_STATIC_DIR" ]; then
STATIC_DIR=$(realpath $PREBUILT_ASSETS_STATIC_DIR)
fi
cd web/ui
cp embed.go.tmpl embed.go
GZIP_OPTS="-fk"
# gzip option '-k' may not always exist in the latest gzip available on different distros.
if ! gzip -k -h &>/dev/null; then GZIP_OPTS="-f"; fi
mkdir -p static
find static -type f -name '*.gz' -delete
# Compress files from the prebuilt static directory and replicate the structure in the current static directory
find "${STATIC_DIR}" -type f ! -name '*.gz' -exec bash -c '
for file; do
dest="${file#${STATIC_DIR}}"
mkdir -p "static/$(dirname "$dest")"
gzip '"$GZIP_OPTS"' "$file" -c > "static/${dest}.gz"
done
' bash {} +
# Append the paths of gzipped files to embed.go
find static -type f -name '*.gz' -print0 | sort -z | xargs -0 echo //go:embed >> embed.go
echo var EmbedFS embed.FS >> embed.go