Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add action to clean nightly assets #1371

Merged
Merged
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
49 changes: 49 additions & 0 deletions .github/workflows/cleanup-nightly-assets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Cleanup Nightly Assets

on:
workflow_dispatch: # Manual trigger
schedule:
- cron: '0 0 * * 0' # At 00:00 on Sunday, UTC

jobs:
nightly:
if: ${{ github.repository == 'shipwright-io/build' }}
runs-on: ubuntu-latest
permissions:
contents: write # To be able to update releases.

steps:
- uses: actions/checkout@v3
- name: Get current month
id: currentmonth
run: echo "date=$(date +'%Y-%m')" >> $GITHUB_OUTPUT
- name: Get previous month
id: previousmonth
run: echo "date=$(date -d "1 month ago" +%Y-%m')" >> $GITHUB_OUTPUT
- name: Get two months ago
id: twomonthsago
run: echo "date=$(date -d "2 month ago" +%Y-%m)" >> $GITHUB_OUTPUT
- name: Generate and upload release YAMLs
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
WORK_DIR=`mktemp -d -p "$DIR"`
gh release download nightly -D ${WORK_DIR}

ASSETS_TOTAL=$(ls $WORK_DIR | wc -l)
echo "[INFO] Currently ${ASSETS_TOTAL} assets for nightly release"

ASSETS_TO_REMOVE=$(ls $WORK_DIR | grep -v "${{ steps.currentmonth.outputs.date }}\|${{ steps.previousmonth.outputs.date }}\|${{ steps.twomonthsago.outputs.date }}" | wc -l)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Today I learned: grep -v means "get everything that does not match".

if [ "$ASSETS_TO_REMOVE" -eq "0" ]; then
echo "[INFO] Nothing to delete"
exit 0
fi

find $WORK_DIR -type f -iname "*.yaml" -printf "%f\n" | grep -v "${{ steps.currentmonth.outputs.date }}\|${{ steps.previousmonth.outputs.date }}\|${{ steps.twomonthsago.outputs.date }}" |
while IFS= read FILE; do
gh release delete-asset nightly $FILE -y
done

rm -rf "$WORK_DIR"
echo "[INFO] Deleted temporary directory ${WORK_DIR}"