@@ -38,24 +38,32 @@ echo "::endgroup::"
3838
3939echo " ::group::Artifact Cleanup"
4040echo " 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
4852echo
4953
50- artifactIds=" $( gh api " $ARTIFACT_API_URL " --paginate --jq ' .artifacts[] | select(.workflow_run.head_branch == "' " $GITHUB_HEAD_REF " ' ") | .id' ) "
5154echo " 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
5662done
5763echo
5864
5965echo " 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
6169echo " ::endgroup::"
0 commit comments