Prune Untagged Images #352
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
--- | |
name: Prune Untagged Images | |
on: | |
schedule: | |
- cron: '22 1 * * *' # 22 past 1am daily | |
workflow_dispatch: | |
jobs: | |
prune_images: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/github-script@v6 | |
with: | |
github-token: ${{ github.token }} | |
# Based on https://github.com/orgs/community/discussions/26716#discussioncomment-3253034 | |
# Thanks to @jcansdale | |
script: | | |
const response = await github.request("GET /${{ env.OWNER }}/packages/container/${{ env.PACKAGE_NAME }}/versions", | |
{ per_page: ${{ env.PER_PAGE }} | |
}); | |
empty_tag_versions = response.data.filter(function(version) { | |
return version.metadata.container.tags.length == 0; | |
} | |
) | |
sort_by_created_date = function(a, b) { | |
date_a = new Date(a.created_at) | |
date_b = new Date(b.created_at) | |
// Oldest first | |
if ( date_a > date_b) { | |
return 1; | |
} else if ( date_b > date_a ) { | |
return -1; | |
} else { | |
return 0; | |
} | |
} | |
empty_tag_versions.sort(sort_by_created_date) | |
console.log(empty_tag_versions) | |
num_to_delete = empty_tag_versions.length - ${{ env.KEEP_MIN }} | |
console.log("Number of versions to delete: " + num_to_delete) | |
for (let i = 0; i < num_to_delete; i++){ | |
version = empty_tag_versions[i] | |
console.log("delete " + version.id) | |
const deleteResponse = await github.request("DELETE /${{ env.OWNER }}/packages/container/${{ env.PACKAGE_NAME }}/versions/" + version.id, { }); | |
console.log("status " + deleteResponse.status) | |
} | |
env: | |
OWNER: orgs/UtrechtUniversity | |
PACKAGE_NAME: SRC-test-workspace | |
KEEP_MIN: 10 | |
PER_PAGE: 100 |