-
Notifications
You must be signed in to change notification settings - Fork 237
90 lines (83 loc) · 3.07 KB
/
pull-request-cleanup.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
name: Pull Request Cleanup
on:
pull_request:
types: [closed]
concurrency:
group: ${{ github.workflow }}
cancel-in-progress: false
env:
PULL_REQUEST_NUMBER: ${{ github.event.pull_request.number }}
PULL_REQUEST_BRANCH: ${{ github.head_ref }}
IMAGE_NAME: "ghcr.io/${{ github.repository }}"
PACKAGE_NAME: oracledb_exporter
PACKAGE_TYPE: container
USERNAME: ${{ github.repository_owner }}
permissions:
contents: write
packages: write
jobs:
cleanup-ghcr:
runs-on: ubuntu-latest
name: "cleanup ghcr"
steps:
- name: Cleanup versions
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
FILTER: rc.pr-${{ env.PULL_REQUEST_NUMBER }}
run: |
gh api \
-H "Accept: application/vnd.github+json" \
"/users/${{ env.USERNAME }}/packages/${{ env.PACKAGE_TYPE }}/${{ env.PACKAGE_NAME }}/versions" > versions.json
VERSIONS=$(jq -r '.[] | select(.metadata.container.tags[] | test("${{ env.FILTER }}")) | .id' versions.json)
for VERSION in $(echo -n "$VERSIONS")
do
TAG=$(jq -r \
--arg VERSION "$VERSION" \
'.[] | select(.id | tostring | test($VERSION)) | .metadata.container.tags | join(",")' \
versions.json
)
DIGEST=$(jq -r \
--arg VERSION "$VERSION" \
'.[] | select(.id | tostring | test($VERSION)) | .name' \
versions.json
)
echo "deleting ${IMAGE_NAME}:${TAG} with digest=${DIGEST}"
echo "deleting package PACKAGE_VERSION_ID=${VERSION}"
# https://github.com/cli/cli/issues/3937
echo -n | gh api \
--silent \
--method DELETE \
-H "Accept: application/vnd.github+json" \
"/users/${{ env.USERNAME }}/packages/${{ env.PACKAGE_TYPE }}/${{ env.PACKAGE_NAME }}/versions/$VERSION" \
--input -
done
cleanup-pre-releases:
runs-on: ubuntu-latest
name: "cleanup pre-releases"
steps:
- name: Cleanup releases
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
FILTER: rc.pr-${{ env.PULL_REQUEST_NUMBER }}
run: |
gh api \
-H "Accept: application/vnd.github+json" \
"/repos/${{ github.repository }}/releases" \
--jq='.[] | select(.prerelease=false) | select(.tag_name | test("${{ env.FILTER }}"))' > versions.json
VERSIONS=$(jq -r '.id' versions.json)
for VERSION in $(echo -n "$VERSIONS")
do
RELEASE=$(jq -r \
--arg VERSION "$VERSION" \
'select(.id | tostring | test($VERSION)) | .name' \
versions.json
)
echo "deleting release \"$RELEASE\" with RELEASE_ID=${VERSION}"
# https://github.com/cli/cli/issues/3937
echo -n | gh api \
--silent \
--method DELETE \
-H "Accept: application/vnd.github+json" \
"/repos/${{ github.repository }}/releases/$VERSION" \
--input -
done