From b6087b59eee5e68743e94671e3ae5637aeee5c7a Mon Sep 17 00:00:00 2001 From: Stuart Leeks Date: Mon, 6 Jun 2022 17:33:58 +0000 Subject: [PATCH] Add scheduled workflow to clean untagged images --- .github/workflows/clean_untagged.sh | 26 ++++++++++++++++++++ .github/workflows/untagged-image-cleanup.yml | 23 +++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100755 .github/workflows/clean_untagged.sh create mode 100644 .github/workflows/untagged-image-cleanup.yml diff --git a/.github/workflows/clean_untagged.sh b/.github/workflows/clean_untagged.sh new file mode 100755 index 00000000..0a703211 --- /dev/null +++ b/.github/workflows/clean_untagged.sh @@ -0,0 +1,26 @@ +#!/bin/bash +set -e + + +image_names=( + "devcontainer-build-run-devcontainer" + "devcontainer-build-run/tests/run-args" + "devcontainer-build-run/tests/build-args" + "devcontainer-build-run/tests/dockerfile-context" + "devcontainer-build-run/tests/feature-docker-from-docker" + "devcontainer-build-run/tests/docker-from-docker-non-root" + "devcontainer-build-run/tests/docker-from-docker-root" + "devcontainer-build-run/tests/skip-user-update" +) + +for image_name in ${image_names[@]}; +do + echo "Checking for untagged versions for $image_name" + escaped_image_name=$(echo ${image_name} | sed "s/\//%2f/g") + version_ids=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" -H "Accept: application/vnd.github.v3+json" "https://api.github.com/users/stuartleeks/packages/container/$escaped_image_name/versions?per_page=100" | jq -r ".[] | select(.metadata.container.tags | length ==0) | .id") + for version_id in ${version_ids[@]}; + do + echo -e "\tDeleting version '$version_id' for '$image_name:$tag' ..." + curl -s -X DELETE -H "Authorization: Bearer $GITHUB_TOKEN" -H "Accept: application/vnd.github.v3+json" "https://api.github.com/users/stuartleeks/packages/container/$escaped_image_name/versions/$version_id" + done +done diff --git a/.github/workflows/untagged-image-cleanup.yml b/.github/workflows/untagged-image-cleanup.yml new file mode 100644 index 00000000..1832c6cd --- /dev/null +++ b/.github/workflows/untagged-image-cleanup.yml @@ -0,0 +1,23 @@ +name: untagged_image_cleanup + +on: # yamllint disable-line rule:truthy + schedule: + - cron: "4 18 * * *" + workflow_dispatch: + +jobs: + + clean_images: + name: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + with: + persist-credentials: false + + - name: Clean untagged GitHub container images + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: + ./.github/workflows/clean_untagged.sh