From a60967004d5f6aa3f7ebdc37c0747547a9dfb7a1 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 16 Aug 2024 11:23:59 +0200 Subject: [PATCH 01/17] ci: Allow to clear some caches only --- .github/workflows/clear-cache.yml | 28 ++++- .github/workflows/external-contributors.yml | 1 - .../clear-cache-gh-action/.eslintrc.cjs | 14 +++ dev-packages/clear-cache-gh-action/action.yml | 21 ++++ dev-packages/clear-cache-gh-action/index.mjs | 105 ++++++++++++++++++ .../clear-cache-gh-action/package.json | 23 ++++ package.json | 1 + 7 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 dev-packages/clear-cache-gh-action/.eslintrc.cjs create mode 100644 dev-packages/clear-cache-gh-action/action.yml create mode 100644 dev-packages/clear-cache-gh-action/index.mjs create mode 100644 dev-packages/clear-cache-gh-action/package.json diff --git a/.github/workflows/clear-cache.yml b/.github/workflows/clear-cache.yml index 2946723fe6b8..16bc1c584701 100644 --- a/.github/workflows/clear-cache.yml +++ b/.github/workflows/clear-cache.yml @@ -1,11 +1,37 @@ name: "Action: Clear all GHA caches" on: workflow_dispatch: + inputs: + include_pending: + description: If caches of pending workflows should be deleted + type: boolean + default: false + include_develop: + description: If caches of the develop branch should be deleted + type: boolean + default: false + schedule: + # Run every day at midnight + - cron: '0 0 * * *' jobs: clear-caches: name: Delete all caches runs-on: ubuntu-20.04 steps: + - uses: actions/checkout@v4 + + - name: Set up Node + uses: actions/setup-node@v4 + with: + node-version-file: 'package.json' + + - name: Install dependencies + run: yarn install --frozen-lockfile + - name: Clear caches - uses: easimon/wipe-cache@v2 + uses: ./dev-packages/clear-cache-gh-action + with: + include_pending: ${{ inputs.include_pending }} + include_develop: ${{ inputs.include_develop }} + github_token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/external-contributors.yml b/.github/workflows/external-contributors.yml index 50acb2be8e73..e01a1a66a589 100644 --- a/.github/workflows/external-contributors.yml +++ b/.github/workflows/external-contributors.yml @@ -25,7 +25,6 @@ jobs: uses: actions/setup-node@v4 with: node-version-file: 'package.json' - cache: 'yarn' - name: Install dependencies run: yarn install --frozen-lockfile diff --git a/dev-packages/clear-cache-gh-action/.eslintrc.cjs b/dev-packages/clear-cache-gh-action/.eslintrc.cjs new file mode 100644 index 000000000000..8c67e0037908 --- /dev/null +++ b/dev-packages/clear-cache-gh-action/.eslintrc.cjs @@ -0,0 +1,14 @@ +module.exports = { + extends: ['../../.eslintrc.js'], + parserOptions: { + sourceType: 'module', + ecmaVersion: 'latest', + }, + + overrides: [ + { + files: ['*.mjs'], + extends: ['@sentry-internal/sdk/src/base'], + }, + ], +}; diff --git a/dev-packages/clear-cache-gh-action/action.yml b/dev-packages/clear-cache-gh-action/action.yml new file mode 100644 index 000000000000..8a973401e5e1 --- /dev/null +++ b/dev-packages/clear-cache-gh-action/action.yml @@ -0,0 +1,21 @@ +name: 'clear-cache-gh-action' +description: 'Clear caches of the GitHub repository.' +inputs: + github_token: + required: true + description: 'a github access token' + clear_develop: + required: false + default: "" + description: "If set, also clear caches from develop branch." + clear_pending: + required: false + default: "" + description: "If set, also clear caches from incomplete workflow runs." + workflow_name: + required: false + default: "CI: Build & Test" + description: The workflow to clear caches for. +runs: + using: 'node20' + main: 'index.mjs' diff --git a/dev-packages/clear-cache-gh-action/index.mjs b/dev-packages/clear-cache-gh-action/index.mjs new file mode 100644 index 000000000000..6159a520f3ef --- /dev/null +++ b/dev-packages/clear-cache-gh-action/index.mjs @@ -0,0 +1,105 @@ +import * as core from '@actions/core'; + +import { context, getOctokit } from '@actions/github'; + +async function run() { + const { getInput } = core; + + const { repo, owner } = context.repo; + + const githubToken = getInput('github_token'); + const clearDevelop = getInput('clear_develop', { type: 'boolean' }); + const clearPending = getInput('clear_pending', { type: 'boolean' }); + const workflowName = getInput('workflow_name'); + + const octokit = getOctokit(githubToken); + + await clearGithubCaches(octokit, { + repo, + owner, + clearDevelop, + clearPending, + workflowName, + }); +} + +/** + * Clear caches. + * + * @param {ReturnType } octokit + * @param {{repo: string, owner: string, clearDevelop: boolean, clearPending: boolean, workflowName: string}} options + */ +async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPending, workflowName }) { + for await (const response of octokit.paginate.iterator(octokit.rest.actions.getActionsCacheList, { + owner, + repo, + })) { + if (!response.data.length) { + break; + } + + for (const { id, ref } of response.data) { + core.info(`Checking cache ${id} for ${ref}...`); + // Do not clear develop caches if clearDevelop is false. + if (!clearDevelop && ref === 'refs/head/develop') { + core.info('> Keeping cache because it is on develop.'); + continue; + } + + // If clearPending is false, do not clear caches for pull requests that have pending checks. + const pull_number = /^refs\/pull\/(\d+)\/merge$/.exec(ref)?.[1]; + if (!clearPending && pull_number) { + const pr = await octokit.rest.pulls.get({ + owner, + repo, + pull_number, + }); + + const prBranch = pr.data.head.ref; + + // Check if PR has any pending workflows + const workflowRuns = await octokit.rest.actions.listWorkflowRunsForRepo({ + repo, + owner, + branch: prBranch, + }); + + // We only care about the relevant workflow + const relevantWorkflowRuns = workflowRuns.data.workflow_runs.filter(workflow => workflow.name === workflowName); + + const latestWorkflowRun = relevantWorkflowRuns[0]; + + core.info(`> Latest relevant workflow run: ${latestWorkflowRun.html_url}`); + + // No relevant workflow? Clear caches! + if (!latestWorkflowRun) { + core.info('> Clearing cache because no relevant workflow was found.'); + continue; + } + + // If the latest run was not successful, keep caches + // as either the run may be in progress, + // or failed - in which case we may want to re-run the workflow + if (latestWorkflowRun.conclusion !== 'success') { + core.info(`> Keeping cache because latest workflow is ${latestWorkflowRun.status}.`); + continue; + } + + core.info(`> Clearing cache because latest workflow run is ${latestWorkflowRun.status}.`); + } else { + core.info('Clearing cache because it is not a PR'); + } + + // DRY RUN FOR NOW! + core.info(`Would delete cache ${id} for ${ref}...`); + + /* await octokit.rest.actions.deleteActionsCacheById({ + owner, + repo, + cache_id: id, + }); */ + } + } +} + +run(); diff --git a/dev-packages/clear-cache-gh-action/package.json b/dev-packages/clear-cache-gh-action/package.json new file mode 100644 index 000000000000..492f4fc2b31e --- /dev/null +++ b/dev-packages/clear-cache-gh-action/package.json @@ -0,0 +1,23 @@ +{ + "name": "@sentry-internal/clear-cache-gh-action", + "description": "An internal Github Action to clear GitHub caches.", + "version": "8.26.0", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "private": true, + "main": "index.mjs", + "type": "module", + "scripts": { + "lint": "eslint . --format stylish", + "fix": "eslint . --format stylish --fix" + }, + "dependencies": { + "@actions/core": "1.10.1", + "@actions/github": "^5.0.0" + }, + "volta": { + "extends": "../../package.json" + } +} diff --git a/package.json b/package.json index beba7d79d284..4b9ad0383c02 100644 --- a/package.json +++ b/package.json @@ -91,6 +91,7 @@ "dev-packages/overhead-metrics", "dev-packages/test-utils", "dev-packages/size-limit-gh-action", + "dev-packages/clear-cache-gh-action", "dev-packages/external-contributor-gh-action", "dev-packages/rollup-utils" ], From 5243c6d316f1624d30963d4999a6938c6f6576f0 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 16 Aug 2024 11:31:57 +0200 Subject: [PATCH 02/17] changes --- .github/workflows/clear-cache.yml | 9 +- dev-packages/clear-cache-gh-action/action.yml | 4 + dev-packages/clear-cache-gh-action/index.mjs | 96 +++++++++++-------- 3 files changed, 67 insertions(+), 42 deletions(-) diff --git a/.github/workflows/clear-cache.yml b/.github/workflows/clear-cache.yml index 16bc1c584701..b799e0a56553 100644 --- a/.github/workflows/clear-cache.yml +++ b/.github/workflows/clear-cache.yml @@ -3,13 +3,17 @@ on: workflow_dispatch: inputs: include_pending: - description: If caches of pending workflows should be deleted + description: Delete caches of pending workflows type: boolean default: false include_develop: - description: If caches of the develop branch should be deleted + description: Delete caches on develop branch type: boolean default: false + include_branches: + description: Delete caches on non-develop branches + type: boolean + default: true schedule: # Run every day at midnight - cron: '0 0 * * *' @@ -34,4 +38,5 @@ jobs: with: include_pending: ${{ inputs.include_pending }} include_develop: ${{ inputs.include_develop }} + include_branches: ${{ inputs.include_branches }} github_token: ${{ secrets.GITHUB_TOKEN }} diff --git a/dev-packages/clear-cache-gh-action/action.yml b/dev-packages/clear-cache-gh-action/action.yml index 8a973401e5e1..bb3e9ede0670 100644 --- a/dev-packages/clear-cache-gh-action/action.yml +++ b/dev-packages/clear-cache-gh-action/action.yml @@ -8,6 +8,10 @@ inputs: required: false default: "" description: "If set, also clear caches from develop branch." + clear_branches: + required: false + default: "" + description: "If set, also clear caches from non-develop branches." clear_pending: required: false default: "" diff --git a/dev-packages/clear-cache-gh-action/index.mjs b/dev-packages/clear-cache-gh-action/index.mjs index 6159a520f3ef..a6269ef1b7a3 100644 --- a/dev-packages/clear-cache-gh-action/index.mjs +++ b/dev-packages/clear-cache-gh-action/index.mjs @@ -9,6 +9,7 @@ async function run() { const githubToken = getInput('github_token'); const clearDevelop = getInput('clear_develop', { type: 'boolean' }); + const clearBranches = getInput('clear_branches', { type: 'boolean', default: true }); const clearPending = getInput('clear_pending', { type: 'boolean' }); const workflowName = getInput('workflow_name'); @@ -19,6 +20,7 @@ async function run() { owner, clearDevelop, clearPending, + clearBranches, workflowName, }); } @@ -27,9 +29,9 @@ async function run() { * Clear caches. * * @param {ReturnType } octokit - * @param {{repo: string, owner: string, clearDevelop: boolean, clearPending: boolean, workflowName: string}} options + * @param {{repo: string, owner: string, clearDevelop: boolean, clearPending: boolean, clearBranches: boolean, workflowName: string}} options */ -async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPending, workflowName }) { +async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPending, clearBranches, workflowName }) { for await (const response of octokit.paginate.iterator(octokit.rest.actions.getActionsCacheList, { owner, repo, @@ -46,48 +48,62 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend continue; } - // If clearPending is false, do not clear caches for pull requests that have pending checks. + // There are two fundamental paths here: + // If the cache belongs to a PR, we need to check if the PR has any pending workflows. + // Else, we assume the cache belongs to a branch, where we do not check for pending workflows const pull_number = /^refs\/pull\/(\d+)\/merge$/.exec(ref)?.[1]; - if (!clearPending && pull_number) { - const pr = await octokit.rest.pulls.get({ - owner, - repo, - pull_number, - }); - - const prBranch = pr.data.head.ref; - - // Check if PR has any pending workflows - const workflowRuns = await octokit.rest.actions.listWorkflowRunsForRepo({ - repo, - owner, - branch: prBranch, - }); - - // We only care about the relevant workflow - const relevantWorkflowRuns = workflowRuns.data.workflow_runs.filter(workflow => workflow.name === workflowName); - - const latestWorkflowRun = relevantWorkflowRuns[0]; - - core.info(`> Latest relevant workflow run: ${latestWorkflowRun.html_url}`); - - // No relevant workflow? Clear caches! - if (!latestWorkflowRun) { - core.info('> Clearing cache because no relevant workflow was found.'); - continue; + if (pull_number) { + if (!clearPending) { + const pr = await octokit.rest.pulls.get({ + owner, + repo, + pull_number, + }); + + const prBranch = pr.data.head.ref; + + // Check if PR has any pending workflows + const workflowRuns = await octokit.rest.actions.listWorkflowRunsForRepo({ + repo, + owner, + branch: prBranch, + }); + + // We only care about the relevant workflow + const relevantWorkflowRuns = workflowRuns.data.workflow_runs.filter( + workflow => workflow.name === workflowName, + ); + + const latestWorkflowRun = relevantWorkflowRuns[0]; + + core.info(`> Latest relevant workflow run: ${latestWorkflowRun.html_url}`); + + // No relevant workflow? Clear caches! + if (!latestWorkflowRun) { + core.info('> Clearing cache because no relevant workflow was found.'); + continue; + } + + // If the latest run was not successful, keep caches + // as either the run may be in progress, + // or failed - in which case we may want to re-run the workflow + if (latestWorkflowRun.conclusion !== 'success') { + core.info(`> Keeping cache because latest workflow is ${latestWorkflowRun.status}.`); + continue; + } + + core.info(`> Clearing cache because latest workflow run is ${latestWorkflowRun.status}.`); + } else { + core.info('> Clearing cache of PR workflow run.'); } - - // If the latest run was not successful, keep caches - // as either the run may be in progress, - // or failed - in which case we may want to re-run the workflow - if (latestWorkflowRun.conclusion !== 'success') { - core.info(`> Keeping cache because latest workflow is ${latestWorkflowRun.status}.`); + } else { + // This means this is not a pull request, so check clearBranches + if (clearBranches) { + core.info('> Clearing cache because it is not a PR.'); + } else { + core.info('> Keeping cache for non-PR workflow run.'); continue; } - - core.info(`> Clearing cache because latest workflow run is ${latestWorkflowRun.status}.`); - } else { - core.info('Clearing cache because it is not a PR'); } // DRY RUN FOR NOW! From 4199082cb7ee00fb56fa18ebf0feb50749e352f9 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 16 Aug 2024 11:39:57 +0200 Subject: [PATCH 03/17] fixes --- .github/workflows/clear-cache.yml | 12 ++++++------ dev-packages/clear-cache-gh-action/index.mjs | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/clear-cache.yml b/.github/workflows/clear-cache.yml index b799e0a56553..888e0c9dbf37 100644 --- a/.github/workflows/clear-cache.yml +++ b/.github/workflows/clear-cache.yml @@ -2,15 +2,15 @@ name: "Action: Clear all GHA caches" on: workflow_dispatch: inputs: - include_pending: + clear_pending: description: Delete caches of pending workflows type: boolean default: false - include_develop: + clear_develop: description: Delete caches on develop branch type: boolean default: false - include_branches: + clear_branches: description: Delete caches on non-develop branches type: boolean default: true @@ -36,7 +36,7 @@ jobs: - name: Clear caches uses: ./dev-packages/clear-cache-gh-action with: - include_pending: ${{ inputs.include_pending }} - include_develop: ${{ inputs.include_develop }} - include_branches: ${{ inputs.include_branches }} + clear_pending: ${{ inputs.clear_pending }} + clear_develop: ${{ inputs.clear_develop }} + clear_branches: ${{ inputs.clear_branches }} github_token: ${{ secrets.GITHUB_TOKEN }} diff --git a/dev-packages/clear-cache-gh-action/index.mjs b/dev-packages/clear-cache-gh-action/index.mjs index a6269ef1b7a3..a09677dca5c1 100644 --- a/dev-packages/clear-cache-gh-action/index.mjs +++ b/dev-packages/clear-cache-gh-action/index.mjs @@ -43,7 +43,7 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend for (const { id, ref } of response.data) { core.info(`Checking cache ${id} for ${ref}...`); // Do not clear develop caches if clearDevelop is false. - if (!clearDevelop && ref === 'refs/head/develop') { + if (!clearDevelop && ref === 'refs/heads/develop') { core.info('> Keeping cache because it is on develop.'); continue; } @@ -92,7 +92,7 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend continue; } - core.info(`> Clearing cache because latest workflow run is ${latestWorkflowRun.status}.`); + core.info(`> Clearing cache because latest workflow run is ${latestWorkflowRun.conclusion}.`); } else { core.info('> Clearing cache of PR workflow run.'); } From 0d842e3325950505bc4430c953017063ba0c65dd Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 16 Aug 2024 11:44:42 +0200 Subject: [PATCH 04/17] clarify --- .github/workflows/clear-cache.yml | 6 +++--- dev-packages/clear-cache-gh-action/action.yml | 4 ++-- dev-packages/clear-cache-gh-action/index.mjs | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/clear-cache.yml b/.github/workflows/clear-cache.yml index 888e0c9dbf37..d7994d1f075a 100644 --- a/.github/workflows/clear-cache.yml +++ b/.github/workflows/clear-cache.yml @@ -2,8 +2,8 @@ name: "Action: Clear all GHA caches" on: workflow_dispatch: inputs: - clear_pending: - description: Delete caches of pending workflows + clear_pending_prs: + description: Delete caches of pending PR workflows type: boolean default: false clear_develop: @@ -36,7 +36,7 @@ jobs: - name: Clear caches uses: ./dev-packages/clear-cache-gh-action with: - clear_pending: ${{ inputs.clear_pending }} + clear_pending_prs: ${{ inputs.clear_pending_prs }} clear_develop: ${{ inputs.clear_develop }} clear_branches: ${{ inputs.clear_branches }} github_token: ${{ secrets.GITHUB_TOKEN }} diff --git a/dev-packages/clear-cache-gh-action/action.yml b/dev-packages/clear-cache-gh-action/action.yml index bb3e9ede0670..06493534b23e 100644 --- a/dev-packages/clear-cache-gh-action/action.yml +++ b/dev-packages/clear-cache-gh-action/action.yml @@ -12,10 +12,10 @@ inputs: required: false default: "" description: "If set, also clear caches from non-develop branches." - clear_pending: + clear_pending_prs: required: false default: "" - description: "If set, also clear caches from incomplete workflow runs." + description: "If set, also clear caches from pending PR workflow runs." workflow_name: required: false default: "CI: Build & Test" diff --git a/dev-packages/clear-cache-gh-action/index.mjs b/dev-packages/clear-cache-gh-action/index.mjs index a09677dca5c1..7ee0304e9b32 100644 --- a/dev-packages/clear-cache-gh-action/index.mjs +++ b/dev-packages/clear-cache-gh-action/index.mjs @@ -10,7 +10,7 @@ async function run() { const githubToken = getInput('github_token'); const clearDevelop = getInput('clear_develop', { type: 'boolean' }); const clearBranches = getInput('clear_branches', { type: 'boolean', default: true }); - const clearPending = getInput('clear_pending', { type: 'boolean' }); + const clearPending = getInput('clear_pending_prs', { type: 'boolean' }); const workflowName = getInput('workflow_name'); const octokit = getOctokit(githubToken); From 05880660b034c90e2564caf3e0d92ad496fa494c Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 16 Aug 2024 11:53:45 +0200 Subject: [PATCH 05/17] more fixes --- dev-packages/clear-cache-gh-action/index.mjs | 23 +++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/dev-packages/clear-cache-gh-action/index.mjs b/dev-packages/clear-cache-gh-action/index.mjs index 7ee0304e9b32..ebaa6112e8eb 100644 --- a/dev-packages/clear-cache-gh-action/index.mjs +++ b/dev-packages/clear-cache-gh-action/index.mjs @@ -8,9 +8,9 @@ async function run() { const { repo, owner } = context.repo; const githubToken = getInput('github_token'); - const clearDevelop = getInput('clear_develop', { type: 'boolean' }); - const clearBranches = getInput('clear_branches', { type: 'boolean', default: true }); - const clearPending = getInput('clear_pending_prs', { type: 'boolean' }); + const clearDevelop = inputToBoolean(getInput('clear_develop', { type: 'boolean' })); + const clearBranches = inputToBoolean(getInput('clear_branches', { type: 'boolean', default: true })); + const clearPending = inputToBoolean(getInput('clear_pending_prs', { type: 'boolean' })); const workflowName = getInput('workflow_name'); const octokit = getOctokit(githubToken); @@ -94,7 +94,8 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend core.info(`> Clearing cache because latest workflow run is ${latestWorkflowRun.conclusion}.`); } else { - core.info('> Clearing cache of PR workflow run.'); + core.info('> Keeping cache of every PR workflow run.'); + continue; } } else { // This means this is not a pull request, so check clearBranches @@ -107,7 +108,7 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend } // DRY RUN FOR NOW! - core.info(`Would delete cache ${id} for ${ref}...`); + core.info(`> >>Would delete cache ${id} for ${ref}...`); /* await octokit.rest.actions.deleteActionsCacheById({ owner, @@ -119,3 +120,15 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend } run(); + +function inputToBoolean(input) { + if (typeof input === 'boolean') { + return input; + } + + if (typeof input === 'string') { + return input === 'true'; + } + + return false; +} From b029c04a2693accdf2b50c543a2b3c304387ca2b Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 16 Aug 2024 12:00:30 +0200 Subject: [PATCH 06/17] actually clear the caches... --- .github/workflows/clear-cache.yml | 1 + dev-packages/clear-cache-gh-action/index.mjs | 7 +++---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/clear-cache.yml b/.github/workflows/clear-cache.yml index d7994d1f075a..fd2b60093486 100644 --- a/.github/workflows/clear-cache.yml +++ b/.github/workflows/clear-cache.yml @@ -30,6 +30,7 @@ jobs: with: node-version-file: 'package.json' + # TODO: Use cached version if possible (but never store cache) - name: Install dependencies run: yarn install --frozen-lockfile diff --git a/dev-packages/clear-cache-gh-action/index.mjs b/dev-packages/clear-cache-gh-action/index.mjs index ebaa6112e8eb..5e7cb46e4f65 100644 --- a/dev-packages/clear-cache-gh-action/index.mjs +++ b/dev-packages/clear-cache-gh-action/index.mjs @@ -107,14 +107,13 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend } } - // DRY RUN FOR NOW! - core.info(`> >>Would delete cache ${id} for ${ref}...`); + core.info(`> Clearing cache ${id}...`); - /* await octokit.rest.actions.deleteActionsCacheById({ + await octokit.rest.actions.deleteActionsCacheById({ owner, repo, cache_id: id, - }); */ + }); } } } From 54c5ac71ca8963fb94a01f4e94222bc7593fb908 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 16 Aug 2024 12:03:29 +0200 Subject: [PATCH 07/17] cache queries --- dev-packages/clear-cache-gh-action/index.mjs | 31 +++++++++++++------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/dev-packages/clear-cache-gh-action/index.mjs b/dev-packages/clear-cache-gh-action/index.mjs index 5e7cb46e4f65..d88d7f40feea 100644 --- a/dev-packages/clear-cache-gh-action/index.mjs +++ b/dev-packages/clear-cache-gh-action/index.mjs @@ -36,6 +36,11 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend owner, repo, })) { + /** @type {Map>} */ + const cachedPrs = new Map(); + /** @type {Map>} */ + const cachedWorkflows = new Map(); + if (!response.data.length) { break; } @@ -54,20 +59,26 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend const pull_number = /^refs\/pull\/(\d+)\/merge$/.exec(ref)?.[1]; if (pull_number) { if (!clearPending) { - const pr = await octokit.rest.pulls.get({ - owner, - repo, - pull_number, - }); + const pr = + cachedPrs.get(pull_number) || + (await octokit.rest.pulls.get({ + owner, + repo, + pull_number, + })); + cachedPrs.set(pull_number, pr); const prBranch = pr.data.head.ref; // Check if PR has any pending workflows - const workflowRuns = await octokit.rest.actions.listWorkflowRunsForRepo({ - repo, - owner, - branch: prBranch, - }); + const workflowRuns = + cachedWorkflows.get(prBranch) || + (await octokit.rest.actions.listWorkflowRunsForRepo({ + repo, + owner, + branch: prBranch, + })); + cachedWorkflows.set(prBranch, workflowRuns); // We only care about the relevant workflow const relevantWorkflowRuns = workflowRuns.data.workflow_runs.filter( From 04b2986002bbb94aafee931a460b848484e42c86 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 16 Aug 2024 12:05:26 +0200 Subject: [PATCH 08/17] debug it one more time... --- dev-packages/clear-cache-gh-action/index.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dev-packages/clear-cache-gh-action/index.mjs b/dev-packages/clear-cache-gh-action/index.mjs index d88d7f40feea..bc8f3b09b776 100644 --- a/dev-packages/clear-cache-gh-action/index.mjs +++ b/dev-packages/clear-cache-gh-action/index.mjs @@ -120,11 +120,11 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend core.info(`> Clearing cache ${id}...`); - await octokit.rest.actions.deleteActionsCacheById({ + /* await octokit.rest.actions.deleteActionsCacheById({ owner, repo, cache_id: id, - }); + }); */ } } } From cfa7b8cc00ba1fb1bc27a48d90454160304f9355 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 16 Aug 2024 13:34:58 +0200 Subject: [PATCH 09/17] small fix --- dev-packages/clear-cache-gh-action/index.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev-packages/clear-cache-gh-action/index.mjs b/dev-packages/clear-cache-gh-action/index.mjs index bc8f3b09b776..feff1acc9f86 100644 --- a/dev-packages/clear-cache-gh-action/index.mjs +++ b/dev-packages/clear-cache-gh-action/index.mjs @@ -99,7 +99,7 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend // as either the run may be in progress, // or failed - in which case we may want to re-run the workflow if (latestWorkflowRun.conclusion !== 'success') { - core.info(`> Keeping cache because latest workflow is ${latestWorkflowRun.status}.`); + core.info(`> Keeping cache because latest workflow is ${latestWorkflowRun.conclusion}.`); continue; } From 5b1e6e29b9b61ce94b81d6ed779d6f018342c6e2 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 16 Aug 2024 13:36:17 +0200 Subject: [PATCH 10/17] actually clear it --- dev-packages/clear-cache-gh-action/index.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dev-packages/clear-cache-gh-action/index.mjs b/dev-packages/clear-cache-gh-action/index.mjs index feff1acc9f86..7559426521dc 100644 --- a/dev-packages/clear-cache-gh-action/index.mjs +++ b/dev-packages/clear-cache-gh-action/index.mjs @@ -120,11 +120,11 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend core.info(`> Clearing cache ${id}...`); - /* await octokit.rest.actions.deleteActionsCacheById({ + await octokit.rest.actions.deleteActionsCacheById({ owner, repo, cache_id: id, - }); */ + }); } } } From d7df5dcff5e070e6b143a19b4de003f113073129 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 16 Aug 2024 13:43:21 +0200 Subject: [PATCH 11/17] better document --- dev-packages/clear-cache-gh-action/index.mjs | 113 ++++++++++--------- 1 file changed, 57 insertions(+), 56 deletions(-) diff --git a/dev-packages/clear-cache-gh-action/index.mjs b/dev-packages/clear-cache-gh-action/index.mjs index 7559426521dc..94a7904e24c7 100644 --- a/dev-packages/clear-cache-gh-action/index.mjs +++ b/dev-packages/clear-cache-gh-action/index.mjs @@ -56,66 +56,67 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend // There are two fundamental paths here: // If the cache belongs to a PR, we need to check if the PR has any pending workflows. // Else, we assume the cache belongs to a branch, where we do not check for pending workflows - const pull_number = /^refs\/pull\/(\d+)\/merge$/.exec(ref)?.[1]; - if (pull_number) { - if (!clearPending) { - const pr = - cachedPrs.get(pull_number) || - (await octokit.rest.pulls.get({ - owner, - repo, - pull_number, - })); - cachedPrs.set(pull_number, pr); - - const prBranch = pr.data.head.ref; - - // Check if PR has any pending workflows - const workflowRuns = - cachedWorkflows.get(prBranch) || - (await octokit.rest.actions.listWorkflowRunsForRepo({ - repo, - owner, - branch: prBranch, - })); - cachedWorkflows.set(prBranch, workflowRuns); - - // We only care about the relevant workflow - const relevantWorkflowRuns = workflowRuns.data.workflow_runs.filter( - workflow => workflow.name === workflowName, - ); - - const latestWorkflowRun = relevantWorkflowRuns[0]; - - core.info(`> Latest relevant workflow run: ${latestWorkflowRun.html_url}`); - - // No relevant workflow? Clear caches! - if (!latestWorkflowRun) { - core.info('> Clearing cache because no relevant workflow was found.'); - continue; - } - - // If the latest run was not successful, keep caches - // as either the run may be in progress, - // or failed - in which case we may want to re-run the workflow - if (latestWorkflowRun.conclusion !== 'success') { - core.info(`> Keeping cache because latest workflow is ${latestWorkflowRun.conclusion}.`); - continue; - } - - core.info(`> Clearing cache because latest workflow run is ${latestWorkflowRun.conclusion}.`); - } else { - core.info('> Keeping cache of every PR workflow run.'); + const pullNumber = /^refs\/pull\/(\d+)\/merge$/.exec(ref)?.[1]; + const isPr = !!pullNumber; + + // Case 1: This is a PR, and we do not want to clear pending PRs + // In this case, we need to fetch all PRs and workflow runs to check them + if (isPr && !clearPending) { + const pr = + cachedPrs.get(pullNumber) || + (await octokit.rest.pulls.get({ + owner, + repo, + pull_number: pullNumber, + })); + cachedPrs.set(pullNumber, pr); + + const prBranch = pr.data.head.ref; + + // Check if PR has any pending workflows + const workflowRuns = + cachedWorkflows.get(prBranch) || + (await octokit.rest.actions.listWorkflowRunsForRepo({ + repo, + owner, + branch: prBranch, + })); + cachedWorkflows.set(prBranch, workflowRuns); + + // We only care about the relevant workflow + const relevantWorkflowRuns = workflowRuns.data.workflow_runs.filter(workflow => workflow.name === workflowName); + + const latestWorkflowRun = relevantWorkflowRuns[0]; + + core.info(`> Latest relevant workflow run: ${latestWorkflowRun.html_url}`); + + // No relevant workflow? Clear caches! + if (!latestWorkflowRun) { + core.info('> Clearing cache because no relevant workflow was found.'); continue; } - } else { - // This means this is not a pull request, so check clearBranches - if (clearBranches) { - core.info('> Clearing cache because it is not a PR.'); - } else { - core.info('> Keeping cache for non-PR workflow run.'); + + // If the latest run was not successful, keep caches + // as either the run may be in progress, + // or failed - in which case we may want to re-run the workflow + if (latestWorkflowRun.conclusion !== 'success') { + core.info(`> Keeping cache because latest workflow is ${latestWorkflowRun.conclusion}.`); continue; } + + core.info(`> Clearing cache because latest workflow run is ${latestWorkflowRun.conclusion}.`); + } else if (isPr) { + // Case 2: This is a PR, but we do not want to clear pending PRs + // In this case, this cache should never be cleared + core.info('> Keeping cache of every PR workflow run.'); + continue; + } else if (clearBranches) { + // Case 3: This is not a PR, and we want to clean branches + core.info('> Clearing cache because it is not a PR.'); + } else { + // Case 4: This is not a PR, and we do not want to clean branches + core.info('> Keeping cache for non-PR workflow run.'); + continue; } core.info(`> Clearing cache ${id}...`); From 8295beb701f779bc554db756a838f90770ee37d0 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 16 Aug 2024 13:43:59 +0200 Subject: [PATCH 12/17] better label --- .github/workflows/clear-cache.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/clear-cache.yml b/.github/workflows/clear-cache.yml index fd2b60093486..5c327553e3b8 100644 --- a/.github/workflows/clear-cache.yml +++ b/.github/workflows/clear-cache.yml @@ -34,7 +34,7 @@ jobs: - name: Install dependencies run: yarn install --frozen-lockfile - - name: Clear caches + - name: Delete GHA caches uses: ./dev-packages/clear-cache-gh-action with: clear_pending_prs: ${{ inputs.clear_pending_prs }} From cbf78ff2d4faf12bab3e8b3b64e947538e534337 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 16 Aug 2024 13:48:29 +0200 Subject: [PATCH 13/17] show summary --- dev-packages/clear-cache-gh-action/index.mjs | 29 ++++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/dev-packages/clear-cache-gh-action/index.mjs b/dev-packages/clear-cache-gh-action/index.mjs index 94a7904e24c7..67f2bee89d25 100644 --- a/dev-packages/clear-cache-gh-action/index.mjs +++ b/dev-packages/clear-cache-gh-action/index.mjs @@ -41,15 +41,23 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend /** @type {Map>} */ const cachedWorkflows = new Map(); + let deletedCaches = 0; + let remainingCaches = 0; + + let deletedSize = 0; + let remainingSize = 0; + if (!response.data.length) { break; } - for (const { id, ref } of response.data) { + for (const { id, ref, size_in_bytes } of response.data) { core.info(`Checking cache ${id} for ${ref}...`); // Do not clear develop caches if clearDevelop is false. if (!clearDevelop && ref === 'refs/heads/develop') { core.info('> Keeping cache because it is on develop.'); + remainingCaches++; + remainingSize += size_in_bytes; continue; } @@ -109,6 +117,8 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend // Case 2: This is a PR, but we do not want to clear pending PRs // In this case, this cache should never be cleared core.info('> Keeping cache of every PR workflow run.'); + remainingCaches++; + remainingSize += size_in_bytes; continue; } else if (clearBranches) { // Case 3: This is not a PR, and we want to clean branches @@ -116,17 +126,30 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend } else { // Case 4: This is not a PR, and we do not want to clean branches core.info('> Keeping cache for non-PR workflow run.'); + remainingCaches++; + remainingSize += size_in_bytes; continue; } core.info(`> Clearing cache ${id}...`); - await octokit.rest.actions.deleteActionsCacheById({ + deletedCaches++; + deletedSize += size_in_bytes; + + /* await octokit.rest.actions.deleteActionsCacheById({ owner, repo, cache_id: id, - }); + }); */ } + + const format = new Intl.NumberFormat('en-US', { + style: 'decimal', + }); + + core.info('Summary:'); + core.info(`Deleted ${deletedCaches} caches, freeing up ~${format.format(deletedSize / 1000 / 1000)} mb.`); + core.info(`Remaining ${remainingCaches} caches, using ~${format.format(remainingSize / 1000 / 1000)} mb.`); } } From 2754010d7c9811e368e84e4e920b57ad616b4307 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 16 Aug 2024 14:32:04 +0200 Subject: [PATCH 14/17] clean up and fix logs --- dev-packages/clear-cache-gh-action/index.mjs | 44 ++++++++++++-------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/dev-packages/clear-cache-gh-action/index.mjs b/dev-packages/clear-cache-gh-action/index.mjs index 67f2bee89d25..f3fb994bb6d8 100644 --- a/dev-packages/clear-cache-gh-action/index.mjs +++ b/dev-packages/clear-cache-gh-action/index.mjs @@ -51,14 +51,16 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend break; } - for (const { id, ref, size_in_bytes } of response.data) { - core.info(`Checking cache ${id} for ${ref}...`); + /** + * Clear caches. + * + * @param {{ref: string}} options + */ + const shouldClearCache = async ({ ref }) => { // Do not clear develop caches if clearDevelop is false. if (!clearDevelop && ref === 'refs/heads/develop') { core.info('> Keeping cache because it is on develop.'); - remainingCaches++; - remainingSize += size_in_bytes; - continue; + return false; } // There are two fundamental paths here: @@ -101,7 +103,7 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend // No relevant workflow? Clear caches! if (!latestWorkflowRun) { core.info('> Clearing cache because no relevant workflow was found.'); - continue; + return true; } // If the latest run was not successful, keep caches @@ -109,7 +111,7 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend // or failed - in which case we may want to re-run the workflow if (latestWorkflowRun.conclusion !== 'success') { core.info(`> Keeping cache because latest workflow is ${latestWorkflowRun.conclusion}.`); - continue; + return false; } core.info(`> Clearing cache because latest workflow run is ${latestWorkflowRun.conclusion}.`); @@ -117,30 +119,38 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend // Case 2: This is a PR, but we do not want to clear pending PRs // In this case, this cache should never be cleared core.info('> Keeping cache of every PR workflow run.'); - remainingCaches++; - remainingSize += size_in_bytes; - continue; + return false; } else if (clearBranches) { // Case 3: This is not a PR, and we want to clean branches core.info('> Clearing cache because it is not a PR.'); + return true; } else { // Case 4: This is not a PR, and we do not want to clean branches core.info('> Keeping cache for non-PR workflow run.'); - remainingCaches++; - remainingSize += size_in_bytes; - continue; + return false; } + }; - core.info(`> Clearing cache ${id}...`); + for (const { id, ref, size_in_bytes } of response.data) { + core.info(`Checking cache ${id} for ${ref}...`); + + const shouldDelete = await shouldClearCache({ ref }); + + if (shouldDelete) { + core.info(`> Clearing cache ${id}...`); - deletedCaches++; - deletedSize += size_in_bytes; + deletedCaches++; + deletedSize += size_in_bytes; - /* await octokit.rest.actions.deleteActionsCacheById({ + /* await octokit.rest.actions.deleteActionsCacheById({ owner, repo, cache_id: id, }); */ + } else { + remainingCaches++; + remainingSize += size_in_bytes; + } } const format = new Intl.NumberFormat('en-US', { From a32153f517b736967f6f2248047d3bcdf04b226e Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 16 Aug 2024 14:38:27 +0200 Subject: [PATCH 15/17] fix indentation --- dev-packages/clear-cache-gh-action/index.mjs | 196 +++++++++---------- 1 file changed, 98 insertions(+), 98 deletions(-) diff --git a/dev-packages/clear-cache-gh-action/index.mjs b/dev-packages/clear-cache-gh-action/index.mjs index f3fb994bb6d8..76435a7a0581 100644 --- a/dev-packages/clear-cache-gh-action/index.mjs +++ b/dev-packages/clear-cache-gh-action/index.mjs @@ -32,105 +32,105 @@ async function run() { * @param {{repo: string, owner: string, clearDevelop: boolean, clearPending: boolean, clearBranches: boolean, workflowName: string}} options */ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPending, clearBranches, workflowName }) { + let deletedCaches = 0; + let remainingCaches = 0; + + let deletedSize = 0; + let remainingSize = 0; + + /** @type {Map>} */ + const cachedPrs = new Map(); + /** @type {Map>} */ + const cachedWorkflows = new Map(); + + /** + * Clear caches. + * + * @param {{ref: string}} options + */ + const shouldClearCache = async ({ ref }) => { + // Do not clear develop caches if clearDevelop is false. + if (!clearDevelop && ref === 'refs/heads/develop') { + core.info('> Keeping cache because it is on develop.'); + return false; + } + + // There are two fundamental paths here: + // If the cache belongs to a PR, we need to check if the PR has any pending workflows. + // Else, we assume the cache belongs to a branch, where we do not check for pending workflows + const pullNumber = /^refs\/pull\/(\d+)\/merge$/.exec(ref)?.[1]; + const isPr = !!pullNumber; + + // Case 1: This is a PR, and we do not want to clear pending PRs + // In this case, we need to fetch all PRs and workflow runs to check them + if (isPr && !clearPending) { + const pr = + cachedPrs.get(pullNumber) || + (await octokit.rest.pulls.get({ + owner, + repo, + pull_number: pullNumber, + })); + cachedPrs.set(pullNumber, pr); + + const prBranch = pr.data.head.ref; + + // Check if PR has any pending workflows + const workflowRuns = + cachedWorkflows.get(prBranch) || + (await octokit.rest.actions.listWorkflowRunsForRepo({ + repo, + owner, + branch: prBranch, + })); + cachedWorkflows.set(prBranch, workflowRuns); + + // We only care about the relevant workflow + const relevantWorkflowRuns = workflowRuns.data.workflow_runs.filter(workflow => workflow.name === workflowName); + + const latestWorkflowRun = relevantWorkflowRuns[0]; + + core.info(`> Latest relevant workflow run: ${latestWorkflowRun.html_url}`); + + // No relevant workflow? Clear caches! + if (!latestWorkflowRun) { + core.info('> Clearing cache because no relevant workflow was found.'); + return true; + } + + // If the latest run was not successful, keep caches + // as either the run may be in progress, + // or failed - in which case we may want to re-run the workflow + if (latestWorkflowRun.conclusion !== 'success') { + core.info(`> Keeping cache because latest workflow is ${latestWorkflowRun.conclusion}.`); + return false; + } + + core.info(`> Clearing cache because latest workflow run is ${latestWorkflowRun.conclusion}.`); + } else if (isPr) { + // Case 2: This is a PR, but we do not want to clear pending PRs + // In this case, this cache should never be cleared + core.info('> Keeping cache of every PR workflow run.'); + return false; + } else if (clearBranches) { + // Case 3: This is not a PR, and we want to clean branches + core.info('> Clearing cache because it is not a PR.'); + return true; + } else { + // Case 4: This is not a PR, and we do not want to clean branches + core.info('> Keeping cache for non-PR workflow run.'); + return false; + } + }; + for await (const response of octokit.paginate.iterator(octokit.rest.actions.getActionsCacheList, { owner, repo, })) { - /** @type {Map>} */ - const cachedPrs = new Map(); - /** @type {Map>} */ - const cachedWorkflows = new Map(); - - let deletedCaches = 0; - let remainingCaches = 0; - - let deletedSize = 0; - let remainingSize = 0; - if (!response.data.length) { break; } - /** - * Clear caches. - * - * @param {{ref: string}} options - */ - const shouldClearCache = async ({ ref }) => { - // Do not clear develop caches if clearDevelop is false. - if (!clearDevelop && ref === 'refs/heads/develop') { - core.info('> Keeping cache because it is on develop.'); - return false; - } - - // There are two fundamental paths here: - // If the cache belongs to a PR, we need to check if the PR has any pending workflows. - // Else, we assume the cache belongs to a branch, where we do not check for pending workflows - const pullNumber = /^refs\/pull\/(\d+)\/merge$/.exec(ref)?.[1]; - const isPr = !!pullNumber; - - // Case 1: This is a PR, and we do not want to clear pending PRs - // In this case, we need to fetch all PRs and workflow runs to check them - if (isPr && !clearPending) { - const pr = - cachedPrs.get(pullNumber) || - (await octokit.rest.pulls.get({ - owner, - repo, - pull_number: pullNumber, - })); - cachedPrs.set(pullNumber, pr); - - const prBranch = pr.data.head.ref; - - // Check if PR has any pending workflows - const workflowRuns = - cachedWorkflows.get(prBranch) || - (await octokit.rest.actions.listWorkflowRunsForRepo({ - repo, - owner, - branch: prBranch, - })); - cachedWorkflows.set(prBranch, workflowRuns); - - // We only care about the relevant workflow - const relevantWorkflowRuns = workflowRuns.data.workflow_runs.filter(workflow => workflow.name === workflowName); - - const latestWorkflowRun = relevantWorkflowRuns[0]; - - core.info(`> Latest relevant workflow run: ${latestWorkflowRun.html_url}`); - - // No relevant workflow? Clear caches! - if (!latestWorkflowRun) { - core.info('> Clearing cache because no relevant workflow was found.'); - return true; - } - - // If the latest run was not successful, keep caches - // as either the run may be in progress, - // or failed - in which case we may want to re-run the workflow - if (latestWorkflowRun.conclusion !== 'success') { - core.info(`> Keeping cache because latest workflow is ${latestWorkflowRun.conclusion}.`); - return false; - } - - core.info(`> Clearing cache because latest workflow run is ${latestWorkflowRun.conclusion}.`); - } else if (isPr) { - // Case 2: This is a PR, but we do not want to clear pending PRs - // In this case, this cache should never be cleared - core.info('> Keeping cache of every PR workflow run.'); - return false; - } else if (clearBranches) { - // Case 3: This is not a PR, and we want to clean branches - core.info('> Clearing cache because it is not a PR.'); - return true; - } else { - // Case 4: This is not a PR, and we do not want to clean branches - core.info('> Keeping cache for non-PR workflow run.'); - return false; - } - }; - for (const { id, ref, size_in_bytes } of response.data) { core.info(`Checking cache ${id} for ${ref}...`); @@ -152,15 +152,15 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend remainingSize += size_in_bytes; } } + } - const format = new Intl.NumberFormat('en-US', { - style: 'decimal', - }); + const format = new Intl.NumberFormat('en-US', { + style: 'decimal', + }); - core.info('Summary:'); - core.info(`Deleted ${deletedCaches} caches, freeing up ~${format.format(deletedSize / 1000 / 1000)} mb.`); - core.info(`Remaining ${remainingCaches} caches, using ~${format.format(remainingSize / 1000 / 1000)} mb.`); - } + core.info('Summary:'); + core.info(`Deleted ${deletedCaches} caches, freeing up ~${format.format(deletedSize / 1000 / 1000)} mb.`); + core.info(`Remaining ${remainingCaches} caches, using ~${format.format(remainingSize / 1000 / 1000)} mb.`); } run(); From f79e72b4d3f5beb36a5feb574289ae401776e3cf Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 16 Aug 2024 14:46:36 +0200 Subject: [PATCH 16/17] actually delete --- dev-packages/clear-cache-gh-action/index.mjs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dev-packages/clear-cache-gh-action/index.mjs b/dev-packages/clear-cache-gh-action/index.mjs index 76435a7a0581..9e37d6ee738d 100644 --- a/dev-packages/clear-cache-gh-action/index.mjs +++ b/dev-packages/clear-cache-gh-action/index.mjs @@ -142,11 +142,11 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend deletedCaches++; deletedSize += size_in_bytes; - /* await octokit.rest.actions.deleteActionsCacheById({ - owner, - repo, - cache_id: id, - }); */ + await octokit.rest.actions.deleteActionsCacheById({ + owner, + repo, + cache_id: id, + }); } else { remainingCaches++; remainingSize += size_in_bytes; From 1a9a170f6d1cc70e0adcdab7567047316952138b Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 16 Aug 2024 14:48:13 +0200 Subject: [PATCH 17/17] more cleanup --- dev-packages/clear-cache-gh-action/index.mjs | 27 ++++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/dev-packages/clear-cache-gh-action/index.mjs b/dev-packages/clear-cache-gh-action/index.mjs index 9e37d6ee738d..b1cb75c5a5c0 100644 --- a/dev-packages/clear-cache-gh-action/index.mjs +++ b/dev-packages/clear-cache-gh-action/index.mjs @@ -107,20 +107,25 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend } core.info(`> Clearing cache because latest workflow run is ${latestWorkflowRun.conclusion}.`); - } else if (isPr) { - // Case 2: This is a PR, but we do not want to clear pending PRs - // In this case, this cache should never be cleared - core.info('> Keeping cache of every PR workflow run.'); - return false; - } else if (clearBranches) { - // Case 3: This is not a PR, and we want to clean branches + return true; + } + + // Case 2: This is a PR, but we do want to clear pending PRs + // In this case, this cache should always be cleared + if (isPr) { + core.info('> Clearing cache of every PR workflow run.'); + return true; + } + + // Case 3: This is not a PR, and we want to clean branches + if (clearBranches) { core.info('> Clearing cache because it is not a PR.'); return true; - } else { - // Case 4: This is not a PR, and we do not want to clean branches - core.info('> Keeping cache for non-PR workflow run.'); - return false; } + + // Case 4: This is not a PR, and we do not want to clean branches + core.info('> Keeping cache for non-PR workflow run.'); + return false; }; for await (const response of octokit.paginate.iterator(octokit.rest.actions.getActionsCacheList, {