Skip to content

Commit d5e6625

Browse files
feat: add required cache-key input for v2
Replace the hardcoded GITHUB_REPO_NAME sticky disk key with a required cache-key input. Users must explicitly declare which cache entity their build targets, preventing cache thrashing in multi-target repos. The cache-key is saved to action state and passed through to the commit phase so the same key is used for both expose and commit RPCs. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent ab5c1da commit d5e6625

7 files changed

Lines changed: 30 additions & 8 deletions

File tree

action.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ branding:
55
icon: layers
66
color: black
77
inputs:
8+
cache-key:
9+
description: >-
10+
Unique identifier for this build's Docker layer cache. Builds with the
11+
same cache-key share cached layers across runs. Set this to the Dockerfile
12+
being built (e.g., services/api/Dockerfile). Jobs building different
13+
Dockerfiles should use different cache-keys to avoid cache thrashing.
14+
required: true
815
buildx-version:
916
description: Buildx version. (e.g., v0.23.0, latest)
1017
required: false

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ async function startBlacksmithBuilder(
506506
);
507507

508508
// Save state for post action
509+
stateHelper.setCacheKey(core.getInput("cache-key"));
509510
stateHelper.setExposeId(stickyDiskSetup.exposeId);
510511

511512
return { addr: buildkitdAddr, exposeId: stickyDiskSetup.exposeId };
@@ -959,7 +960,11 @@ void actionsToolkit.run(
959960
"No previous step failures detected, committing sticky disk after successful cleanup",
960961
);
961962

962-
await reporter.commitStickyDisk(exposeId, fsDiskUsageBytes);
963+
await reporter.commitStickyDisk(
964+
exposeId,
965+
fsDiskUsageBytes,
966+
stateHelper.getCacheKey(),
967+
);
963968
} catch (error) {
964969
core.error(
965970
`Failed to commit sticky disk: ${(error as Error).message}`,

src/reporter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,14 @@ export async function reportIntegrityCheckFailure(
207207
export async function commitStickyDisk(
208208
exposeId: string,
209209
fsDiskUsageBytes: number | null,
210+
cacheKey?: string,
210211
): Promise<void> {
211212
try {
212213
const agentClient = createBlacksmithAgentClient();
213214

214215
const commitRequest: Record<string, unknown> = {
215216
exposeId: exposeId,
216-
stickyDiskKey: process.env.GITHUB_REPO_NAME || "",
217+
stickyDiskKey: cacheKey || process.env.GITHUB_REPO_NAME || "",
217218
vmId: process.env.BLACKSMITH_VM_ID || "",
218219
shouldCommit: true,
219220
repoName: process.env.GITHUB_REPO_NAME || "",

src/setup_builder.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,11 +377,12 @@ export async function getStickyDisk(options?: {
377377
throw new Error(`grpc connection test failed: ${(error as Error).message}`);
378378
}
379379

380-
const stickyDiskKey = process.env.GITHUB_REPO_NAME || "";
381-
if (stickyDiskKey === "") {
382-
throw new Error("GITHUB_REPO_NAME is not set");
380+
const cacheKey = core.getInput("cache-key", { required: true });
381+
if (cacheKey === "") {
382+
throw new Error("cache-key input is required but was empty");
383383
}
384-
core.info(`Getting sticky disk for ${stickyDiskKey}`);
384+
const stickyDiskKey = cacheKey;
385+
core.info(`Getting sticky disk for cache-key: ${stickyDiskKey}`);
385386

386387
const response = await client.getStickyDisk(
387388
{

src/state-helper.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ export function getBuilderName(): string {
3838
return core.getState("builderName");
3939
}
4040

41+
export function setCacheKey(key: string) {
42+
core.saveState("cacheKey", key);
43+
}
44+
45+
export function getCacheKey(): string {
46+
return core.getState("cacheKey");
47+
}
48+
4149
let _sigkillUsed = false;
4250

4351
export function setSigkillUsed(used: boolean) {

0 commit comments

Comments
 (0)