Skip to content
Merged
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
10 changes: 10 additions & 0 deletions .changeset/cache-options-for-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@cartesi/cli": minor
---

add --cache-from and --cache-to options to cartesi build

Expose Docker Buildx cache backend options via `--cache-from <spec>` and
`--cache-to <spec>` CLI flags (repeatable). The same options can also be
set per-drive in `cartesi.toml` via `cache_from` and `cache_to` arrays.
CLI flags override the TOML values when provided.
35 changes: 32 additions & 3 deletions apps/cli/src/api/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ export type BuildOptions = ConfigOptions &
*/
debug?: boolean;

/**
* Docker Buildx cache sources (`--cache-from`) of every docker drive,
* replacing the ones of the configuration when not empty.
* @default []
*/
cacheFrom?: string[];

/**
* Docker Buildx cache destinations (`--cache-to`) of every docker
* drive, replacing the ones of the configuration when not empty.
* @default []
*/
cacheTo?: string[];

/**
* Only build the drives, do not boot the machine.
* @default false
Expand Down Expand Up @@ -65,6 +79,8 @@ export type BuildResult = {

// context for Listr build tasks
type BuildContext = {
cacheFrom: string[];
cacheTo: string[];
config: Config;
debug: boolean;
destination: string;
Expand All @@ -77,7 +93,7 @@ const buildDriveTask = (
): ListrTask<BuildContext> => ({
title: `Building drive ${chalk.cyan(name)}`,
task: async (ctx, task) => {
const { config, debug, destination } = ctx;
const { cacheFrom, cacheTo, config, debug, destination } = ctx;
const sdk = config.sdk;
const reporter = (line: string) => {
task.output = line;
Expand All @@ -98,7 +114,12 @@ const buildDriveTask = (
case "docker": {
const imageInfo = await buildDocker(
name,
drive,
{
...drive,
cacheFrom:
cacheFrom.length > 0 ? cacheFrom : drive.cacheFrom,
cacheTo: cacheTo.length > 0 ? cacheTo : drive.cacheTo,
},
sdk,
destination,
debug,
Expand Down Expand Up @@ -151,7 +172,13 @@ const buildNvramTask = (
export const build = async (
options: BuildOptions = {},
): Promise<BuildResult> => {
const { debug = false, drivesOnly = false, progress = "silent" } = options;
const {
cacheFrom = [],
cacheTo = [],
debug = false,
drivesOnly = false,
progress = "silent",
} = options;

// get application configuration, from a Config object or 'cartesi.toml'
const config = resolveConfig(options.config);
Expand All @@ -164,6 +191,8 @@ export const build = async (

// build context
const ctx: BuildContext = {
cacheFrom,
cacheTo,
config,
debug,
destination,
Expand Down
14 changes: 13 additions & 1 deletion apps/cli/src/builder/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import type { BuildxMetadata } from "../types/docker.js";

type ImageBuildOptions = Pick<
DockerDriveConfig,
"buildArgs" | "context" | "dockerfile" | "tags" | "target"
| "buildArgs"
| "cacheFrom"
| "cacheTo"
| "context"
| "dockerfile"
| "tags"
| "target"
> & { destination: string; dockerfileContent?: string; reporter?: Reporter };

type ImageInfo = {
Expand All @@ -25,6 +31,8 @@ type ImageInfo = {
const buildImage = async (options: ImageBuildOptions): Promise<string> => {
const {
buildArgs,
cacheFrom,
cacheTo,
context,
destination,
dockerfile,
Expand Down Expand Up @@ -62,6 +70,10 @@ const buildImage = async (options: ImageBuildOptions): Promise<string> => {
const metadataFile = path.join(tmpDir, "metadata.json");
args.push("--metadata-file", metadataFile);

// set cache options
args.push(...cacheFrom.flatMap((spec) => ["--cache-from", spec]));
args.push(...cacheTo.flatMap((spec) => ["--cache-to", spec]));

if (target) {
args.push("--target", target);
}
Expand Down
17 changes: 16 additions & 1 deletion apps/cli/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,27 @@ export const createBuildCommand = () => {
.default(false)
.hideHelp(),
)
.option(
"--cache-from <spec>",
"cache source for docker buildx build (can be repeated)",
(value: string, prev: string[]) => prev.concat([value]),
[] as string[],
)
.option(
"--cache-to <spec>",
"cache destination for docker buildx build (can be repeated)",
(value: string, prev: string[]) => prev.concat([value]),
[] as string[],
)
.option("-d, --drives-only", "only build drives, do not boot machine")
.option("-v, --verbose", "verbose output", false)
.action(async (options) => {
const { config, debug, drivesOnly, verbose } = options;
const { cacheFrom, cacheTo, config, debug, drivesOnly, verbose } =
options;

const result = await build({
cacheFrom,
cacheTo,
config,
debug,
drivesOnly,
Expand Down
2 changes: 2 additions & 0 deletions apps/cli/src/config/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type { Config, DriveConfig, MachineConfig } from "./types.js";
export const defaultRootDriveConfig = (): DriveConfig => ({
builder: "docker",
buildArgs: [],
cacheFrom: [],
cacheTo: [],
context: ".",
dockerfile: "Dockerfile", // file on current working directory
extraSize: 0,
Expand Down
6 changes: 6 additions & 0 deletions apps/cli/src/config/toml/drives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ const BUILDER_KEYS: Record<Builder, readonly string[]> = {
directory: ["directory", "extra_size", "format"],
docker: [
"build_args",
"cache_from",
"cache_to",
"context",
"dockerfile",
"extra_size",
Expand Down Expand Up @@ -149,6 +151,8 @@ const parseBuilderDrive = (builder: Builder, drive: TomlTable): DriveConfig => {
case "docker": {
const {
build_args,
cache_from,
cache_to,
context,
dockerfile,
extra_size,
Expand All @@ -163,6 +167,8 @@ const parseBuilderDrive = (builder: Builder, drive: TomlTable): DriveConfig => {
return {
builder: "docker",
buildArgs: parseStringArray(build_args),
cacheFrom: parseStringArray(cache_from),
cacheTo: parseStringArray(cache_to),
image: parseOptionalString(image),
context: parseString(context, "."),
dockerfile: parseString(dockerfile, "Dockerfile"),
Expand Down
2 changes: 2 additions & 0 deletions apps/cli/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export type DirectoryDriveConfig = {
export type DockerDriveConfig = {
builder: "docker";
buildArgs: string[]; // default is empty array
cacheFrom: string[]; // default is empty array
cacheTo: string[]; // default is empty array
context: string;
dockerfile: string;
extraSize: number; // default is 0 (no extra size)
Expand Down
Loading
Loading