Skip to content

Clean

Clean #657

Workflow file for this run

name: Clean
permissions:
contents: write
pull-requests: read
actions: write
defaults:
run:
shell: bash
on:
schedule:
- cron: '0 */12 * * *'
workflow_dispatch: { }
jobs:
clean:
timeout-minutes: 5
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 1
persist-credentials: false
- name: Remove Cache
uses: actions/github-script@v6
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
}
// do not clean toolbox caches.
if (c.key.match(/^toolbox-.*$/)) {
continue
}
// clean push archived caches.
if (c.key.match(/^archive-.*$/)) {
deleteCaches.push(c)
continue
}
// clean stale built caches.
if (!c.key.match(/^setup-go-.*-${{ hashFiles('**/go.sum') }}$/)) {
deleteCaches.push(c)
continue
}
}
// 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}"`)
}
continue-on-error: true