diff --git a/.github/workflows/python_ci.yaml b/.github/workflows/python_ci.yaml index fe85939..8732ab5 100644 --- a/.github/workflows/python_ci.yaml +++ b/.github/workflows/python_ci.yaml @@ -94,3 +94,26 @@ jobs: uses: docker/build-push-action@4a13e500e55cf31b7a5d59a38ab2040ab0f42f56 with: push: false + + cleanup-ghcr-images: + runs-on: ubuntu-latest + if: github.ref == 'refs/heads/main' + permissions: + packages: write + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + - name: Cleanup old GHCR images + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + ghcr_images=$(gh api -H "Accept: application/vnd.github.v3+json" /orgs/gnosis/packages/container/mech-tool-deployment/versions | jq -r '.[] | select(.created_at < (now - 86400 | todate)) | .id') + for image_id in $ghcr_images; do + tags=$(gh api -H "Accept: application/vnd.github.v3+json" /orgs/gnosis/packages/container/mech-tool-deployment/versions/$image_id | jq -r '.metadata.container.tags[]') + if [[ ! " ${tags[@]} " =~ " main " ]]; then + echo "Deleting image with ID: $image_id" + gh api --silent --method DELETE "/orgs/gnosis/packages/container/mech-tool-deployment/versions/$image_id" + else + echo "Skipping image with ID: $image_id as it is tagged as main" + fi + done