From 30cb868f69625cc1dc564b6a469a296ba8e2e01a Mon Sep 17 00:00:00 2001 From: alxndrsn Date: Thu, 13 Feb 2025 08:41:59 +0000 Subject: [PATCH 1/7] ci/test-images: print docker context This commit adds visibility into the docker context used for building container images. While the docker context is built via a blacklist-based `.dockerignore`, this should make it easier to spot: * unwanted files creeping into docker images * context bloat --- .github/workflows/ghcr.yml | 3 + .github/workflows/test.yml | 1 + test/check-docker-context.sh | 112 +++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100755 test/check-docker-context.sh diff --git a/.github/workflows/ghcr.yml b/.github/workflows/ghcr.yml index c7659113b..510b72d54 100644 --- a/.github/workflows/ghcr.yml +++ b/.github/workflows/ghcr.yml @@ -35,6 +35,9 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} + - name: Show Docker Context + run: ./test/check-docker-context.sh --report + - name: Extract Docker metadata id: meta uses: docker/metadata-action@v5 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cf092f660..d73de4cdc 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -45,6 +45,7 @@ jobs: fetch-depth: 0 fetch-tags: true submodules: recursive + - run: ./test/check-docker-context.sh --report - run: ./test/test-images.sh - if: always() run: docker compose logs diff --git a/test/check-docker-context.sh b/test/check-docker-context.sh new file mode 100755 index 000000000..8bb1130e5 --- /dev/null +++ b/test/check-docker-context.sh @@ -0,0 +1,112 @@ +#!/bin/bash -eu +set -o pipefail +log() { echo "[$(basename "$0")] $*"; } + +# See: https://stackoverflow.com/a/71751097 + +while [[ $# -gt 0 ]]; do + case "$1" in + --report) skip_size=true; skip_count=true ;; + + --min-size) shift;min_size="$1" ;; + --max-size) shift;max_size="$1" ;; + --skip-size) skip_size=true ;; + + --min-count) shift;min_count="$1" ;; + --max-count) shift;max_count="$1" ;; + --skip-count) skip_count=true ;; + + *) log "!!! Unrecognised arg: $1"; exit 1 ;; + esac + shift +done + +tmp="$(mktemp)" + +log "Creating custom docker build driver..." +# Use custom builder to prevent log truncation: +# > output clipped, log limit 200KiB/s reached +docker buildx rm docker_context_checker || true +docker buildx create --name docker_context_checker \ + --driver-opt env.BUILDKIT_STEP_LOG_MAX_SIZE=-1 \ + --driver-opt env.BUILDKIT_STEP_LOG_MAX_SPEED=-1 +docker buildx use docker_context_checker + +log "Building docker image..." +( +docker \ + buildx build --load \ + --no-cache --progress plain --file - . 2>&1 </dev/null +} +throw_err() { + log "!!!" + log "!!! $* !!!" + log "!!!" + cleanup + exit 1 +} + +for_humans() { + local size="$1" + if [[ "$size" -gt 999999 ]]; then + echo "$((size / 1000000)) GB" + else + echo "$((size / 1000)) MB" + fi +} + +log "File count: $file_count" +if [[ "${skip_count-}" != "true" ]]; then + if [[ "$file_count" -lt "$min_count" ]] || [[ "$file_count" -gt "$max_count" ]]; then + throw_err "This is a surprising number of files - expected between $min_count and $max_count" + fi +fi + +log "Total size: $(for_humans "$total_size")" +if [[ "${skip_size-}" != "true" ]]; then + # N.B. busybox `du` outputs in kB + # See: https://www.busybox.net/downloads/BusyBox.html#du + expected="- expected between $(for_humans "$min_size") and $(for_humans "$max_size")" + if [[ "$total_size" -lt "$min_size" ]]; then + throw_err "This is a surprisingly small total size $expected" + elif [[ "$total_size" -gt "$max_size" ]]; then + throw_err "This is a surprisingly large total size $expected" + fi +fi + +cleanup +log "Everything looks OK." From 497541becc1c448375c587c81baa33596248660d Mon Sep 17 00:00:00 2001 From: alxndrsn Date: Thu, 27 Mar 2025 06:35:49 +0000 Subject: [PATCH 2/7] Add usage message --- test/check-docker-context.sh | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/check-docker-context.sh b/test/check-docker-context.sh index 8bb1130e5..4b7fc7cd2 100755 --- a/test/check-docker-context.sh +++ b/test/check-docker-context.sh @@ -4,6 +4,15 @@ log() { echo "[$(basename "$0")] $*"; } # See: https://stackoverflow.com/a/71751097 +if [[ $# = 0 ]]; then + cat < Date: Thu, 27 Mar 2025 06:36:26 +0000 Subject: [PATCH 3/7] update for_humans to human_size --- test/check-docker-context.sh | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/test/check-docker-context.sh b/test/check-docker-context.sh index 4b7fc7cd2..a8fcd91c4 100755 --- a/test/check-docker-context.sh +++ b/test/check-docker-context.sh @@ -89,15 +89,6 @@ throw_err() { exit 1 } -for_humans() { - local size="$1" - if [[ "$size" -gt 999999 ]]; then - echo "$((size / 1000000)) GB" - else - echo "$((size / 1000)) MB" - fi -} - log "File count: $file_count" if [[ "${skip_count-}" != "true" ]]; then if [[ "$file_count" -lt "$min_count" ]] || [[ "$file_count" -gt "$max_count" ]]; then @@ -105,15 +96,23 @@ if [[ "${skip_count-}" != "true" ]]; then fi fi -log "Total size: $(for_humans "$total_size")" +human_size() { + if [[ "$1" -gt 999999 ]]; then + echo "$(bc <<< "scale=3; $1 / 1000000") GB" + else + echo "$(bc <<< "scale=3; $1 / 1000") MB" + fi +} + +log "Total size: $(human_size "$total_size")" if [[ "${skip_size-}" != "true" ]]; then # N.B. busybox `du` outputs in kB # See: https://www.busybox.net/downloads/BusyBox.html#du - expected="- expected between $(for_humans "$min_size") and $(for_humans "$max_size")" - if [[ "$total_size" -lt "$min_size" ]]; then - throw_err "This is a surprisingly small total size $expected" - elif [[ "$total_size" -gt "$max_size" ]]; then - throw_err "This is a surprisingly large total size $expected" + expected="- expected between $(human_size "$min_size") and $(human_size "$max_size")" + if [[ "$total_size" -lt $minSize ]]; then + throw_err "This is a surprisingly small total size (expected min: $(human_size "$minSize"))" + elif [[ "$total_size" -gt $maxSize ]]; then + throw_err "This is a surprisingly large total size (expected max: $(human_size "$maxSize"))" fi fi From 3e94b08210fbd1d96a2eb70e5352e2ffdb9016d0 Mon Sep 17 00:00:00 2001 From: alxndrsn Date: Thu, 27 Mar 2025 06:39:00 +0000 Subject: [PATCH 4/7] add standard shebang --- test/check-docker-context.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/check-docker-context.sh b/test/check-docker-context.sh index a8fcd91c4..51ca61221 100755 --- a/test/check-docker-context.sh +++ b/test/check-docker-context.sh @@ -1,5 +1,7 @@ #!/bin/bash -eu set -o pipefail +shopt -s inherit_errexit + log() { echo "[$(basename "$0")] $*"; } # See: https://stackoverflow.com/a/71751097 From 97311c0b5c60b22fb421bb2efbf381c2f607ba30 Mon Sep 17 00:00:00 2001 From: alxndrsn Date: Thu, 27 Mar 2025 06:40:41 +0000 Subject: [PATCH 5/7] fix size error checking --- test/check-docker-context.sh | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/check-docker-context.sh b/test/check-docker-context.sh index 51ca61221..4af896341 100755 --- a/test/check-docker-context.sh +++ b/test/check-docker-context.sh @@ -110,11 +110,10 @@ log "Total size: $(human_size "$total_size")" if [[ "${skip_size-}" != "true" ]]; then # N.B. busybox `du` outputs in kB # See: https://www.busybox.net/downloads/BusyBox.html#du - expected="- expected between $(human_size "$min_size") and $(human_size "$max_size")" - if [[ "$total_size" -lt $minSize ]]; then - throw_err "This is a surprisingly small total size (expected min: $(human_size "$minSize"))" - elif [[ "$total_size" -gt $maxSize ]]; then - throw_err "This is a surprisingly large total size (expected max: $(human_size "$maxSize"))" + if [[ "$total_size" -lt $min_size ]]; then + throw_err "This is a surprisingly small total size (expected min: $(human_size "$min_size"))" + elif [[ "$total_size" -gt $max_size ]]; then + throw_err "This is a surprisingly large total size (expected max: $(human_size "$max_size"))" fi fi From ce17412f50dceb521682dfaff8078dbaee280bbb Mon Sep 17 00:00:00 2001 From: alxndrsn Date: Thu, 27 Mar 2025 06:47:16 +0000 Subject: [PATCH 6/7] add --help --- test/check-docker-context.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/check-docker-context.sh b/test/check-docker-context.sh index 4af896341..efa40ea41 100755 --- a/test/check-docker-context.sh +++ b/test/check-docker-context.sh @@ -6,17 +6,23 @@ log() { echo "[$(basename "$0")] $*"; } # See: https://stackoverflow.com/a/71751097 -if [[ $# = 0 ]]; then +usage() { cat < Date: Fri, 28 Mar 2025 09:17:49 +0300 Subject: [PATCH 7/7] Update stackoverflow link Was pointing to the wrong answer. --- test/check-docker-context.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/check-docker-context.sh b/test/check-docker-context.sh index efa40ea41..7e7076ca2 100755 --- a/test/check-docker-context.sh +++ b/test/check-docker-context.sh @@ -4,7 +4,7 @@ shopt -s inherit_errexit log() { echo "[$(basename "$0")] $*"; } -# See: https://stackoverflow.com/a/71751097 +# See: https://stackoverflow.com/questions/38946683/ usage() { cat <