|
1 | 1 | #!/usr/bin/env bash |
2 | 2 |
|
3 | 3 | DRY_RUN=false |
| 4 | +DELETE_PR=false |
| 5 | +DELETE_CI=false |
4 | 6 |
|
5 | 7 | while [[ $# -gt 0 ]]; do |
6 | 8 | case "$1" in |
7 | 9 | --dry-run|-n) |
8 | 10 | DRY_RUN=true |
9 | 11 | shift |
10 | 12 | ;; |
| 13 | + --delete-pr) |
| 14 | + DELETE_PR=true |
| 15 | + shift |
| 16 | + ;; |
| 17 | + --delete-ci) |
| 18 | + DELETE_CI=true |
| 19 | + shift |
| 20 | + ;; |
11 | 21 | --help|-h) |
12 | | - echo "Usage: $0 [--dry-run]" |
| 22 | + echo "Usage: $0 [--dry-run] [--delete-pr] [--delete-ci]" |
13 | 23 | exit 0 |
14 | 24 | ;; |
15 | 25 | *) |
16 | 26 | echo "Unknown option: $1" >&2 |
17 | | - echo "Usage: $0 [--dry-run]" >&2 |
| 27 | + echo "Usage: $0 [--dry-run] [--delete-pr] [--delete-ci]" >&2 |
18 | 28 | exit 1 |
19 | 29 | ;; |
20 | 30 | esac |
21 | 31 | done |
22 | 32 |
|
| 33 | +if [[ "${DELETE_PR}" == "false" && "${DELETE_CI}" == "false" ]]; then |
| 34 | + DELETE_PR=true |
| 35 | +fi |
| 36 | + |
23 | 37 | get_container_package_name() { |
24 | 38 | local container_name=$1 |
25 | 39 |
|
@@ -66,12 +80,12 @@ delete_pr_images() { |
66 | 80 |
|
67 | 81 | while IFS= read -r tag; do |
68 | 82 | local pull_request |
69 | | - if [[ "${tag}" =~ ^pr-([0-9]+)- ]]; then |
| 83 | + if [[ "${tag}" =~ ^pr-([0-9]+)(-.+)?$ ]]; then |
70 | 84 | pull_request=${BASH_REMATCH[1]} |
71 | | - elif [[ "${tag}" =~ ^githubactions-pr-([0-9]+)- ]]; then |
| 85 | + elif [[ "${tag}" =~ ^githubactions-pr-([0-9]+)(-.+)?$ ]]; then |
72 | 86 | pull_request=${BASH_REMATCH[1]} |
73 | 87 | else |
74 | | - echo "Tag ${tag} does not match expected PR tag format, skipping." |
| 88 | + echo "Tag ${tag} does not match expected PR tag format for container ${container_name}, skipping." |
75 | 89 | continue |
76 | 90 | fi |
77 | 91 |
|
@@ -108,16 +122,75 @@ delete_pr_images() { |
108 | 122 | done <<<"${tags}" |
109 | 123 | } |
110 | 124 |
|
| 125 | +delete_ci_images() { |
| 126 | + local container_name=$1 |
| 127 | + local package_name |
| 128 | + local versions_json |
| 129 | + local tags |
| 130 | + |
| 131 | + if [[ -z "${container_name}" ]]; then |
| 132 | + echo "Container name is required" >&2 |
| 133 | + return 1 |
| 134 | + fi |
| 135 | + |
| 136 | + package_name=$(get_container_package_name "${container_name}") |
| 137 | + versions_json=$(get_container_versions_json "${container_name}") |
| 138 | + tags=$(jq -r '[.[].metadata.container.tags[]?] | unique | .[]' <<<"${versions_json}") |
| 139 | + |
| 140 | + if [[ -z "${tags}" ]]; then |
| 141 | + echo "No tags found for container ${container_name}, skipping." |
| 142 | + return 0 |
| 143 | + fi |
| 144 | + |
| 145 | + while IFS= read -r tag; do |
| 146 | + if [[ ! "${tag}" =~ ^ci-[0-9a-fA-F]{8}.*$ ]] && [[ ! "${tag}" =~ ^githubactions-ci-[0-9a-fA-F]{8}.*$ ]]; then |
| 147 | + echo "Tag ${tag} does not match expected CI tag format for container ${container_name}, skipping." |
| 148 | + continue |
| 149 | + fi |
| 150 | + |
| 151 | + jq -r --arg tag "${tag}" '.[] | select(.metadata.container.tags[]? == $tag) | .id' \ |
| 152 | + <<<"${versions_json}" \ |
| 153 | + | while IFS= read -r version_id; do |
| 154 | + if [[ -n "${version_id}" ]]; then |
| 155 | + if [[ "${DRY_RUN}" == "true" ]]; then |
| 156 | + echo "[DRY RUN] Would delete CI image with tag ${tag} (version ID: ${version_id}) from container ${container_name}." |
| 157 | + else |
| 158 | + echo "Deleting CI image with tag ${tag} (version ID: ${version_id}) from container ${container_name}..." |
| 159 | + gh api \ |
| 160 | + -H "Accept: application/vnd.github+json" \ |
| 161 | + -X DELETE \ |
| 162 | + "/orgs/nhsdigital/packages/container/${package_name}/versions/${version_id}" |
| 163 | + fi |
| 164 | + fi |
| 165 | + done |
| 166 | + done <<<"${tags}" |
| 167 | +} |
| 168 | + |
111 | 169 |
|
112 | 170 | language_folders=$(find src/languages -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | jq -R -s -c 'split("\n")[:-1]') |
113 | 171 | project_folders=$(find src/projects -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | jq -R -s -c 'split("\n")[:-1]') |
114 | 172 |
|
115 | 173 | for container_name in $(jq -r '.[]' <<<"${project_folders}"); do |
116 | | - delete_pr_images "${container_name}" |
| 174 | + if [[ "${DELETE_PR}" == "true" ]]; then |
| 175 | + delete_pr_images "${container_name}" |
| 176 | + fi |
| 177 | + if [[ "${DELETE_CI}" == "true" ]]; then |
| 178 | + delete_ci_images "${container_name}" |
| 179 | + fi |
117 | 180 | done |
118 | 181 |
|
119 | 182 | for container_name in $(jq -r '.[]' <<<"${language_folders}"); do |
120 | | - delete_pr_images "${container_name}" |
| 183 | + if [[ "${DELETE_PR}" == "true" ]]; then |
| 184 | + delete_pr_images "${container_name}" |
| 185 | + fi |
| 186 | + if [[ "${DELETE_CI}" == "true" ]]; then |
| 187 | + delete_ci_images "${container_name}" |
| 188 | + fi |
121 | 189 | done |
122 | 190 |
|
123 | | -delete_pr_images "base" |
| 191 | +if [[ "${DELETE_PR}" == "true" ]]; then |
| 192 | + delete_pr_images "base" |
| 193 | +fi |
| 194 | +if [[ "${DELETE_CI}" == "true" ]]; then |
| 195 | + delete_ci_images "base" |
| 196 | +fi |
0 commit comments