|
1 | | -name: Limit Posts Folder to 20 Files |
| 1 | +name: Archive and Limit Posts Folder as Release |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | push: |
5 | 5 | paths: |
6 | 6 | - 'posts/**' |
7 | | - workflow_dispatch: |
8 | 7 |
|
9 | 8 | jobs: |
10 | | - manage-posts: |
| 9 | + archive-and-prune: |
11 | 10 | runs-on: ubuntu-latest |
12 | | - |
13 | 11 | steps: |
14 | 12 | - name: Checkout repo |
15 | 13 | uses: actions/checkout@v4 |
16 | 14 |
|
17 | | - - name: Limit /posts to 20 files |
| 15 | + - name: Prepare archive and delete files |
| 16 | + id: prune |
18 | 17 | run: | |
19 | 18 | cd posts |
20 | 19 | total_files=$(ls -1 | wc -l) |
21 | 20 | echo "Total files in posts: $total_files" |
22 | 21 | if [ "$total_files" -gt 20 ]; then |
23 | 22 | to_delete=$((total_files - 20)) |
24 | 23 | echo "Deleting $to_delete oldest files" |
25 | | - ls -1tr | head -n $to_delete | xargs rm -f |
| 24 | +
|
| 25 | + files_to_delete=$(ls -1tr | head -n $to_delete) |
| 26 | + echo "$files_to_delete" |
| 27 | +
|
| 28 | + archive_name="../Archive-$(date +'%Y-%m-%d').tar.gz" |
| 29 | + echo "Creating archive $archive_name" |
| 30 | +
|
| 31 | + tar -czf "$archive_name" $files_to_delete |
| 32 | + echo "archive_name=$archive_name" >> $GITHUB_OUTPUT |
| 33 | +
|
| 34 | + echo "$files_to_delete" | xargs rm -f |
| 35 | +
|
| 36 | + echo "deleted=true" >> $GITHUB_OUTPUT |
26 | 37 | else |
27 | | - echo "No files need to be deleted." |
| 38 | + echo "deleted=false" >> $GITHUB_OUTPUT |
28 | 39 | fi |
29 | 40 |
|
30 | | - - name: Commit changes if any files deleted |
| 41 | + - name: Commit changes if files deleted |
| 42 | + if: steps.prune.outputs.deleted == 'true' |
31 | 43 | run: | |
32 | | - if [[ -n $(git status -s) ]]; then |
33 | | - git config user.name "github-actions[bot]" |
34 | | - git config user.email "github-actions[bot]@users.noreply.github.com" |
35 | | - git add posts/ |
36 | | - git commit -m "chore: prune posts folder to max 20 files" |
37 | | - git push |
38 | | - else |
39 | | - echo "No changes to commit." |
40 | | - fi |
| 44 | + git config user.name "github-actions[bot]" |
| 45 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 46 | + git add posts/ |
| 47 | + git commit -m "chore: prune posts folder to max 20 files" |
| 48 | + git push |
| 49 | +
|
| 50 | + - name: Create or update release with archive |
| 51 | + if: steps.prune.outputs.deleted == 'true' |
| 52 | + uses: softprops/action-gh-release@v1 |
| 53 | + with: |
| 54 | + tag_name: "archive-$(date +'%Y-%m-%d')" |
| 55 | + name: "Archive: $(date +'%Y-%m-%d')" |
| 56 | + body: "Auto archive of deleted posts on $(date +'%Y-%m-%d')" |
| 57 | + draft: false |
| 58 | + prerelease: false |
| 59 | + files: ${{ steps.prune.outputs.archive_name }} |
| 60 | + env: |
| 61 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments