Files
cozystack/hack/retention-delete.sh
Aleksei Sviridkin da1f1cc387 ci(retention): delete with the workflow token
The app installation token lists package versions but its DELETE of a
listed version is answered with 404, while the repository's
GITHUB_TOKEN with packages: write deleted the same version without any
change to package settings. Deletes therefore go through GITHUB_TOKEN;
listing keeps the app token.

That deletion was shown on a package linked to this repository. A
package linked elsewhere is not attempted: it is skipped before its
versions are read and named in a warning, so it cannot turn every
apply run red. A failed read of the link still fails the run, and so
does a sweep in which no resolved package is linked, since that sweep
prunes nothing.

Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
2026-10-10 21:15:48 +03:00

41 lines
1.6 KiB
Bash
Executable File

#!/bin/sh
# Delete GHCR container-package versions for the retention workflow.
#
# Usage: APPLY=true|false ORG=<org> [DELETE_TOKEN=<token>] \
# retention-delete.sh <package> <label> <version-id>...
#
# Deletes with DELETE_TOKEN, required under APPLY=true, rather than the GH_TOKEN
# that listed the versions: the app installation token can list org package
# versions but has its DELETE answered with 404, while the workflow's
# GITHUB_TOKEN deleted a version of a package linked to its repository.
#
# <label> names what is deleted in the log ("version", "promotion candidate
# version"). Without APPLY=true it only reports. A refused delete is an error,
# not a warning: the remaining ids are still tried, then the exit is non-zero,
# so a token that cannot delete turns the run red instead of leaving it green
# with nothing removed.
set -eu
PKG="${1:?usage: retention-delete.sh <package> <label> <version-id>...}"
LABEL="${2:?usage: retention-delete.sh <package> <label> <version-id>...}"
shift 2
: "${ORG:?ORG is required}"
if [ "${APPLY:-false}" = "true" ]; then
: "${DELETE_TOKEN:?DELETE_TOKEN is required to delete}"
fi
enc="$(printf '%s' "$PKG" | sed 's|/|%2F|g')"
failed=0
for id in "$@"; do
if [ "${APPLY:-false}" != "true" ]; then
echo " • would delete ${PKG} ${LABEL} ${id}"
continue
fi
echo " ✗ deleting ${PKG} ${LABEL} ${id}"
if ! err="$(GH_TOKEN="$DELETE_TOKEN" gh api -X DELETE "/orgs/${ORG}/packages/container/${enc}/versions/${id}" 2>&1 >/dev/null)"; then
echo "::error::failed to delete ${PKG} ${LABEL} ${id}: ${err}"
failed=1
fi
done
exit "$failed"