Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pr_cleanup/artifact_template.tpl
Original file line number Diff line number Diff line change
@@ -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 -}}
32 changes: 20 additions & 12 deletions pr_cleanup/cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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::"
4 changes: 3 additions & 1 deletion spec/pr_cleanup_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading