-
Notifications
You must be signed in to change notification settings - Fork 183
199 lines (177 loc) · 7.25 KB
/
clean_up.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
on:
workflow_dispatch:
inputs:
delete_draft_releases:
type: boolean
description: Delete all draft releases on GitHub. This will interfere with any active publishing workflow.
required: false
default: false
delete_temp_branches:
type: boolean
description: Delete temporary branches created for publishing. This will interfere with any active publishing workflow.
required: false
default: false
schedule:
- cron: "*/5 * * * *"
pull_request_target:
types:
- closed
name: Clean up
jobs:
bot_branches:
name: Delete temporary bot/* branches
if: github.event_name == 'workflow_dispatch' && inputs.delete_temp_branches
runs-on: ubuntu-latest
steps:
- run: |
branches_to_delete=`gh api --paginate \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/${{ github.repository }}/branches \
--jq '.[] | select(.name | test("^bot/[0-9]+")).name'`
for branch in $branches_to_delete; do
echo "Deleting $branch."
gh api \
--method DELETE \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/nvidia/cuda-quantum/git/refs/heads/$branch
done
env:
GH_TOKEN: ${{ secrets.REPO_BOT_ACCESS_TOKEN || github.token }}
draft_releases:
name: Delete draft release
if: github.event_name == 'workflow_dispatch' && inputs.delete_draft_releases
runs-on: ubuntu-latest
steps:
- run: |
gh release list -L 100 -R ${{ github.repository }} > rels.txt
while read rel _; do
isDraft=`gh release view $rel -R ${{ github.repository }} --json isDraft --jq '.isDraft'`
isPrerelease=`gh release view $rel -R ${{ github.repository }} --json isPrerelease --jq '.isPrerelease'`
if $isDraft && $isPrerelease; then
echo "Deleting release $rel."
gh release delete $rel -R ${{ github.repository }} -y
else
echo "Skipping release $rel."
fi
done < rels.txt
env:
GH_TOKEN: ${{ github.token }}
ghcr_images:
name: Clean up GHCR images
runs-on: ubuntu-latest
strategy:
matrix:
image_name: [cuda-quantum, cuda-quantum-assets, cuda-quantum-dev, cuda-quantum-devdeps, open-mpi]
fail-fast: false
steps:
# We need to keep a good number of untagged manifests,
# since each tagged image contains/depends on untagged components
- name: Delete untagged images
uses: actions/delete-package-versions@v5
with:
package-name: ${{ matrix.image_name }}
package-type: 'container'
min-versions-to-keep: 200
delete-only-untagged-versions: 'true'
- name: Log in to the container registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Find matching signatures files
id: sig_files
run: |
gh api -X GET --paginate -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" \
/orgs/${{ github.repository_owner }}/packages/container/${{ matrix.image_name }}/versions >> packages.json
sig_tags=`cat packages.json | jq -r ".[] | select(.metadata.package_type==\"container\").metadata.container.tags[]" | (egrep -o '^sha256-\S+\.sig$' || true)`
nr_sigs=100 # limit how much we delete at a time to avoid exceeding the service rate limit
for sig in $sig_tags; do
if [ $nr_sigs -lt 1 ]; then continue; fi
matching_image=`cat packages.json | jq -r ".[] | select(.name==\"sha256:${sig:7:-4}\").id"`
if [ -z "$matching_image" ]; then
echo "Marking signature $sig for deletion."
delete+=", $sig"
nr_sigs=$(($nr_sigs-1))
fi
done
echo "tags_to_remove=${delete:2}" >> $GITHUB_OUTPUT
env:
GH_TOKEN: ${{ github.token }}
- name: Look up version numbers
id: packages
run: |
for tag in ${{ steps.sig_files.outputs.tags_to_remove }}; do
echo "Finding version id for tag ${tag%,}."
version_id=`cat packages.json | jq ".[] | select(.metadata.package_type==\"container\" and (.metadata.container.tags[] | contains(\"${tag%,}\")))" | jq '.id'`
if [ -n "$version_id" ]; then
echo "Marking version $version_id for deletion."
delete+=", $version_id"
fi
done
echo "[${delete:2}]"
echo "versions_to_remove=${delete:2}" >> $GITHUB_OUTPUT
env:
GH_TOKEN: ${{ github.token }}
- name: Delete matching signatures
if: steps.packages.outputs.versions_to_remove
uses: actions/delete-package-versions@v5
with:
package-name: ${{ matrix.image_name }}
package-type: 'container'
package-version-ids: '${{ steps.packages.outputs.versions_to_remove }}'
# We use environments to deploy to a public registry after PRs are merged.
# Since we use the same workflows during CI, a default environment that defines
# the necessary variables is used instead. Unfortunately, this automatically
# also creates an (unwanted) deployment, which we delete with this job.
# See also https://github.com/actions/runner/issues/2120
deployments:
name: Deployments
runs-on: ubuntu-latest
permissions:
deployments: write
steps:
- uses: actions/github-script@v7
with:
script: |
const deployments = await github.rest.repos.listDeployments({
owner: context.repo.owner,
repo: context.repo.repo,
environment: 'default'
});
await Promise.all(
deployments.data.map(async (deployment) => {
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: deployment.id,
state: 'inactive'
});
return github.rest.repos.deleteDeployment({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: deployment.id
});
})
);
pr_cleanup:
name: Clean up documentation previews
if: github.event_name == 'pull_request_target'
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ vars.preview_branch }}
- name: Delete preview folder
run: |
git config --global user.name "cuda-quantum-bot"
git config --global user.email "[email protected]"
git rm -r "pr-${{ github.event.pull_request.number }}" --ignore-unmatch
git commit --allow-empty -m "Cleaning up docs preview for PR #${{ github.event.pull_request.number }}."
git config pull.rebase true
git pull --no-edit && git push