Cleanup old artifacts #16
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
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 }} |