From 620ddb31fd2f9b8fd15bfc159c0826c1b204114e Mon Sep 17 00:00:00 2001 From: Daniel Adam Date: Fri, 10 Nov 2023 16:39:46 +0100 Subject: [PATCH] Add retry to clean-up packages GitHub action --- .github/actions/cleanup-stale/action.yaml | 52 +++++++++++++++++++++++ .github/workflows/ghcr-cleanup.yaml | 46 +++++++++----------- 2 files changed, 71 insertions(+), 27 deletions(-) create mode 100644 .github/actions/cleanup-stale/action.yaml diff --git a/.github/actions/cleanup-stale/action.yaml b/.github/actions/cleanup-stale/action.yaml new file mode 100644 index 00000000..1455cc1d --- /dev/null +++ b/.github/actions/cleanup-stale/action.yaml @@ -0,0 +1,52 @@ +name: Delete stale images + +description: Delete stale images + +inputs: + delay: + description: Delay before running the action (in seconds) + required: false + default: "0" # seconds + token: + description: A Github PAT token to fetch repository and delete packages (needs delete:packages permissions). + required: true + +runs: + using: composite + steps: + - name: Delay # needed in case of retry to avoid triggering the rate limit of the Github API + if: ${{ inputs.delay != '0' }} + shell: bash + run: | + sleep ${{ inputs.delay }} + + - name: Delete older than a month vnext images + uses: snok/container-retention-policy@v2 + with: + image-names: device/* + cut-off: One month ago UTC + account-type: org + org-name: plgd-dev + filter-tags: vnext-* + skip-tags: main + token: ${{ inputs.token }} + + - name: Set up Docker Buildx for delete untagged images action + uses: docker/setup-buildx-action@v3 + + - name: Login to GitHub Container Registry for delete untagged images action + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ inputs.token }} + + - name: Delete untagged images with no dependency + uses: Chizkiyahu/delete-untagged-ghcr-action@v3 + with: + repository: ${{ github.repository }} + repository_owner: ${{ github.repository_owner }} + token: ${{ inputs.token }} + owner_type: org + untagged_only: true + except_untagged_multiplatform: true diff --git a/.github/workflows/ghcr-cleanup.yaml b/.github/workflows/ghcr-cleanup.yaml index e74d4a52..4e358b1a 100644 --- a/.github/workflows/ghcr-cleanup.yaml +++ b/.github/workflows/ghcr-cleanup.yaml @@ -1,42 +1,34 @@ name: Delete old ghcr images on: schedule: - - cron: "15 1 * * *" # every day at 1:15am + - cron: "0 1 * * *" # every day at 1:00am workflow_dispatch: jobs: ghcr-cleanup: if: ${{ github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' }} - name: Delete old vnext images + name: Delete stale or untagged images runs-on: ubuntu-latest steps: - - name: Delete older than a month vnext images - uses: snok/container-retention-policy@v2 + - name: Checkout local actions + uses: actions/checkout@v4 + - name: Delete stale or untagged images (1) + id: run-1 + continue-on-error: true + uses: ./.github/actions/cleanup-stale with: - image-names: device/* - cut-off: One month ago UTC - account-type: org - org-name: plgd-dev - filter-tags: vnext-* - skip-tags: main,latest token: ${{ secrets.GHCR_CLEANUP_PAT }} - - - name: Set up Docker Buildx for delete untagged images action - uses: docker/setup-buildx-action@v3 - - - name: Login to GitHub Container Registry for delete untagged images action - uses: docker/login-action@v3 + - name: Delete stale or untagged images (2) + id: run-2 + if: ${{ steps.run-1.outcome == 'failure' }} with: - registry: ghcr.io - username: ${{ github.actor }} - password: ${{ secrets.GHCR_CLEANUP_PAT }} - - - name: Delete untagged images with no dependency - uses: Chizkiyahu/delete-untagged-ghcr-action@v3 + token: ${{ secrets.GHCR_CLEANUP_PAT }} + delay: 60 + continue-on-error: true + uses: ./.github/actions/cleanup-stale + - name: Delete stale or untagged images (3) + if: ${{ steps.run-2.outcome == 'failure' }} + uses: ./.github/actions/cleanup-stale with: - repository: ${{ github.repository }} - repository_owner: ${{ github.repository_owner }} token: ${{ secrets.GHCR_CLEANUP_PAT }} - owner_type: org - untagged_only: true - except_untagged_multiplatform: true + delay: 60