Skip to content

Tools - Delete old VM templates #29

Tools - Delete old VM templates

Tools - Delete old VM templates #29

name: "Tools - Delete old VM templates"
on:
workflow_dispatch:
permissions:
contents: read
jobs:
Delete:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
image: ['ubuntu-22.04', 'ubuntu-24.04']
steps:
- name: "Check if variable 'keep_snapshots' exist"
if: "${{ vars.KEEP_SNAPSHOTS == '' }}"
run: |
echo "Variable nicht gefunden. Job wird abgebrochen."
exit 1
- name: Install Hetzner Cloud Cli
uses: hetznercloud/setup-hcloud@4ff9bbeda4053fb69dd7fa1fcbdfcce7e648bf33 # v1.0.2
- name: Query und delete snapshots
id: hlcoud_snapshot
run: |
# List all snapshots
echo -e "$(hcloud image list --type "snapshot" -l "os=${{ matrix.image }}" -s "id:desc")\n"
# Query
objects=$(hcloud image list --output "columns=ID" --output "noheader" --type "snapshot" -l "os=${{ matrix.image }}" -s "id:desc" | tail -n +$((KEEP_SNAPSHOTS+1)) )
if [ -n "$objects" ]; then
echo "$(echo $objects | wc -w) Snapshot(s) zum Löschen gefunden:"
for i in $objects ; do
echo ID: "$i"
result=$(hcloud image delete $i --quiet)
echo "Snapshot wurde erfolgreich gelöscht."
done
else
echo "Keine Snapshots zum Löschen gefunden."
fi
env:
HCLOUD_TOKEN: ${{ secrets.HCLOUD_TOKEN }}
KEEP_SNAPSHOTS: ${{ vars.KEEP_SNAPSHOTS }}