From 634544c25759ef31213f42221ab9a4d5c7cab551 Mon Sep 17 00:00:00 2001 From: lystopad Date: Mon, 4 Nov 2024 14:24:05 +0100 Subject: [PATCH] Cleanup old snapshot docker images (main-xxxxxxx). Keep only latest 100 docker images with tag matching pattern main-XXXXXXX --- .../ci-cd-main-branch-docker-images.yml | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci-cd-main-branch-docker-images.yml b/.github/workflows/ci-cd-main-branch-docker-images.yml index bdfc1e6a883..b476d0db4c9 100644 --- a/.github/workflows/ci-cd-main-branch-docker-images.yml +++ b/.github/workflows/ci-cd-main-branch-docker-images.yml @@ -9,6 +9,7 @@ env: CHECKOUT_REF: "main" DOCKERHUB_REPOSITORY: "erigontech/erigon" LABEL_DESCRIPTION: "[docker image built on a last commit id from the main branch] Erigon is an implementation of Ethereum (execution layer with embeddable consensus layer), on the efficiency frontier. Archive Node by default." + KEEP_IMAGES: 100 on: push: @@ -127,7 +128,7 @@ jobs: --push \ --platform linux/amd64,linux/arm64 . - - name: export and print docker build tag + - name: export and print docker build tag, cleanup old docker images id: built_tag_export env: BUILD_VERSION: "main-${{ steps.getCommitId.outputs.short_commit_id }}" @@ -136,6 +137,36 @@ jobs: echo The following docker images have been published: echo "${{ env.DOCKERHUB_REPOSITORY }}:main-${{ env.BUILD_VERSION }}" echo "${{ env.DOCKERHUB_REPOSITORY }}:main-latest" + echo + echo "Cleanup old docker images matching pattern tag ~= main-XXXXXXX" + curl_cmd="curl -s -H \"Authorization: JWT ${{ secrets.ORG_DOCKERHUB_ERIGONTECH_TOKEN }}\" " + dockerhub_url='https://hub.docker.com/v2/namespaces/erigontech/repositories/erigon' + my_list () { + # First page: + next_page="$dockerhub_url/tags?page=1&page_size=100" + while [ "$next_page" != "null" ] + do + # Print tags and push dates for tags matching "main-": + $curl_cmd $next_page | jq -r '.results|.[]|.name + " " + .tag_last_pushed' | grep 'main-' + next_page=`$curl_cmd $next_page | jq '.next' | sed -e 's/^\"//' -e 's/\"$//'` + done + } + + my_list | tail -n+${{ env.KEEP_IMAGES }} | while read line; do + echo -n "Removing docker image/published - $line " + current_image=$(echo $line | sed -e 's/^\(main-.\{7\}\) .*/\1/') + output_code=$(curl --write-out %{http_code} --output curl-output.log \ + -s -X DELETE -H "Accept: application/json" \ + -H "Authorization: JWT ${{ secrets.ORG_DOCKERHUB_ERIGONTECH_TOKEN }}" \ + https://hub.docker.com/v2/repositories/erigontech/erigon/tags/${current_image} ) + if [ $output_code -ne 204 ]; then + echo "ERROR: failed to remove docker image erigon:${current_image}" + echo "ERROR: API response: $(cat curl-output.log)." + else + echo -n " - removed. " + fi + echo "Done." + done run-kurtosis-assertoor: needs: [define_matrix, Build] @@ -143,4 +174,4 @@ jobs: with: checkout_ref: ${{ github.sha }} os: ${{ needs.define_matrix.outputs.os }} - docker_build_tag: ${{ needs.Build.outputs.docker_build_tag }} \ No newline at end of file + docker_build_tag: ${{ needs.Build.outputs.docker_build_tag }}