Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/docker-stress-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ on:
workflow_dispatch:
inputs:
push_image:
description: 'Push images to registry'
description: "Push images to registry"
required: false
default: 'false'
default: "false"

env:
REGISTRY: ghcr.io
Expand Down Expand Up @@ -181,7 +181,7 @@ jobs:
with:
version: v0.12.0
driver: docker-container

- name: Build with SBOM and provenance
uses: docker/build-push-action@v5
with:
Expand Down Expand Up @@ -246,7 +246,7 @@ jobs:
dockerfile = "Dockerfile"
tags = ["test/bake:latest"]
}

target "production" {
inherits = ["default"]
tags = ["test/bake:prod"]
Expand Down Expand Up @@ -453,7 +453,7 @@ jobs:
target: base
tags: test/base:latest
outputs: type=docker

- name: Second build - production image
uses: docker/build-push-action@v5
with:
Expand All @@ -462,4 +462,4 @@ jobs:
tags: test/production:latest
build-args: |
BASE_IMAGE=test/base:latest
outputs: type=docker
outputs: type=docker
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ inputs:
buildkit-version:
description: "BuildKit version to install (e.g., v0.16.0, v0.18.0). If not specified, uses system default"
required: false
verbose:
description: "Enable verbose logging for debugging cache operations"
required: false
default: "false"
runs:
using: node20
main: dist/index.js
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface Inputs {
platforms: string[];
nofallback: boolean;
"github-token": string;
verbose: boolean;
}

async function getInputs(): Promise<Inputs> {
Expand All @@ -47,6 +48,7 @@ async function getInputs(): Promise<Inputs> {
platforms: Util.getInputList("platforms"),
nofallback: core.getBooleanInput("nofallback"),
"github-token": core.getInput("github-token"),
verbose: core.getBooleanInput("verbose"),
};
}

Expand Down Expand Up @@ -329,6 +331,8 @@ void actionsToolkit.run(
async () => {
await core.group("Cleaning up Docker builder", async () => {
const exposeId = stateHelper.getExposeId();
const inputs = stateHelper.getInputs() as Inputs;
const verbose = inputs.verbose || false;
let cleanupError: Error | null = null;

try {
Expand All @@ -339,7 +343,7 @@ void actionsToolkit.run(
// Optional: Prune cache before shutdown (non-critical)
try {
core.info("Pruning BuildKit cache");
await pruneBuildkitCache();
await pruneBuildkitCache(verbose);
core.info("BuildKit cache pruned");
} catch (error) {
core.warning(
Expand Down
4 changes: 2 additions & 2 deletions src/setup-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe("setup_builder", () => {
},
);

await setupBuilder.pruneBuildkitCache();
await setupBuilder.pruneBuildkitCache(false);
expect(core.debug).toHaveBeenCalledWith(
"Successfully pruned buildkit cache",
);
Expand All @@ -117,7 +117,7 @@ describe("setup_builder", () => {
},
);

await expect(setupBuilder.pruneBuildkitCache()).rejects.toThrow();
await expect(setupBuilder.pruneBuildkitCache(false)).rejects.toThrow();
expect(core.warning).toHaveBeenCalled();
});
});
Expand Down
120 changes: 118 additions & 2 deletions src/setup_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { promisify } from "util";
import * as TOML from "@iarna/toml";
import * as reporter from "./reporter";
import { execa } from "execa";
// TODO: Uncomment when updating to @buf/[email protected]+
// import { Metric_MetricType } from "@buf/blacksmith_vm-agent.bufbuild_es/stickydisk/v1/stickydisk_pb.js";

// Constants for configuration.
const BUILDKIT_DAEMON_ADDR = "tcp://127.0.0.1:1234";
Expand Down Expand Up @@ -250,6 +252,13 @@ export async function startAndConfigureBuildkitd(
core.info(
`Found ${lines.length - 1} workers, required ${requiredWorkers}`,
);
// TODO: Report how long it took for workers to be available
// Uncomment when updating to @buf/[email protected]+
// const workersAvailableDuration = Date.now() - startTimeBuildkitReady;
// await reporter.reportMetric(
// Metric_MetricType.BPA_V2_DEBUG_WORKERS_AVAILABLE_MS,
// workersAvailableDuration,
// );
break;
}
} catch (error) {
Expand Down Expand Up @@ -287,14 +296,121 @@ export async function startAndConfigureBuildkitd(
* We don't specify any keep bytes here since we are
* handling the ceph volume size limits ourselves in
* the VM Agent.
* @param verbose - If true, logs detailed cache information
* @throws Error if buildctl prune command fails
*/
export async function pruneBuildkitCache(): Promise<void> {
export async function pruneBuildkitCache(verbose = false): Promise<void> {
try {
// Log cache state before pruning using docker buildx du
if (verbose) {
try {
const { stdout: cacheBeforePrune } = await execAsync(
`sudo docker buildx du --builder default 2>/dev/null || true`,
);
if (cacheBeforePrune) {
core.info("BuildKit cache details before prune:");
cacheBeforePrune
.split("\n")
.filter((line) => line.trim())
.forEach((line) => {
core.info(` ${line}`);
});
}

// Also get a summary view with more details
const { stdout: detailedCacheBefore } = await execAsync(
`sudo docker buildx du --builder default --verbose 2>/dev/null || true`,
);
if (detailedCacheBefore) {
const lines = detailedCacheBefore
.split("\n")
.filter((line) => line.trim());
// Log the summary line and first few cache entries for context
const summaryLine = lines.find(
(line) => line.includes("Total:") || line.includes("TOTAL"),
);
if (summaryLine) {
core.info(`Total cache size before prune: ${summaryLine}`);
}
}
} catch (error) {
core.info(
`Could not get cache details before prune: ${(error as Error).message}`,
);
}
}

const sevenDaysInHours = 7 * 24;
await execAsync(
const pruneOutput = await execAsync(
`sudo buildctl --addr ${BUILDKIT_DAEMON_ADDR} prune --keep-duration ${sevenDaysInHours}h --all`,
);

// Parse prune output to get bytes freed
// buildctl prune typically outputs something like "Total: 1.2GB"
if (pruneOutput.stdout && verbose) {
const match = pruneOutput.stdout.match(
/Total:\s*([0-9.]+)\s*([KMGT]?B)/i,
);
if (match) {
// TODO: When updating to @buf/[email protected]+
// Parse the size and unit, convert to bytes, and report metric:
// const size = parseFloat(match[1]);
// const unit = match[2].toUpperCase();
// let bytes = size;
// switch (unit) {
// case 'KB': bytes = size * 1024; break;
// case 'MB': bytes = size * 1024 * 1024; break;
// case 'GB': bytes = size * 1024 * 1024 * 1024; break;
// case 'TB': bytes = size * 1024 * 1024 * 1024 * 1024; break;
// }
// await reporter.reportMetric(
// Metric_MetricType.BPA_V2_PRUNE_BYTES,
// Math.round(bytes),
// );

core.info(`Pruned ${match[0]} from BuildKit cache`);
}
}

// Log cache state after pruning using docker buildx du
if (verbose) {
try {
const { stdout: cacheAfterPrune } = await execAsync(
`sudo docker buildx du --builder default 2>/dev/null || true`,
);
if (cacheAfterPrune) {
core.info("BuildKit cache details after prune:");
cacheAfterPrune
.split("\n")
.filter((line) => line.trim())
.forEach((line) => {
core.info(` ${line}`);
});
}

// Also get a summary view with more details
const { stdout: detailedCacheAfter } = await execAsync(
`sudo docker buildx du --builder default --verbose 2>/dev/null || true`,
);
if (detailedCacheAfter) {
const lines = detailedCacheAfter
.split("\n")
.filter((line) => line.trim());
// Log the summary line
const summaryLine = lines.find(
(line) => line.includes("Total:") || line.includes("TOTAL"),
);
if (summaryLine) {
core.info(`Total cache size after prune: ${summaryLine}`);
}
}
} catch (error) {
core.info(
`Could not get cache details after prune: ${(error as Error).message}`,
);
}
}

core.debug("Successfully pruned buildkit cache");
} catch (error) {
core.warning(`Error pruning buildkit cache: ${(error as Error).message}`);
Expand Down
5 changes: 5 additions & 0 deletions src/state-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ export function setBuilderName(name: string) {
export function getBuilderName(): string {
return core.getState("builderName");
}

export function getInputs(): unknown {
const inputsState = core.getState("inputs");
return inputsState ? JSON.parse(inputsState) : {};
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Post-Action Fails to Retrieve Inputs

The post-action attempts to retrieve the verbose input using stateHelper.getInputs(), but the inputs are not saved to state during the main action. This means stateHelper.getInputs() returns an empty object, so the verbose setting isn't respected during cleanup.

Additional Locations (1)

Fix in Cursor Fix in Web