Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Aug 16, 2024
1 parent 2c5b8ca commit 9b09c13
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 42 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/clear-cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 * * *'
Expand All @@ -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 }}
4 changes: 4 additions & 0 deletions dev-packages/clear-cache-gh-action/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ""
Expand Down
96 changes: 56 additions & 40 deletions dev-packages/clear-cache-gh-action/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -19,6 +20,7 @@ async function run() {
owner,
clearDevelop,
clearPending,
clearBranches,
workflowName,
});
}
Expand All @@ -27,9 +29,9 @@ async function run() {
* Clear caches.
*
* @param {ReturnType<import("@actions/github").getOctokit> } 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,
Expand All @@ -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!
Expand Down

0 comments on commit 9b09c13

Please sign in to comment.