Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 59 additions & 31 deletions scripts/delete-test-mirror-entries.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
# This script is called in the delete-test-mirror-pr GitHub workflow.
# It must be run from the root of ddoghq/images.
#
# The tags to remove are read from the PR's group in mirror.yaml rather than
# from a hard-coded variant list. The workflow always runs this script from the
# default branch, so the group may have been created with a different set of
# variants (e.g. before or after an EA JDK was added or removed).
#
# Required env var:
# PR_NUMBER — pull request number in dd-trace-java-docker-build (numeric)

set -euo pipefail

readonly SOURCE_PREFIX="ghcr.io/datadog/dd-trace-java-docker-build"
readonly DEST_REPO="dd-trace-java-docker-build"
readonly CI_VARIANTS=(base 7 8 11 17 21 25 tip zulu8 zulu11 oracle8 ibm8 semeru8 semeru11 semeru17 graalvm17 graalvm21 graalvm25)

if ! [[ "${PR_NUMBER}" =~ ^[0-9]+$ ]]; then
echo "::error::PR_NUMBER must be numeric (got: '${PR_NUMBER}')" >&2
Expand All @@ -21,37 +25,51 @@ fi

readonly PREFIX="${PR_NUMBER}_merge-"

require_entry_exists() {
local tag="$1"
if ! grep -qF "\"${tag}\"" mirror.yaml; then
echo "::error::Missing mirror.yaml tag for ${SOURCE_PREFIX}:${tag}" >&2
exit 1
fi
if ! grep -qF "${SOURCE_PREFIX}:${tag}" mirror.lock.yaml; then
echo "::error::Missing mirror.lock.yaml entry for ${SOURCE_PREFIX}:${tag}" >&2
exit 1
fi
# Perl snippet defining $group_re, which matches the whole mirror.yaml group
# whose tags all start with ${PREFIX}. The leading quote anchors the prefix, so
# "12_merge-" does not match "123_merge-" tags. Capture group 1 holds the tag lines.
# shellcheck disable=SC2016 # Expanded by perl, not bash.
readonly GROUP_RE_PERL='my $group_re = qr/^ - source_prefix: "\Q$ENV{GROUP_SOURCE_PREFIX}\E"\n dest_repo: "\Q$ENV{GROUP_DEST_REPO}\E"\n tags:\n((?: - "\Q$ENV{GROUP_PREFIX}\E[^"\n]+"\n)+) replication_target: ""(?:\n|\z)/m;'

# run_group_perl CODE [FILE...] — run perl in slurp mode with $group_re defined.
run_group_perl() {
local code="$1"
shift
GROUP_SOURCE_PREFIX="${SOURCE_PREFIX}" \
GROUP_DEST_REPO="${DEST_REPO}" \
GROUP_PREFIX="${PREFIX}" \
perl -0777 -e "${GROUP_RE_PERL} ${code}" "$@"
}

# Prints the tags of the PR's group in mirror.yaml, one per line.
# Exits 46 unless exactly one matching group is found.
list_group_tags() {
run_group_perl '
local $_ = <>;
my @groups;
push @groups, $1 while /$group_re/g;
exit 46 if @groups != 1;
print "$1\n" while $groups[0] =~ /"([^"]+)"/g;
' mirror.yaml
}

remove_group_from_mirror_yaml() {
local file="mirror.yaml"
local block

block="$(
printf ' - source_prefix: "%s"\n dest_repo: "%s"\n tags:\n' "${SOURCE_PREFIX}" "${DEST_REPO}"
for variant in "${CI_VARIANTS[@]}"; do
printf ' - "%s%s"\n' "${PREFIX}" "${variant}"
done
printf ' replication_target: ""\n'
)"

TARGET_BLOCK="${block}" perl -0pe '
BEGIN { $removed = 0 }
$removed = s/\Q$ENV{TARGET_BLOCK}\E\n?//s;
END { exit($removed ? 0 : 44) }
run_group_perl '
local $_ = <>;
s/$group_re// or exit 44;
print;
' "${file}" > "${file}.tmp" && mv "${file}.tmp" "${file}"
}

require_lock_entry_exists() {
local tag="$1"
if ! grep -qxF " - source: ${SOURCE_PREFIX}:${tag}" mirror.lock.yaml; then
echo "::error::Missing mirror.lock.yaml entry for ${SOURCE_PREFIX}:${tag}" >&2
exit 1
fi
}

remove_from_mirror_lock() {
local tag="$1"
local src=" - source: ${SOURCE_PREFIX}:${tag}"
Expand All @@ -68,16 +86,26 @@ remove_from_mirror_lock() {
' "${file}" > "${file}.tmp" && mv "${file}.tmp" "${file}"
}

echo "Validating test mirror entries for prefix '${PREFIX}'..."
for variant in "${CI_VARIANTS[@]}"; do
require_entry_exists "${PREFIX}${variant}"
echo "Looking up test mirror entries for prefix '${PREFIX}'..."
if ! group_tags="$(list_group_tags)"; then
echo "::error::Expected exactly one mirror.yaml group with '${PREFIX}*' tags for ${SOURCE_PREFIX}" >&2
exit 1
fi
TAGS=()
while IFS= read -r tag; do
TAGS+=("${tag}")
done <<< "${group_tags}"
echo "Found ${#TAGS[@]} tags: ${TAGS[*]}"

echo "Validating mirror.lock.yaml entries..."
for tag in "${TAGS[@]}"; do
require_lock_entry_exists "${tag}"
done

echo "Removing test mirror entries for prefix '${PREFIX}'..."
remove_group_from_mirror_yaml
for variant in "${CI_VARIANTS[@]}"; do
tag="${PREFIX}${variant}"
for tag in "${TAGS[@]}"; do
remove_from_mirror_lock "${tag}"
done

echo "Removed grouped mirror.yaml entry and ${#CI_VARIANTS[@]} entries from mirror.lock.yaml"
echo "Removed grouped mirror.yaml entry and ${#TAGS[@]} entries from mirror.lock.yaml"
Loading