From de4263376a79eeb427ed0ae46efe3298de15e9cd Mon Sep 17 00:00:00 2001 From: Cee Chen <549407+cee-chen@users.noreply.github.com> Date: Fri, 26 Apr 2024 09:44:15 -0700 Subject: [PATCH] [buildkite] Delete all but most recent 50 PR staging subdirs (#7707) --- .../pipelines/pipeline_prune_staging_docs.sh | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/.buildkite/scripts/pipelines/pipeline_prune_staging_docs.sh b/.buildkite/scripts/pipelines/pipeline_prune_staging_docs.sh index 2fa808b33024..b66f377b93c3 100644 --- a/.buildkite/scripts/pipelines/pipeline_prune_staging_docs.sh +++ b/.buildkite/scripts/pipelines/pipeline_prune_staging_docs.sh @@ -25,14 +25,17 @@ BUCKET=${GPROJECT}-${EUI_DOCS_PROJECT} # https://cloud.google.com/storage/docs/gsutil/commands/ls ls_options=( -d # only list directories - -l # Print additional details about the subdir + -l # include additional details about the subdir, notably the date as a second field ) -echo "Listing all PR staging links" -gsutil ls "${ls_options[@]}" "gs://${BUCKET}/pr_*" # | sort -k 2 # sort by the 2nd field returned by -l which is the 'created by' timestamp - -# https://cloud.google.com/storage/docs/gsutil/commands/rm -rm_options=( - -r # recursive, delete everything inside subdir - -m # enables multi-threading for large numbers of objects +echo "Getting all but the most recent 50 PR staging links..." +list=$(gsutil ls "${ls_options[@]}" "gs://${BUCKET}/pr_*" \ + | sort -k 2 `# sort by the 2nd field returned by -l which is a timestamp` \ + | head -n -50 `# remove the last 50 items, so basically keep the latest 50 staging docs` \ ) -# gsutil rm "${rm_options[@]}" "gs://${BUCKET}/pr_TODO" +while IFS= read -r line || [[ -n $line ]]; do + url="$(echo -e "${line}" | tr -d '[:space:]')" # trim the leading whitespaces + url=${url%/} # trim the trailing slash + echo "Deleting $url" + # https://cloud.google.com/storage/docs/gsutil/commands/rm + gsutil -m rm -r "$url" +done < <(printf '%s' "$list")