Skip to content

Commit 4f15d50

Browse files
authored
BUILD-10625 Fix artifact cleanup timeout (#228)
1 parent d6f6b07 commit 4f15d50

3 files changed

Lines changed: 25 additions & 13 deletions

File tree

pr_cleanup/artifact_template.tpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
{{- if .artifacts -}}
12
{{tablerow "NAME" "ID" "SIZE (BYTES)" "BRANCH" "HEAD_SHA" "RUN_ID"}}
23
{{- range .artifacts -}}
34
{{- tablerow .name .id .size_in_bytes .workflow_run.head_branch .workflow_run.head_sha .workflow_run.id -}}
45
{{- end -}}
56
{{- tablerender -}}
7+
{{- end -}}

pr_cleanup/cleanup.sh

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,32 @@ echo "::endgroup::"
3838

3939
echo "::group::Artifact Cleanup"
4040
echo "Fetching list of artifacts on $GITHUB_REPOSITORY for $GITHUB_HEAD_REF"
41-
tpl_tmp_file="$(mktemp)"
42-
# shellcheck disable=SC2016
43-
envsubst '$GITHUB_HEAD_REF' < "$CURDIR"/artifact_template.tpl > "$tpl_tmp_file"
44-
ARTIFACT_TEMPLATE="$(cat "$tpl_tmp_file")"
41+
ARTIFACT_TEMPLATE="$(cat "$CURDIR"/artifact_template.tpl)"
4542

46-
ARTIFACT_API_URL="/repos/$GITHUB_REPOSITORY/actions/artifacts"
47-
gh api "$ARTIFACT_API_URL" --paginate --template "$ARTIFACT_TEMPLATE"
43+
RUNS_API_URL="/repos/$GITHUB_REPOSITORY/actions/runs"
44+
45+
# List workflow runs scoped to the PR branch instead of paginating all repo artifacts.
46+
# This avoids timeouts in large repositories with many accumulated artifacts.
47+
runIds="$(gh api -X GET "$RUNS_API_URL" -f branch="$GITHUB_HEAD_REF" -f per_page=100 --paginate --jq '.workflow_runs[].id')"
48+
49+
for runId in $runIds; do
50+
gh api "/repos/$GITHUB_REPOSITORY/actions/runs/$runId/artifacts" --paginate --template "$ARTIFACT_TEMPLATE"
51+
done
4852
echo
4953

50-
artifactIds="$(gh api "$ARTIFACT_API_URL" --paginate --jq '.artifacts[] | select(.workflow_run.head_branch == "'"$GITHUB_HEAD_REF"'") | .id')"
5154
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
55+
for runId in $runIds; do
56+
artifactIds="$(gh api "/repos/$GITHUB_REPOSITORY/actions/runs/$runId/artifacts" --paginate --jq '.artifacts[].id')"
57+
for artifactId in $artifactIds
58+
do
59+
echo "Deleting artifact: $artifactId"
60+
gh api -X DELETE "/repos/$GITHUB_REPOSITORY/actions/artifacts/$artifactId" || true
61+
done
5662
done
5763
echo
5864

5965
echo "Fetching list of artifacts after deletion"
60-
gh api "$ARTIFACT_API_URL" --paginate --template "$ARTIFACT_TEMPLATE"
66+
for runId in $runIds; do
67+
gh api "/repos/$GITHUB_REPOSITORY/actions/runs/$runId/artifacts" --paginate --template "$ARTIFACT_TEMPLATE"
68+
done
6169
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)