From 5cce5d38b7d7d0fcca9a6ab2e33fe37bd47685ec Mon Sep 17 00:00:00 2001 From: thxCode Date: Wed, 10 Jul 2024 23:44:29 +0800 Subject: [PATCH] ci: clean cache [skip ci] Signed-off-by: thxCode --- .github/workflows/clean.yml | 62 +++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 .github/workflows/clean.yml diff --git a/.github/workflows/clean.yml b/.github/workflows/clean.yml new file mode 100644 index 0000000..b77c644 --- /dev/null +++ b/.github/workflows/clean.yml @@ -0,0 +1,62 @@ +name: clean + +permissions: + contents: write + pull-requests: read + actions: write + +defaults: + run: + shell: bash + +on: + workflow_dispatch: { } + +jobs: + clean: + timeout-minutes: 5 + runs-on: ubuntu-22.04 + steps: + - name: Remove Cache + uses: actions/github-script@v7 + continue-on-error: true + with: + # clean up caches, + # ref to https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#force-deleting-cache-entries, + # and https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#restrictions-for-accessing-a-cache. + script: | + const owner = context.repo.owner + const repo = context.repo.repo + var deleteCaches = new Array() + + // get candidate items. + const { data: cs } = await github.rest.actions.getActionsCacheList({ + owner: owner, + repo: repo, + }); + for (const c of cs.actions_caches) { + // clean closed pull request's caches. + if (c.ref.match(/^refs\/pull\/.*$/)) { + var prNum = c.ref.replace(/[^\d]/g, "") + const { data: pr } = await github.rest.pulls.get({ + owner: owner, + repo: repo, + pull_number: prNum, + }) + if (pr.state === 'closed') { + deleteCaches.push(c) + } + continue + } + deleteCaches.push(c) + } + + // delete + for (const c of deleteCaches) { + await github.rest.actions.deleteActionsCacheById({ + owner: owner, + repo: repo, + cache_id: c.id, + }) + console.log(`cleaned cache "${c.key}"`) + }