85 lines
2.1 KiB
YAML
85 lines
2.1 KiB
YAML
name: Clean
|
||
|
||
on:
|
||
schedule:
|
||
- cron: "0 1 * * *"
|
||
workflow_dispatch:
|
||
push:
|
||
|
||
|
||
concurrency:
|
||
group: ${{ github.workflow }}-${{ github.ref }}
|
||
cancel-in-progress: true
|
||
|
||
|
||
jobs:
|
||
clean:
|
||
strategy:
|
||
matrix:
|
||
job:
|
||
- fsn-1-hetzner-host
|
||
- hel-1-hetzner-host
|
||
- rog-host
|
||
|
||
runs-on: ${{ matrix.job }}
|
||
name: ${{ matrix.job }}
|
||
steps:
|
||
- name: Show details
|
||
run: |
|
||
docker ps -a
|
||
docker image ls -a --format json | jq
|
||
docker network ls
|
||
|
||
- name: Generic clean
|
||
run: |
|
||
docker container prune --force
|
||
docker volume prune --all --force
|
||
docker network prune --force
|
||
|
||
- name: Clean old Renovate Bot (keep only latest)
|
||
run: |
|
||
#!/bin/sh
|
||
set -eu
|
||
|
||
REPO="ghcr.io/renovatebot/renovate"
|
||
|
||
# 取出該 repo 的所有 images
|
||
images="$(docker image ls -a --format json | jq -s --arg repo "$REPO" '
|
||
map(select(.Repository == $repo))
|
||
')"
|
||
|
||
count="$(printf '%s' "$images" | jq 'length')"
|
||
|
||
if [ "$count" -eq 0 ]; then
|
||
echo "No images found for $REPO. Nothing to do."
|
||
exit 0
|
||
fi
|
||
|
||
# 找最新的一張(CreatedAt 最大)
|
||
keep_id="$(printf '%s' "$images" | jq -r '
|
||
max_by(.CreatedAt) | .ID
|
||
')"
|
||
|
||
echo "Keeping latest $REPO image: $keep_id"
|
||
|
||
# 找其餘要刪的 IDs
|
||
remove_ids="$(printf '%s' "$images" | jq -r '
|
||
map(.ID) | unique | map(select(. != "'"$keep_id"'")) | .[]
|
||
')"
|
||
|
||
if [ -z "$remove_ids" ]; then
|
||
echo "No older $REPO images to remove."
|
||
exit 0
|
||
fi
|
||
|
||
echo "Removing older $REPO images:"
|
||
printf ' - %s\n' $remove_ids
|
||
|
||
docker image rm -f $remove_ids
|
||
|
||
|
||
- name: Show details
|
||
run: |
|
||
docker ps -a
|
||
docker image ls -a --format json | jq
|
||
docker network ls
|