Files
deploy/remote/remove_replaced_images.sh
T
Christian Manivong 476afe3982
CI / check (pull_request) Successful in 19s
fix: remove only the images the deploy replaced, never every unused one
The deploy ended with docker image prune -af: every image no container
used at that moment. On a host that runs other things too, that takes
their rollback images; the KB's copy of this line did exactly that on
compute-01 (NetOrk/kb-server#157).

deploy.sh now records the image IDs netOrk's own containers use before
the swap and, after the verification and the migration, hands them to
remote/remove_replaced_images.sh. It removes each by its tags, or by its
ID once a moving tag left it without one, and keeps any image a
container still uses: a redeploy of the same tag replaces nothing.
2026-10-23 12:06:08 +02:00

30 lines
1.2 KiB
Bash

#!/bin/bash
# remove_replaced_images.sh <image id>...: the images a deploy replaced, and
# nothing else. deploy.sh records the IDs netOrk's own containers use before the
# swap and runs this on the host after it.
#
# It used to be `docker image prune -af`, which removes every image no container
# uses at that moment. On a host that runs other things too, that took their
# rollback images (NetOrk/kb-server#157). An ID goes here only if no container --
# netOrk's or anyone else's -- uses it: a redeploy of the same tag replaces
# nothing. It goes by every tag it carries, or by its ID once a moving tag such as
# latest-dev has left it without one.
set -u
in_use=$(docker ps -aq | xargs -r docker inspect --format '{{.Image}}' 2>/dev/null | sort -u)
for id in "$@"; do
if printf '%s\n' "$in_use" | grep -qx -- "$id"; then
echo "kept ${id:7:12} (in use)"
continue
fi
tags=$(docker image inspect --format '{{range .RepoTags}}{{.}} {{end}}' "$id" 2>/dev/null) || continue
# shellcheck disable=SC2086 # the tags are separate words on purpose
if docker image rm -- ${tags:-$id} >/dev/null 2>&1; then
echo "removed ${tags:-$id}"
else
echo "kept ${id:7:12}"
fi
done
exit 0