Update delete-old-releases.yml

This commit is contained in:
XargonWan 2024-01-02 13:37:10 +01:00 committed by GitHub
parent 9e29aa6ab3
commit eee266cc57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,46 +1,33 @@
name: Delete Old Releases name: Clean Old Releases
on: on:
schedule: schedule:
- cron: '0 0 * * 0' # Runs every Sunday at midnight - cron: '0 0 * * *' # Run daily at midnight
workflow_dispatch:
jobs: jobs:
delete-old-releases: clean-releases:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout code - name: Checkout Repository
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Set up Git - name: Set up Git
run: | run: |
git config --global user.name 'GitHub Actions' git config user.email "${{ secrets.GIT_MAIL }}"
git config --global user.email 'actions@github.com' git config user.name "${{ secrets.GIT_USERNAME }}"
- name: List releases - name: Clean Old Releases
id: list-releases run: |
run: | # Define the threshold date (1 month ago)
echo "::set-output name=releases::$(gh release list --json name,created_at --limit 100 --format json)" THRESHOLD_DATE=$(date -d "1 month ago" +%Y-%m-%dT%H:%M:%SZ)
- name: Delete old releases # Get releases older than 1 month
run: | OLD_RELEASES=$(jq --arg threshold "$THRESHOLD_DATE" '. | select(.published_at < $threshold) | .id' <<< "$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" https://api.github.com/repos/$GITHUB_REPOSITORY/releases)")
releases=${{ steps.list-releases.outputs.releases }}
current_time=$(date -u +%s)
two_weeks_ago=$(date -u -d "2 weeks ago" +%s)
for release in $(echo $releases | jq -r '.[] | @base64'); do # Delete old releases
_jq() { for release_id in $OLD_RELEASES; do
echo ${release} | base64 --decode | jq -r ${1} curl -X DELETE -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" "https://api.github.com/repos/$GITHUB_REPOSITORY/releases/$release_id"
} done
env:
release_created_at=$(_jq '.created_at') GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
release_timestamp=$(date -u -d "$release_created_at" +%s)
if [[ $release_timestamp -lt $two_weeks_ago ]]; then
release_name=$(_jq '.name')
echo "Deleting release: $release_name"
gh release delete "$release_name" -y
fi
done
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}