-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add cache warm and cache evict workflows #2944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
032f133
Add cache warm and cache evict workflows
cprecioso c422c26
Add concurrency groups
cprecioso 25c0660
Merge branch 'main' into cprecioso/2919-ensure-warm-cache-for-ci
cprecioso 16793f4
WIP
cprecioso e1bba2a
Remove dry-run
cprecioso 71b190f
Remove existing schedule
cprecioso 78aedab
Comments
cprecioso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: Evict caches for merged PRs | ||
|
||
# Caches for merged PRs stay around until we bump against our cache limit. | ||
# They are also not shareable between branches. Therefore, once a PR is | ||
# merged, the cache for that branch is immediately not needed anymore, and we | ||
# can remove it. If we remove these caches, we can save space and be more sure | ||
# that the remaining caches are actually useful, and not at danger of being | ||
# evicted. | ||
|
||
on: | ||
workflow_dispatch: | ||
push: | ||
branches: | ||
- main | ||
|
||
concurrency: | ||
group: "cache-evict" | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
evict-cache: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Evict caches | ||
shell: bash | ||
env: | ||
GITHUB_TOKEN: ${{ github.token }} | ||
run: node ./scripts/cache-evict.mjs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: Keeps build cache warm | ||
|
||
on: | ||
workflow_dispatch: | ||
schedule: | ||
# GitHub evicts unused caches after 7 days, so we run this every 6 days | ||
- cron: "0 0 */6 * *" | ||
|
||
concurrency: | ||
group: "cache-warm" | ||
cancel-in-progress: false | ||
|
||
jobs: | ||
warm-cache-build: | ||
uses: ./.github/workflows/waspc-build.yaml | ||
|
||
warm-cache-ci: | ||
uses: ./.github/workflows/waspc-ci.yaml | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// @ts-check | ||
|
||
// This script is intended to be run in a GitHub Actions workflow to delete | ||
// orphaned caches that are no longer associated with any remote refs. | ||
// Called from `.github/workflows/cache-evict.yml`. | ||
|
||
import assert from "node:assert/strict"; | ||
import { execFileSync } from "node:child_process"; | ||
|
||
const { repository } = assertGithubActionsEnv(); | ||
deleteOrphanedCaches(repository); | ||
|
||
function deleteOrphanedCaches(/** @type {string} */ githubRepository) { | ||
const ghCaches = listGitHubCaches(); | ||
const remoteRefs = listRemoteRefs(githubRepository); | ||
|
||
const cachesToDelete = ghCaches.filter((cache) => !remoteRefs.has(cache.ref)); | ||
console.log(`Found ${cachesToDelete.length} caches to delete`); | ||
|
||
for (const { key, ref } of cachesToDelete) { | ||
deleteGitHubCache(key, ref); | ||
} | ||
|
||
console.log("Done"); | ||
} | ||
|
||
function listGitHubCaches() { | ||
const ghCachesOutput = /** @type {{ key: string, ref: string }[]} */ ( | ||
JSON.parse( | ||
runCmd( | ||
// We ask for the output to be a JSON array of {key, ref} objects. | ||
"gh", | ||
["cache", "list", "--limit", "100", "--json", "key,ref"], | ||
), | ||
) | ||
); | ||
console.log(`Found ${ghCachesOutput.length} cache keys`); | ||
return ghCachesOutput; | ||
} | ||
|
||
function listRemoteRefs(githubRepository) { | ||
const parsedLsRemoteOutput = parseGitRemoteOutput( | ||
runCmd( | ||
// We use `git ls-remote` so we also receive refs such as `refs/pull/123/head` | ||
// which are not downloaded by `git fetch` or `git pull`. | ||
"git", | ||
["ls-remote", `https://github.com/${githubRepository}.git`], | ||
), | ||
); | ||
|
||
const remoteRefs = new Set( | ||
parsedLsRemoteOutput.map(({ gitRefName }) => gitRefName), | ||
); | ||
|
||
console.log(`Found ${remoteRefs.size} remote refs`); | ||
return remoteRefs; | ||
} | ||
|
||
function deleteGitHubCache( | ||
/** @type {string} */ key, | ||
/** @type {string} */ ref, | ||
) { | ||
try { | ||
console.group(`Deleting cache "${key}" for ref "${ref}"`); | ||
runCmd("gh", ["cache", "delete", key], { collectStdout: false }); | ||
console.log(`Done`); | ||
} catch (e) { | ||
console.warn(`::warning::Failed to delete cache key ${key}`); | ||
} finally { | ||
console.groupEnd(); | ||
} | ||
} | ||
|
||
function assertGithubActionsEnv() { | ||
const GITHUB_REPOSITORY = process.env.GITHUB_REPOSITORY; | ||
assert( | ||
GITHUB_REPOSITORY, | ||
"GITHUB_REPOSITORY environment variable is required. This environment variable is typically set by GitHub Actions.", | ||
); | ||
|
||
const ghVersion = runCmd("gh", ["--version"]).trim(); | ||
const gitVersion = runCmd("git", ["--version"]).trim(); | ||
|
||
console.group("Environment"); | ||
console.log(`GITHUB_REPOSITORY: ${GITHUB_REPOSITORY}`); | ||
console.log(`gh version: ${ghVersion}`); | ||
console.log(`git version: ${gitVersion}`); | ||
console.groupEnd(); | ||
|
||
return { repository: GITHUB_REPOSITORY, ghVersion, gitVersion }; | ||
} | ||
|
||
function runCmd( | ||
/** @type {string} */ cmd, | ||
/** @type {string[]} */ args, | ||
{ collectStdout = true } = {}, | ||
) { | ||
return execFileSync(cmd, args, { | ||
encoding: "utf-8", | ||
stdio: | ||
// stdin, stdout, stderr | ||
["ignore", collectStdout ? "pipe" : "inherit", "inherit"], | ||
}); | ||
} | ||
|
||
function parseGitRemoteOutput(/** @type {string} */ str) { | ||
// According to https://git-scm.com/docs/git-ls-remote, the output is: | ||
// <oid> TAB <ref> LF | ||
// `oid` being the internal Git object ID, and `ref` the reference name | ||
|
||
return str | ||
.trim() | ||
.split("\n") | ||
.map((line) => { | ||
const [gitObjectId, gitRefName] = line.split("\t"); | ||
return { gitObjectId, gitRefName }; | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.