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
5 changes: 5 additions & 0 deletions .changeset/funny-ligers-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"trigger.dev": patch
---

Added external cache support for local image builds
12 changes: 11 additions & 1 deletion packages/cli-v3/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const DeployCommandOptions = CommonCommandOptions.extend({
envFile: z.string().optional(),
// Local build options
forceLocalBuild: z.boolean().optional(),
useRegistryCache: z.boolean().default(false),
network: z.enum(["default", "none", "host"]).optional(),
push: z.boolean().optional(),
builder: z.string().default("trigger"),
Expand Down Expand Up @@ -112,11 +113,19 @@ export function configureDeployCommand(program: Command) {
"Skip promoting the deployment to the current deployment for the environment."
)
)
.addOption(
new CommandOption(
"--use-registry-cache",
"Use the registry cache when building the image. The registry must be supported as a cache storage backend."
).hideHelp()
)
.addOption(
new CommandOption(
"--no-cache",
"Do not use the cache when building the image. This will slow down the build process but can be useful if you are experiencing issues with the cache."
).hideHelp()
)
.conflicts("useRegistryCache")
.hideHelp()
)
.addOption(
new CommandOption("--load", "Load the built image into your local docker").hideHelp()
Expand Down Expand Up @@ -418,6 +427,7 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {

const buildResult = await buildImage({
isLocalBuild,
useRegistryCache: options.useRegistryCache,
noCache: options.noCache,
deploymentId: deployment.id,
deploymentVersion: deployment.version,
Expand Down
21 changes: 20 additions & 1 deletion packages/cli-v3/src/deploy/buildImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { CliApiClient } from "../apiClient.js";
export interface BuildImageOptions {
// Common options
isLocalBuild: boolean;
useRegistryCache?: boolean;
imagePlatform: string;
noCache?: boolean;
load?: boolean;
Expand Down Expand Up @@ -53,6 +54,7 @@ export interface BuildImageOptions {
export async function buildImage(options: BuildImageOptions): Promise<BuildImageResults> {
const {
isLocalBuild,
useRegistryCache,
imagePlatform,
noCache,
push,
Expand Down Expand Up @@ -94,6 +96,7 @@ export async function buildImage(options: BuildImageOptions): Promise<BuildImage
authenticateToRegistry,
load,
noCache,
useRegistryCache,
extraCACerts,
apiUrl,
apiKey,
Expand Down Expand Up @@ -307,6 +310,7 @@ interface SelfHostedBuildImageOptions {
apiClient: CliApiClient;
branchName?: string;
noCache?: boolean;
useRegistryCache?: boolean;
extraCACerts?: string;
buildEnvVars?: Record<string, string | undefined>;
network?: string;
Expand All @@ -316,7 +320,7 @@ interface SelfHostedBuildImageOptions {
}

async function localBuildImage(options: SelfHostedBuildImageOptions): Promise<BuildImageResults> {
const { builder, imageTag, deploymentId, apiClient } = options;
const { builder, imageTag, deploymentId, apiClient, useRegistryCache } = options;

// Ensure multi-platform build is supported on the local machine
let builderExists = false;
Expand Down Expand Up @@ -483,6 +487,8 @@ async function localBuildImage(options: SelfHostedBuildImageOptions): Promise<Bu
options.onLog?.(`Successfully logged in to ${cloudRegistryHost}`);
}

const projectCacheRef = getProjectCacheRefFromImageTag(imageTag);

const args = [
"buildx",
"build",
Expand All @@ -491,6 +497,14 @@ async function localBuildImage(options: SelfHostedBuildImageOptions): Promise<Bu
"-f",
"Containerfile",
options.noCache ? "--no-cache" : undefined,
...(useRegistryCache
? [
"--cache-to",
`type=registry,mode=max,image-manifest=true,oci-mediatypes=true,ref=${projectCacheRef}`,
"--cache-from",
`type=registry,ref=${projectCacheRef}`,
]
: []),
"--platform",
options.imagePlatform,
options.network ? `--network=${options.network}` : undefined,
Expand Down Expand Up @@ -928,6 +942,11 @@ function extractRegistryHostFromImageTag(imageTag: string): string | undefined {
return host;
}

function getProjectCacheRefFromImageTag(imageTag: string): string {
const lastColonIndex = imageTag.lastIndexOf(":");
return `${imageTag.substring(0, lastColonIndex)}:cache`;
}

async function getDockerUsernameAndPassword(
apiClient: CliApiClient,
deploymentId: string
Expand Down
Loading