Skip to content

Commit 06af01a

Browse files
committed
Snapshot cleanup
Fixes #1
1 parent 8c21779 commit 06af01a

2 files changed

Lines changed: 86 additions & 1 deletion

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
name: Snapshot Cleanup
2+
3+
on:
4+
schedule:
5+
- cron: '12 5 * * 6'
6+
workflow_dispatch:
7+
8+
jobs:
9+
cleanup:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
packages: write
13+
contents: read
14+
steps:
15+
- name: Cleanup
16+
env:
17+
ORG: ${{ github.repository_owner }}
18+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
19+
run: |
20+
echo "Organization is $ORG"
21+
PACKAGES_VISIBILITY=public
22+
PACKAGES_PER_PAGE=100
23+
VERSIONS_PER_PAGE=100
24+
MAX_SNAPSHOT_AGE=`date +%Y-%m-%d'T'%H:%M:%S'Z' -d "7 days ago"`
25+
26+
packages_page=1
27+
while true; do
28+
packages_response=$(curl -sL \
29+
-H "Accept: application/vnd.github+json" \
30+
-H "Authorization: Bearer $GITHUB_TOKEN" \
31+
-H "X-GitHub-Api-Version: 2022-11-28" \
32+
"https://api.github.com/orgs/$ORG/packages?package_type=maven&visibility=$PACKAGES_VISIBILITY&per_page=$PACKAGES_PER_PAGE&page=$packages_page")
33+
34+
readarray -t names < <(jq -r '.[].name' <<< "$packages_response")
35+
36+
# If no packages
37+
(( ! ${#names[@]} )) && break
38+
39+
for name in "${names[@]}"; do
40+
echo "Checking $name"
41+
sleep 1 # Throttle
42+
43+
versions_page=1
44+
while true; do
45+
versions_response=$(curl -sL \
46+
-H "Accept: application/vnd.github+json" \
47+
-H "Authorization: Bearer $GITHUB_TOKEN" \
48+
-H "X-GitHub-Api-Version: 2022-11-28" \
49+
"https://api.github.com/orgs/$ORG/packages/maven/$name/versions?per_page=$VERSIONS_PER_PAGE&page=$versions_page")
50+
51+
readarray -t versions < <(jq -r '.[].name' <<< "$versions_response")
52+
53+
# If no versions
54+
(( ! ${#versions[@]} )) && break
55+
56+
for version in "${versions[@]}"; do
57+
echo "$version"
58+
done
59+
60+
readarray -t snapshot_ids < <(jq -r --arg date "$MAX_SNAPSHOT_AGE" \
61+
'.[] | select (.updated_at < $date) | select(.name | endswith("-SNAPSHOT")) | .id ' \
62+
<<< "$versions_response")
63+
64+
for snapshot_id in "${snapshot_ids[@]}"; do
65+
echo "Deleting $snapshot_id"
66+
curl -L \
67+
-X DELETE \
68+
-H "Accept: application/vnd.github+json" \
69+
-H "Authorization: Bearer $GITHUB_TOKEN" \
70+
-H "X-GitHub-Api-Version: 2022-11-28" \
71+
https://api.github.com/orgs/$ORG/packages/maven/$name/versions/$snapshot_id
72+
done
73+
74+
versions_page=$((versions_page+1))
75+
done
76+
done
77+
78+
packages_page=$((packages_page+1))
79+
done

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Central repository for [XDEV's GitHub packages](https://github.com/orgs/xdev-sof
55
## How to use it?
66

77
> [!NOTE]
8-
> Related [GitHub documentation](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-apache-maven-registry#authenticating-with-a-personal-access-token)
8+
> Related [GitHub documentation](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-apache-maven-registry#authenticating-with-a-personal-access-token)
99
1010
``~/.m2/settings.xml``
1111
```xml
@@ -53,3 +53,9 @@ Central repository for [XDEV's GitHub packages](https://github.com/orgs/xdev-sof
5353

5454
Because we currently have around 50 individual repos that would all require you to add them to your ``settings.xml``.
5555
This is impossible to manage and also massively degrades performance.
56+
57+
#### Snapshots?
58+
59+
Can be published into the repo but will be automatically deleted after a few days.
60+
61+
[![Snapshot Cleanup](https://github.com/xdev-software/central/actions/workflows/snapshot-cleanup.yml/badge.svg)](https://github.com/xdev-software/central/actions/workflows/snapshot-cleanup.yml)

0 commit comments

Comments
 (0)