Clean up preview site #120
This file contains hidden or 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: Clean up preview site | |
| on: | |
| workflow_dispatch: | |
| schedule: | |
| - cron: '00 8 * * 1' | |
| jobs: | |
| delete: | |
| name: Clean up preview site | |
| runs-on: ubuntu-latest | |
| env: | |
| CI: true | |
| PREVIEW_DIR: preview | |
| DAYS_OLD: 7 | |
| steps: | |
| - name: Checkout Repo | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: gh-pages | |
| # This makes Actions fetch all Git history so that the cleanup script can access metadata | |
| # from past commits. | |
| # Filter by `blob:none` to avoid downloading unnecessary file contents. | |
| fetch-depth: 0 | |
| filter: blob:none | |
| - name: Delete old preview directories | |
| run: | | |
| DATE_FROM_DAYS_AGO=`date --date="${{env.DAYS_OLD}} days ago" +"%Y"-"%m"-"%d"`; | |
| for i in $(find ${{ env.PREVIEW_DIR }} -mindepth 1 -maxdepth 1 -type d -print); do | |
| if [[ $(git log --before=$DATE_FROM_DAYS_AGO $i | tail -1) ]]; then | |
| echo "Deleting directory $i" | |
| rm -rf $i | |
| fi | |
| done | |
| if [[ $(git status --porcelain --untracked-files=no | wc -l) -eq 0 ]]; then | |
| echo "No directories to clean up" | |
| exit 0 | |
| fi | |
| git config --local user.name 'GitHub Actions' | |
| git config --local user.email '[email protected]' | |
| git add ${{ env.PREVIEW_DIR }} | |
| git commit --quiet -m "Clean up old preview directories" | |
| git pull --rebase | |
| git push | |
| shell: bash | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |