Skip to content

Cleanup old artifacts #12

Cleanup old artifacts

Cleanup old artifacts #12

---
name: Cleanup old artifacts
on:
schedule:
- cron: '0 0 * * 0' # Runs weekly
workflow_dispatch: # Allow to trigger manually
env:
retention_days: 30
jobs:
cleanup-artifacts:
runs-on: ubuntu-22.04
steps:
- name: List and delete old artifacts
run: |
CUTOFF_DATE="$(date --date='${{ env.retention_days }} days ago' +'%Y-%m-%dT%H:%M:%SZ')"
echo "Cutoff date: $CUTOFF_DATE"
# Get the list of all artifacts
gh api repos/$GITHUB_REPOSITORY/actions/artifacts --paginate | jq -r '.artifacts[] | select(.created_at < "'"$CUTOFF_DATE"'") | [.id, .created_at] | @tsv' | while read -r id created_at; do
echo "Deleting artifact $id created on $created_at"
gh api --method DELETE repos/$GITHUB_REPOSITORY/actions/artifacts/$id
done
env:
GH_TOKEN: ${{ github.token }}