Skip to content

Commit a992f86

Browse files
committed
BUILD-10625: fix artifact cleanup timeout by scoping lookup to PR branch runs
1 parent d6f6b07 commit a992f86

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

pr_cleanup/cleanup.sh

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,30 @@ tpl_tmp_file="$(mktemp)"
4343
envsubst '$GITHUB_HEAD_REF' < "$CURDIR"/artifact_template.tpl > "$tpl_tmp_file"
4444
ARTIFACT_TEMPLATE="$(cat "$tpl_tmp_file")"
4545

46-
ARTIFACT_API_URL="/repos/$GITHUB_REPOSITORY/actions/artifacts"
47-
gh api "$ARTIFACT_API_URL" --paginate --template "$ARTIFACT_TEMPLATE"
46+
RUNS_API_URL="/repos/$GITHUB_REPOSITORY/actions/runs"
47+
48+
# List workflow runs scoped to the PR branch instead of paginating all repo artifacts.
49+
# This avoids timeouts in large repositories with many accumulated artifacts.
50+
runIds="$(gh api "${RUNS_API_URL}?branch=${GITHUB_HEAD_REF}&per_page=100" --paginate --jq '.workflow_runs[].id')"
51+
52+
for runId in $runIds; do
53+
gh api "/repos/$GITHUB_REPOSITORY/actions/runs/$runId/artifacts" --paginate --template "$ARTIFACT_TEMPLATE"
54+
done
4855
echo
4956

50-
artifactIds="$(gh api "$ARTIFACT_API_URL" --paginate --jq '.artifacts[] | select(.workflow_run.head_branch == "'"$GITHUB_HEAD_REF"'") | .id')"
5157
echo "Deleting artifacts..."
52-
for artifactId in $artifactIds
53-
do
54-
echo "Deleting artifact: $artifactId"
55-
gh api -X DELETE "$ARTIFACT_API_URL/$artifactId" || true
58+
for runId in $runIds; do
59+
artifactIds="$(gh api "/repos/$GITHUB_REPOSITORY/actions/runs/$runId/artifacts" --paginate --jq '.artifacts[].id')"
60+
for artifactId in $artifactIds
61+
do
62+
echo "Deleting artifact: $artifactId"
63+
gh api -X DELETE "/repos/$GITHUB_REPOSITORY/actions/artifacts/$artifactId" || true
64+
done
5665
done
5766
echo
5867

5968
echo "Fetching list of artifacts after deletion"
60-
gh api "$ARTIFACT_API_URL" --paginate --template "$ARTIFACT_TEMPLATE"
69+
for runId in $runIds; do
70+
gh api "/repos/$GITHUB_REPOSITORY/actions/runs/$runId/artifacts" --paginate --template "$ARTIFACT_TEMPLATE"
71+
done
6172
echo "::endgroup::"

spec/pr_cleanup_spec.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ Describe "cleanup.sh"
1919
cat "$CACHE_KEY_TO_DELETE"
2020
elif [[ "$*" =~ "cache delete" ]]; then
2121
echo "" > "$CACHE_KEY_TO_DELETE"
22-
elif [[ "$*" =~ "api /repos/".*/actions/artifacts.*--paginate ]]; then
22+
elif [[ "$*" =~ "branch=" ]]; then
23+
echo "123456789"
24+
elif [[ "$*" =~ actions/runs/[0-9]+/artifacts ]]; then
2325
cat "$ARTIFACT_TO_DELETE"
2426
elif [[ "$*" =~ "api -X DELETE" ]]; then
2527
echo "" > "$ARTIFACT_TO_DELETE"

0 commit comments

Comments
 (0)