Skip to content

Cleanup old artifacts #8

Cleanup old artifacts

Cleanup old artifacts #8

---
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
# Get the list of all artifacts
gh api repos/$GITHUB_REPOSITORY/actions/artifacts --paginate | jq -r '.artifacts[] | select(.created_at < "'"$CUTOFF_DATE"'") | .id' | while read -r id; do
# Delete old artifacts
gh api --method DELETE repos/$GITHUB_REPOSITORY/actions/artifacts/$id
done
env:
GH_TOKEN: ${{ github.token }}