Disk cleanup #40
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: Disk cleanup | |
on: | |
schedule: | |
# runs every monday | |
- cron: '0 0 * * 1' | |
workflow_dispatch: | |
jobs: | |
cleanup: | |
runs-on: [self-hosted, media] | |
environment: Media | |
steps: | |
- name: Import environment variables from a file | |
run: | |
grep -E '^\s*DATA_DIR' "/opt/deployment-specific-files/.env" >> $GITHUB_ENV | |
- name: Disk cleanup until required space is available | |
run: | | |
REQUIRED_SPACE=21 # Required space in GB | |
TARGET_DIR="$DATA_DIR" # Target directory to clean up | |
counter=0 # Counter to prevent infinite loop | |
while [ $counter -lt 100 ]; do | |
AVAILABLE_SPACE=$(df -BG --output=avail "$TARGET_DIR" | tail -n 1 | tr -dc '0-9') | |
echo "Available disk space: $AVAILABLE_SPACE GB" | |
if (( AVAILABLE_SPACE >= REQUIRED_SPACE )); then | |
echo "Required space is available. Cleanup is not required." | |
break | |
else | |
echo "Cleaning up the least recently accessed directory..." | |
docker run --rm -v "$TARGET_DIR:$TARGET_DIR" alpine /bin/ash -c "ls -1turF \"$TARGET_DIR\" | grep \"/$\" | sed 's/\/$//' | head -n 100 | xargs -I {} rm -rf \"$TARGET_DIR/{}\"" | |
fi | |
counter=$((counter+1)) | |
done |