-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: remove old versions during nightly db build
- Loading branch information
Showing
5 changed files
with
443 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
Oops, something went wrong.