diff --git a/pr_cleanup/artifact_template.tpl b/pr_cleanup/artifact_template.tpl index 7ef9d4b9..c6904a8f 100644 --- a/pr_cleanup/artifact_template.tpl +++ b/pr_cleanup/artifact_template.tpl @@ -1,5 +1,7 @@ +{{- if .artifacts -}} {{tablerow "NAME" "ID" "SIZE (BYTES)" "BRANCH" "HEAD_SHA" "RUN_ID"}} {{- range .artifacts -}} {{- tablerow .name .id .size_in_bytes .workflow_run.head_branch .workflow_run.head_sha .workflow_run.id -}} {{- end -}} {{- tablerender -}} +{{- end -}} diff --git a/pr_cleanup/cleanup.sh b/pr_cleanup/cleanup.sh index 0cb02b64..a6a6ac73 100755 --- a/pr_cleanup/cleanup.sh +++ b/pr_cleanup/cleanup.sh @@ -38,24 +38,32 @@ echo "::endgroup::" echo "::group::Artifact Cleanup" echo "Fetching list of artifacts on $GITHUB_REPOSITORY for $GITHUB_HEAD_REF" -tpl_tmp_file="$(mktemp)" -# shellcheck disable=SC2016 -envsubst '$GITHUB_HEAD_REF' < "$CURDIR"/artifact_template.tpl > "$tpl_tmp_file" -ARTIFACT_TEMPLATE="$(cat "$tpl_tmp_file")" +ARTIFACT_TEMPLATE="$(cat "$CURDIR"/artifact_template.tpl)" -ARTIFACT_API_URL="/repos/$GITHUB_REPOSITORY/actions/artifacts" -gh api "$ARTIFACT_API_URL" --paginate --template "$ARTIFACT_TEMPLATE" +RUNS_API_URL="/repos/$GITHUB_REPOSITORY/actions/runs" + +# List workflow runs scoped to the PR branch instead of paginating all repo artifacts. +# This avoids timeouts in large repositories with many accumulated artifacts. +runIds="$(gh api -X GET "$RUNS_API_URL" -f branch="$GITHUB_HEAD_REF" -f per_page=100 --paginate --jq '.workflow_runs[].id')" + +for runId in $runIds; do + gh api "/repos/$GITHUB_REPOSITORY/actions/runs/$runId/artifacts" --paginate --template "$ARTIFACT_TEMPLATE" +done echo -artifactIds="$(gh api "$ARTIFACT_API_URL" --paginate --jq '.artifacts[] | select(.workflow_run.head_branch == "'"$GITHUB_HEAD_REF"'") | .id')" echo "Deleting artifacts..." -for artifactId in $artifactIds -do - echo "Deleting artifact: $artifactId" - gh api -X DELETE "$ARTIFACT_API_URL/$artifactId" || true +for runId in $runIds; do + artifactIds="$(gh api "/repos/$GITHUB_REPOSITORY/actions/runs/$runId/artifacts" --paginate --jq '.artifacts[].id')" + for artifactId in $artifactIds + do + echo "Deleting artifact: $artifactId" + gh api -X DELETE "/repos/$GITHUB_REPOSITORY/actions/artifacts/$artifactId" || true + done done echo echo "Fetching list of artifacts after deletion" -gh api "$ARTIFACT_API_URL" --paginate --template "$ARTIFACT_TEMPLATE" +for runId in $runIds; do + gh api "/repos/$GITHUB_REPOSITORY/actions/runs/$runId/artifacts" --paginate --template "$ARTIFACT_TEMPLATE" +done echo "::endgroup::" diff --git a/spec/pr_cleanup_spec.sh b/spec/pr_cleanup_spec.sh index d80cf117..d5d4f061 100755 --- a/spec/pr_cleanup_spec.sh +++ b/spec/pr_cleanup_spec.sh @@ -19,7 +19,9 @@ Describe "cleanup.sh" cat "$CACHE_KEY_TO_DELETE" elif [[ "$*" =~ "cache delete" ]]; then echo "" > "$CACHE_KEY_TO_DELETE" - elif [[ "$*" =~ "api /repos/".*/actions/artifacts.*--paginate ]]; then + elif [[ "$*" =~ "branch=" ]]; then + echo "123456789" + elif [[ "$*" =~ actions/runs/[0-9]+/artifacts ]]; then cat "$ARTIFACT_TO_DELETE" elif [[ "$*" =~ "api -X DELETE" ]]; then echo "" > "$ARTIFACT_TO_DELETE"