-
Notifications
You must be signed in to change notification settings - Fork 4
ci: add compressed image size cap to GitHub Actions and GitLab CI #289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wdconinc
wants to merge
18
commits into
master
Choose a base branch
from
check-image-size
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
51c8aaf
ci: add compressed image size cap to GitHub Actions and GitLab CI
wdconinc 690d2af
fix(ci): handle OCI Image Index in check_image_size
wdconinc 9707436
ci: skip size check for images without a configured limit
wdconinc ccd794e
fix(ci): use eval for POSIX sh indirect variable expansion in GitLab CI
wdconinc b9fbba6
fix: rm refs to query_image_sizes.sh
wdconinc f6ef231
fix: print to two decimals
wdconinc d1b79e7
fix: apk add bc, git, and jq in .build
wdconinc 34ce2f7
fix: add .ci/check_image_size to shellcheck
wdconinc 0897177
fix: clarify docs on multi-arch
wdconinc 0d014b2
fix: suggest using script outside CI too
wdconinc db034dc
fix: skip provenance layers with unknown platform
wdconinc 139555f
fix: update size calibration
wdconinc d018dd2
fix: more provenance layer handling
wdconinc a987ba5
fix: use consistent base size; make rounding optional
wdconinc 007354c
fix: use linux line endings
wdconinc 041e495
fix: use linux line endings
wdconinc 0234a8e
fix: strip :tag before appending @digest in check_image_size
wdconinc 25e667d
fix: replace eval with printenv for indirect variable lookup
wdconinc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| #!/bin/sh | ||
|
wdconinc marked this conversation as resolved.
|
||
| # check_image_size IMAGE_REFERENCE LIMIT_GIB | ||
| # | ||
| # Inspects a single-arch OCI/Docker image manifest (or the first image | ||
| # manifest in a multi-arch OCI/Docker image) that has already been pushed | ||
|
wdconinc marked this conversation as resolved.
|
||
| # to a registry and fails with a clear message if the total compressed layer | ||
| # size exceeds LIMIT_GIB gibibytes. | ||
| # | ||
| # Requires: docker (with buildx), jq, bc | ||
| # Usage: | ||
| # .ci/check_image_size ghcr.io/eic/eic_xl@sha256:abc... 10 | ||
| # .ci/check_image_size ghcr.io/eic/eic_xl:master 10 | ||
|
wdconinc marked this conversation as resolved.
|
||
| # | ||
| # The caller is responsible for being logged in to the registry before calling | ||
| # this script. | ||
|
|
||
| set -e | ||
|
|
||
| IMAGE_REF="${1:?Usage: $0 IMAGE_REFERENCE LIMIT_GIB}" | ||
| LIMIT_GIB="${2:?Usage: $0 IMAGE_REFERENCE LIMIT_GIB}" | ||
|
|
||
| echo "Checking compressed image size for ${IMAGE_REF} (limit: ${LIMIT_GIB} GiB) ..." | ||
|
|
||
| MANIFEST=$(docker buildx imagetools inspect --raw "${IMAGE_REF}") | ||
|
|
||
| # docker/build-push-action (v4+) wraps even single-platform pushes in an OCI | ||
| # Image Index when provenance attestations are enabled (the default). Detect | ||
| # that case and resolve to the real per-platform Image Manifest before summing | ||
| # layer sizes. | ||
| MEDIA_TYPE=$(printf '%s' "${MANIFEST}" | jq -r '.mediaType // ""') | ||
| case "${MEDIA_TYPE}" in | ||
| *index* | *manifest.list*) | ||
| DIGEST=$(printf '%s' "${MANIFEST}" | jq -r ' | ||
| .manifests[] | ||
| | select( | ||
| .artifactType == null | ||
|
wdconinc marked this conversation as resolved.
|
||
| and ((.annotations["vnd.docker.reference.type"] // "") != "attestation-manifest") | ||
| and ((.annotations["vnd.docker.reference.digest"] // "") == "") | ||
| and .platform != null | ||
| and (.platform.os // "") != "" | ||
| and (.platform.architecture // "") != "" | ||
| and (.platform.os // "") != "unknown" | ||
| and (.platform.architecture // "") != "unknown" | ||
| ) | ||
| | .digest' | head -1) | ||
|
wdconinc marked this conversation as resolved.
wdconinc marked this conversation as resolved.
|
||
| if [ -z "${DIGEST}" ]; then | ||
| echo "ERROR: OCI Image Index contains no platform manifest." | ||
| exit 1 | ||
| fi | ||
| IMAGE_BASE="${IMAGE_REF%@*}" | ||
|
wdconinc marked this conversation as resolved.
Outdated
|
||
| MANIFEST=$(docker buildx imagetools inspect --raw "${IMAGE_BASE}@${DIGEST}") | ||
| ;; | ||
| esac | ||
|
|
||
| SIZE_BYTES=$(printf '%s' "${MANIFEST}" | jq '[.layers[].size] | add // 0') | ||
|
|
||
|
wdconinc marked this conversation as resolved.
|
||
| if [ -z "${SIZE_BYTES}" ] || [ "${SIZE_BYTES}" = "null" ] || [ "${SIZE_BYTES}" -eq 0 ]; then | ||
| echo "ERROR: Could not determine image size from manifest (got: ${SIZE_BYTES})." | ||
| echo " Manifest snippet: $(printf '%s' "${MANIFEST}" | head -c 500)" | ||
| exit 1 | ||
| fi | ||
|
|
||
| SIZE_CENTI_GIB=$(echo "(${SIZE_BYTES} * 100 + 1073741824 - 1) / 1073741824" | bc) | ||
| SIZE_GIB="$(echo "${SIZE_CENTI_GIB} / 100" | bc).$(printf '%02d' "$(echo "${SIZE_CENTI_GIB} % 100" | bc)")" | ||
| LIMIT_BYTES=$(echo "${LIMIT_GIB} * 1073741824 / 1" | bc) | ||
|
|
||
| echo " Compressed size : ${SIZE_GIB} GiB (${SIZE_BYTES} bytes)" | ||
| echo " Limit : ${LIMIT_GIB} GiB (${LIMIT_BYTES} bytes)" | ||
|
|
||
| if [ "${SIZE_BYTES}" -gt "${LIMIT_BYTES}" ]; then | ||
| echo "" | ||
| echo "ERROR: Image ${IMAGE_REF} exceeds the size cap!" | ||
| echo " ${SIZE_GIB} GiB > ${LIMIT_GIB} GiB" | ||
| echo "" | ||
| echo "To update the high-water mark after an intentional size increase:" | ||
| echo " 1. Re-run this script to inspect the published image size:" | ||
| echo " .ci/check_image_size \"${IMAGE_REF}\" \"${LIMIT_GIB}\"" | ||
| echo " 2. Add ~15% buffer, and optionally round up to the next whole GiB" | ||
| echo " 3. Update SIZE_LIMIT_*_GIB in .github/workflows/build-push.yml and .gitlab-ci.yml" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo " OK: size is within the ${LIMIT_GIB} GiB limit." | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.