11#! /usr/bin/env bash
22
3+ DRY_RUN=false
4+
5+ while [[ $# -gt 0 ]]; do
6+ case " $1 " in
7+ --dry-run|-n)
8+ DRY_RUN=true
9+ shift
10+ ;;
11+ --help|-h)
12+ echo " Usage: $0 [--dry-run]"
13+ exit 0
14+ ;;
15+ * )
16+ echo " Unknown option: $1 " >&2
17+ echo " Usage: $0 [--dry-run]" >&2
18+ exit 1
19+ ;;
20+ esac
21+ done
22+
323get_container_package_name () {
424 local container_name=$1
525
@@ -40,16 +60,18 @@ delete_pr_images() {
4060 tags=$( jq -r ' [.[].metadata.container.tags[]?] | unique | .[]' <<< " ${versions_json}" )
4161
4262 if [[ -z " ${tags} " ]]; then
63+ echo " No tags found for container ${container_name} , skipping."
4364 return 0
4465 fi
4566
4667 while IFS= read -r tag; do
4768 local pull_request
4869 if [[ " ${tag} " =~ ^pr-([0-9]+)- ]]; then
4970 pull_request=${BASH_REMATCH[1]}
50- elif [[ " ${tag} " =~ ^githubactions-pr-([0-9]+)$ ]]; then
71+ elif [[ " ${tag} " =~ ^githubactions-pr-([0-9]+)- ]]; then
5172 pull_request=${BASH_REMATCH[1]}
5273 else
74+ echo " Tag ${tag} does not match expected PR tag format, skipping."
5375 continue
5476 fi
5577
@@ -72,11 +94,15 @@ delete_pr_images() {
7294 <<< " ${versions_json}" \
7395 | while IFS= read -r version_id; do
7496 if [[ -n " ${version_id} " ]]; then
75- echo " Deleting image with tag ${tag} (version ID: ${version_id} ) from container ${container_name} ..."
76- gh api \
77- -H " Accept: application/vnd.github+json" \
78- -X DELETE \
79- " /orgs/nhsdigital/packages/container/${package_name} /versions/${version_id} "
97+ if [[ " ${DRY_RUN} " == " true" ]]; then
98+ echo " [DRY RUN] Would delete image with tag ${tag} (version ID: ${version_id} ) from container ${container_name} ."
99+ else
100+ echo " Deleting image with tag ${tag} (version ID: ${version_id} ) from container ${container_name} ..."
101+ gh api \
102+ -H " Accept: application/vnd.github+json" \
103+ -X DELETE \
104+ " /orgs/nhsdigital/packages/container/${package_name} /versions/${version_id} "
105+ fi
80106 fi
81107 done
82108 done <<< " ${tags}"
0 commit comments