diff options
author | Sarah German <sgerman@gitlab.com> | 2023-03-29 23:31:35 +0300 |
---|---|---|
committer | Sarah German <sgerman@gitlab.com> | 2023-03-29 23:31:35 +0300 |
commit | 5d9eec9ba36f57ecab95cbc99cf7e40f85250c42 (patch) | |
tree | 4a6671bb94578b6ee75ad091e38b1abdcd28ccbb | |
parent | 6cd764b6b045b08cf21c6ebe1bd6956e0d5f8380 (diff) | |
parent | 826a22430d373bbfd834cdf544e0d462a8cc7125 (diff) |
Merge branch 'eread/refactor-minify-assets-script' into 'main'
Refactor minify-assets.sh script
See merge request https://gitlab.com/gitlab-org/gitlab-docs/-/merge_requests/3708
Merged-by: Sarah German <sgerman@gitlab.com>
Approved-by: Sarah German <sgerman@gitlab.com>
Reviewed-by: Achilleas Pipinellis <axil@gitlab.com>
Co-authored-by: Evan Read <eread@gitlab.com>
-rw-r--r-- | Brewfile | 1 | ||||
-rw-r--r-- | dockerfiles/single.Dockerfile | 27 | ||||
-rwxr-xr-x | scripts/minify-assets.sh | 47 |
3 files changed, 44 insertions, 31 deletions
@@ -1,6 +1,7 @@ # Install on all OSes brew "hadolint" brew "jq" +brew "minify" brew "openssl@1.1", link: :force brew "parallel" brew "yamllint" diff --git a/dockerfiles/single.Dockerfile b/dockerfiles/single.Dockerfile index 383cd3f3..5671da42 100644 --- a/dockerfiles/single.Dockerfile +++ b/dockerfiles/single.Dockerfile @@ -3,26 +3,7 @@ # and rename it to X.Y.Dockerfile, where X.Y the major.minor GitLab version. # -#- Start of minifier stage -# - -# -# Build minifier utility -# Adapted from https://github.com/docker/docker.github.io/blob/publish-tools/Dockerfile.builder -# -FROM golang:1.13-alpine AS minifier -RUN apk add --no-cache git \ - && export GO111MODULE=on \ - && go get -d github.com/tdewolff/minify/v2@latest \ - && go build -v -o /minify github.com/tdewolff/minify/cmd/minify - -#- End of minifier stage -# - -#- Start of builder stage -# - -FROM ruby:3.2.1-alpine3.17 AS builder - -# Copy minifier binary from the minifier stage -COPY --from=minifier /minify /usr/local/bin/minify +FROM ruby:3.2.1-alpine3.17 as builder # Set versions as build args to fetch corresponding branches ARG VER @@ -51,6 +32,7 @@ RUN apk add --no-cache -U \ libcurl \ libxslt \ libxslt-dev \ + minify \ nodejs \ openssl \ pngquant \ @@ -88,7 +70,8 @@ RUN mkdir /site \ # Do some HTML post-processing on the archive, compress images, and minify assets RUN /source/scripts/normalize-links.sh /site "${VER}" \ && /source/scripts/compress_images.sh /site "${VER}" \ - && /source/scripts/minify-assets.sh /site "${VER}" # ATTENTION: This should be the last script to run + # ATTENTION: This should be the last script to run + && /source/scripts/minify-assets.sh /site "${VER}" # Make an index.html and 404.html which will redirect / to /${VER}/ RUN echo "<html><head><title>Redirect for ${VER}</title><meta http-equiv=\"refresh\" content=\"0;url='/${VER}/'\" /></head><body><p>If you are not redirected automatically, click <a href=\"/${VER}/\">here</a>.</p></body></html>" > /site/index.html \ @@ -101,7 +84,7 @@ RUN echo "<html><head><title>Redirect for ${VER}</title><meta http-equiv=\"refre # Copy the ending HTML files from the previous 'builder' stage and copy them # to an NGINX Docker image. # -FROM nginx:alpine +FROM nginx:stable-alpine # Clean out any existing HTML files, and copy the HTML from the builder stage # to the default location for Nginx. diff --git a/scripts/minify-assets.sh b/scripts/minify-assets.sh index 3cca8789..aa27512e 100755 --- a/scripts/minify-assets.sh +++ b/scripts/minify-assets.sh @@ -1,17 +1,22 @@ #!/usr/bin/env bash +# shellcheck disable=SC2059 TARGET="$1" VER="$2" MINIFY_FLAGS=("--html-keep-document-tags" "--html-keep-whitespace" "--recursive") +COLOR_RED="\e[31m" +COLOR_GREEN="\e[32m" +COLOR_RESET="\e[39m" + if [ -z "$TARGET" ] || [ -z "$VER" ]; then echo "Usage: $0 <target> <ver>" - echo "Either <target> or <ver> is missing. Exiting." + printf "${COLOR_RED}ERROR: Either <target> or <ver> is missing.${COLOR_RESET}\n" exit 1 fi if ! [ -d "$TARGET" ]; then - echo "Target directory $TARGET does not exist. Exiting." + printf "${COLOR_RED}ERROR: Target directory $TARGET does not exist.${COLOR_RESET}\n" exit 1 fi @@ -25,17 +30,41 @@ else then MINIFY_BIN=/scripts/minify else - echo "minify binary not found in PATH. Exiting." + printf "${COLOR_RED}ERROR: minify not found in PATH. Run 'make setup'.${COLOR_RESET}\n" exit 1 fi fi # Minify assets -printf "Optimizing assets..." +printf "${COLOR_GREEN}INFO: Minifying HTML...${COLOR_RESET}\n" +if $MINIFY_BIN "${MINIFY_FLAGS[@]}" --type=html --match="\.html$" -o "${TARGET}/${VER}/" "${TARGET}/${VER}"; then + printf "${COLOR_GREEN}INFO: HTML minified!${COLOR_RESET}\n" +else + printf "${COLOR_RED}ERROR: Couldn't minify HTML${COLOR_RESET}\n" + exit 1 +fi + +printf "${COLOR_GREEN}INFO: Minifying CSS...${COLOR_RESET}\n" +if $MINIFY_BIN "${MINIFY_FLAGS[@]}" --type=css --match="\.css$" -o "${TARGET}/${VER}/" "${TARGET}/${VER}"; then + printf "${COLOR_GREEN}INFO: CSS minified!${COLOR_RESET}\n" +else + printf "${COLOR_RED}ERROR: Couldn't minify CSS${COLOR_RESET}\n" + exit 1 +fi -printf "HTML..."; $MINIFY_BIN "${MINIFY_FLAGS[@]}" --type=html --match="\.html$" -o "${TARGET}/${VER}/" "${TARGET}/${VER}" || true -printf "CSS..." ; $MINIFY_BIN "${MINIFY_FLAGS[@]}" --type=css --match="\.css$" -o "${TARGET}/${VER}/" "${TARGET}/${VER}" || true -printf "JSON..."; $MINIFY_BIN "${MINIFY_FLAGS[@]}" --type=json --match="\.json$" -o "${TARGET}/${VER}/" "${TARGET}/${VER}" || true -printf "SVG..." ; $MINIFY_BIN "${MINIFY_FLAGS[@]}" --type=svg --match="\.svg$" -o "${TARGET}/${VER}/" "${TARGET}/${VER}" || true -echo "Done" +printf "${COLOR_GREEN}INFO: Minifying JSON...${COLOR_RESET}\n" +if $MINIFY_BIN "${MINIFY_FLAGS[@]}" --type=json --match="\.json$" -o "${TARGET}/${VER}/" "${TARGET}/${VER}"; then + printf "${COLOR_GREEN}INFO: JSON minified!${COLOR_RESET}\n" +else + printf "${COLOR_RED}ERROR: Couldn't minify JSON${COLOR_RESET}\n" + exit 1 +fi + +printf "${COLOR_GREEN}INFO: Minifying SVGs...${COLOR_RESET}\n" +if $MINIFY_BIN "${MINIFY_FLAGS[@]}" --type=svg --match="\.svg$" -o "${TARGET}/${VER}/" "${TARGET}/${VER}"; then + printf "${COLOR_GREEN}INFO: SVGs minified!${COLOR_RESET}\n" +else + printf "${COLOR_RED}ERROR: Couldn't minify SVGs${COLOR_RESET}\n" + exit 1 +fi |