Skip to content

Commit 1b01154

Browse files
ferfergaPromoFaux
authored andcommitted
Remove dangling images from GHCR on schedule (pi-hole#941)
* Remove dangling images from GHCR on schedule Signed-off-by: Fernando Fernández <[email protected]> * Commit .sh file as executable, so preparation is not needed at runtime Signed-off-by: Fernando Fernández <[email protected]> * Update .github/workflows/maintenance.yml Signed-off-by: Adam Warner <[email protected]> * Update .github/workflows/maintenance.yml Signed-off-by: Fernando Fernández <[email protected]> Co-authored-by: Adam Warner <[email protected]>
1 parent a1d99ef commit 1b01154

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

.github/workflows/maintenance.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Maintenance 🧰
2+
3+
on:
4+
schedule:
5+
- cron: "0 0 * * *"
6+
workflow_dispatch:
7+
8+
jobs:
9+
dangling_images_removal:
10+
runs-on: ubuntu-latest
11+
name: Cleanup dangling images from GHCR 🧹📦
12+
# Errors will probably be caused by excesses in API quota, so we can safely continue.
13+
# Remaining images will be removed in the next scheduled run.
14+
continue-on-error: true
15+
# Pay attention that the org name is not necessary here, as gh will automatically take the one from the logged in user.
16+
# REMEMBER TO KEEP THIS UP TO DATE WITH A LIST OF THE PUBLISHED PACKAGES.
17+
strategy:
18+
fail-fast: false
19+
matrix:
20+
container:
21+
- 'pihole'
22+
- 'ftl-build'
23+
- 'docker-pi-hole-base'
24+
25+
steps:
26+
- name: Clone repository 🔽
27+
uses: actions/[email protected]
28+
29+
- name: Run deletion script 🗑
30+
run: ./delete_ghcr_dangling_images.sh ${{ matrix.container }}
31+
working-directory: ./.github/workflows/maintenance
32+
env:
33+
GITHUB_TOKEN: ${{ secrets.DELETE_PACKAGE_TOKEN }}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Simple script to remove dangling images from GHCR
5+
# You need to be logged to 'gh' first.
6+
7+
# package_name syntax. i.e: jellyfin-vue
8+
container="$1"
9+
temp_file="ghcr_prune.ids"
10+
rm -rf $temp_file
11+
12+
echo "Fetching dangling images from GHCR..."
13+
gh api /user/packages/container/${container}/versions --paginate > $temp_file
14+
15+
ids_to_delete=$(cat "$temp_file" | jq -r '.[] | select(.metadata.container.tags==[]) | .id')
16+
17+
if [ "${ids_to_delete}" = "" ]
18+
then
19+
echo "There are no dangling images to remove for this package"
20+
exit 0
21+
fi
22+
23+
echo -e "\nDeleting dangling images..."
24+
while read -r line; do
25+
id="$line"
26+
## Workaround for https://github.com/cli/cli/issues/4286 and https://github.com/cli/cli/issues/3937
27+
echo -n | gh api --method DELETE /user/packages/container/${container}/versions/${id} --input -
28+
echo Dangling image with ID $id deleted successfully
29+
done <<< $ids_to_delete
30+
31+
rm -rf $temp_file
32+
echo -e "\nAll the dangling images have been removed successfully"
33+
exit 0

0 commit comments

Comments
 (0)