Skip to content

Commit

Permalink
ci: remove old versions during nightly db build
Browse files Browse the repository at this point in the history
  • Loading branch information
NGPixel committed Feb 8, 2023
1 parent c5562fb commit d73e5d7
Show file tree
Hide file tree
Showing 5 changed files with 443 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/dev-db-nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,21 @@ jobs:
push: true
tags: ghcr.io/ietf-tools/datatracker-db-pg:latest,ghcr.io/ietf-tools/datatracker-db-pg:nightly-${{ steps.date.outputs.date }}
provenance: false

cleanup:
name: Remove Old Images
runs-on: ubuntu-latest
if: ${{ always() && !failure() }}
needs: [build]
permissions:
packages: write
steps:
- uses: actions/checkout@v3

- name: Delete Old Versions
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
cd dev/del-old-packages
npm install
node index
15 changes: 15 additions & 0 deletions dev/del-old-packages/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Tool to delete old versions in GitHub Packages container registry

This tool will fetch all versions for packages `datatracker-db` and `datatracker-db-pg` and delete all versions that are not latest and older than 7 days.

### Requirements

- Node 18.x or later
- Must provide a valid token in ENV variable `GITHUB_TOKEN` with read and delete packages permissions.

### Usage

```sh
npm install
node index
```
54 changes: 54 additions & 0 deletions dev/del-old-packages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Octokit } from '@octokit/core'
import { setTimeout } from 'node:timers/promises'
import { DateTime } from 'luxon'

const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN
})

const oldestDate = DateTime.utc().minus({ days: 7 })

for (const pkgName of ['datatracker-db', 'datatracker-db-pg']) {
let hasMore = true
let currentPage = 1

while (hasMore) {
try {
console.info(`Fetching page ${currentPage}...`)
const versions = await octokit.request('GET /orgs/{org}/packages/{package_type}/{package_name}/versions{?page,per_page,state}', {
package_type: 'container',
package_name: pkgName,
org: 'ietf-tools',
page: currentPage,
per_page: 100
})
if (versions?.data?.length > 0) {
for (const ver of versions?.data) {
const verDate = DateTime.fromISO(ver.created_at)
if (ver?.metadata?.container?.tags?.includes('latest') || ver?.metadata?.container?.tags?.includes('latest-arm64') || ver?.metadata?.container?.tags?.includes('latest-x64')) {
console.info(`Latest package (${ver.id})... Skipping...`)
} else if (verDate > oldestDate) {
console.info(`Recent package (${ver.id}, ${verDate.toRelative()})... Skipping...`)
} else {
console.info(`Deleting package version ${ver.id}...`)
await octokit.request('DELETE /orgs/{org}/packages/{package_type}/{package_name}/versions/{package_version_id}', {
package_type: 'container',
package_name: pkgName,
org: 'ietf-tools',
package_version_id: ver.id
})
await setTimeout(250)
}
}
currentPage++
hasMore = true
} else {
hasMore = false
console.info('No more versions for this package.')
}
} catch (err) {
console.error(err)
hasMore = false
}
}
}
Loading

0 comments on commit d73e5d7

Please sign in to comment.