import { intro, log, outro } from "@clack/prompts"; import { EXTERNAL_DEPLOYMENT_ID_MAX_LENGTH, getBranch, needsNodeRuntimeUpdate, prepareDeploymentError, tryCatch, } from "@trigger.dev/core/v3"; import type { DeployBuildPath, InitializeDeploymentRequestBody, InitializeDeploymentResponseBody, GitMeta, DeploymentTriggeredVia, } from "@trigger.dev/core/v3/schemas"; import { BuildManifest } from "@trigger.dev/core/v3/schemas"; import type { Command } from "commander"; import { Option as CommandOption } from "commander"; import { join, relative, resolve } from "node:path"; import { isCI, isWindows } from "std-env"; import { x } from "tinyexec"; import { z } from "zod"; import chalk from "chalk"; import type { CliApiClient } from "../apiClient.js"; import { buildWorker, writeContainerfile } from "../build/buildWorker.js"; import { resolveAlwaysExternal } from "../build/externals.js"; import { createContextArchive, getArchiveSize } from "../deploy/archiveContext.js"; import { createBundleArchive } from "../deploy/bundleArchive.js"; import { type BuildLogRenderer, BuildLogsMode, createBuildLogRenderer, resolveBuildLogsMode, streamDeploymentEvents, } from "../deploy/buildLogs.js"; import { applyBuildPathOptions, nativeOnlyFlagError, resolveBuildPath, } from "../deploy/buildPath.js"; import { readCliVersionPolicy, warnAboutCliVersionPolicy } from "../deploy/cliVersionPolicy.js"; import { S2 } from "@s2-dev/streamstore"; import { mkdir, readFile, unlink } from "node:fs/promises"; import { CommonCommandOptions, commonOptions, handleTelemetry, OutroCommandError, SkipLoggingError, exitAfterTracking, wrapCommandAction, } from "../cli/common.js"; import { loadConfig } from "../config.js"; import { authenticateForDeploy, userIdForDeploy } from "../deploy/auth.js"; import { archivePreviewBranch, runTrustedDeploy, shouldUseGithubOidc, validateTrustedDeployOptions, type NativeBuildClient, } from "../deploy/trustedDeploy.js"; import { buildImage } from "../deploy/buildImage.js"; import { type DeployEnvironment, resolveDeployEnvironment } from "../deploy/environment.js"; import { checkLogsForErrors, checkLogsForWarnings, printErrors, printWarnings, saveLogs, } from "../deploy/logs.js"; import { chalkError, cliLink, isLinksSupported, prettyError, prettyWarning, } from "../utilities/cliOutput.js"; import { SCHEDULE_PLAN_LIMIT_HEADER } from "../dev/errors.js"; import { loadDotEnvVars } from "../utilities/dotEnv.js"; import { isDirectory } from "../utilities/fileSystem.js"; import { setGithubActionsOutputAndEnvVars } from "../utilities/githubActions.js"; import { createGitMeta, isGitHubActions } from "../utilities/gitMeta.js"; import { printStandloneInitialBanner } from "../utilities/initialBanner.js"; import { resolveLocalEnvVars } from "../utilities/localEnvVars.js"; import { logger } from "../utilities/logger.js"; import { getProjectClient, upsertBranch } from "../utilities/session.js"; import { getTmpDir } from "../utilities/tempDirectories.js"; import { outputEnv, spinner } from "../utilities/windows.js"; import { login } from "./login.js"; import { updateTriggerPackages } from "./update.js"; const DeployCommandOptions = CommonCommandOptions.extend({ authMethod: z.enum(["auto", "oidc"]).default("auto"), dryRun: z.boolean().default(false), skipSyncEnvVars: z.boolean().default(false), env: z.enum(["prod", "staging", "preview", "production"]).optional(), prod: z.boolean().default(false), branch: z.string().optional(), load: z.boolean().optional(), config: z.string().optional(), projectRef: z.string().optional(), saveLogs: z.boolean().default(false), skipUpdateCheck: z.boolean().default(false), skipPromotion: z.boolean().default(false), externalId: z.string().optional(), force: z.boolean().default(false), cache: z.boolean().default(true), envFile: z.string().optional(), // Local build options forceLocalBuild: z.boolean().optional(), localBuild: z.boolean().optional(), useRegistryCache: z.boolean().default(false), network: z.enum(["default", "none", "host"]).optional(), push: z.boolean().optional(), builder: z.string().default("trigger"), nativeBuildServer: z.boolean().default(false), nativeBuild: z.boolean().default(false), depotBuild: z.boolean().default(false), localBundle: z.boolean().default(false), fromBundle: z.string().optional(), detach: z.boolean().default(false), plain: z.boolean().default(false), buildLogs: BuildLogsMode.default("compact"), compression: z.enum(["zstd", "gzip"]).default("zstd"), cacheCompression: z.enum(["zstd", "gzip"]).default("zstd"), compressionLevel: z.number().optional(), forceCompression: z.boolean().default(true), }); // `env` is resolved from --env, --prod and TRIGGER_ACCESS_TOKEN before the command runs export type DeployCommandOptions = Omit, "env"> & { env: DeployEnvironment; }; type Deployment = InitializeDeploymentResponseBody; export function configureDeployCommand(program: Command) { return ( commonOptions( program .command("deploy") .addOption( new CommandOption( "--auth-method ", "Authentication method. OIDC ignores stored credentials and requires GitHub Actions." ) .choices(["auto", "oidc"]) .default("auto") ) .description("Deploy your Trigger.dev project to the cloud.") .argument("[path]", "The path to the project", ".") .option( "-e, --env ", "The environment to deploy to: prod, staging or preview. Defaults to the environment of the API key in TRIGGER_ACCESS_TOKEN, otherwise prod." ) .option("--prod", "Deploy to the prod environment. Shorthand for --env prod.") .option( "-b, --branch ", "The preview branch to deploy to when passing --env preview. If not provided, we'll detect your git branch." ) .option("--skip-update-check", "Skip checking for @trigger.dev package updates") .option("-c, --config ", "The name of the config file, found at [path]") .option( "-p, --project-ref ", "The project ref. Required if there is no config file. This will override the project specified in the config file." ) .option( "--dry-run", "Do a dry run of the deployment. This will not actually deploy the project, but will show you what would be deployed." ) .option( "--skip-sync-env-vars", "Skip syncing environment variables when using the syncEnvVars extension." ) .option( "--env-file ", "Path to the .env file to load into the CLI process. Defaults to .env in the project directory." ) .option( "--skip-promotion", "Skip promoting the deployment to the current deployment for the environment." ) .option( "--external-id ", `An id of your choosing for this deploy, such as a commit SHA, CI run id or release tag (max ${EXTERNAL_DEPLOYMENT_ID_MAX_LENGTH} characters). Deploying the same id again returns the existing version instead of building it twice.` ) .option( "--force", "Build again even if --external-id has already been deployed. Requires --external-id." ) ) .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." ) .conflicts("useRegistryCache") .hideHelp() ) .addOption( new CommandOption("--load", "Load the built image into your local docker").hideHelp() ) .addOption( new CommandOption( "--no-load", "Do not load the built image into your local docker" ).hideHelp() ) .addOption( new CommandOption( "--save-logs", "If provided, will save logs even for successful builds" ).hideHelp() ) .addOption( new CommandOption( "--compression ", "Compression algorithm for image layers: zstd or gzip (default: zstd)" ) .choices(["zstd", "gzip"]) .hideHelp() ) .addOption( new CommandOption( "--cache-compression ", "Compression algorithm for build cache: zstd or gzip (default: zstd)" ) .choices(["zstd", "gzip"]) .hideHelp() ) .addOption( new CommandOption( "--compression-level ", "The compression level to use when building the image." ).hideHelp() ) .addOption( new CommandOption( "--force-compression", "Force recompression of all layers. Enabled by default when using zstd." ).hideHelp() ) .addOption( new CommandOption( "--no-force-compression", "Disable forced recompression of layers." ).hideHelp() ) // Local build options .addOption( new CommandOption("--force-local-build", "Deprecated alias for --local-build") .implies({ localBuild: true, }) .conflicts(["nativeBuild", "nativeBuildServer", "localBundle", "detach"]) .hideHelp() ) .addOption( new CommandOption("--local-build", "Build the deployment image locally").conflicts([ "nativeBuild", "nativeBuildServer", "localBundle", "detach", ]) ) .addOption(new CommandOption("--push", "Push the image after local builds").hideHelp()) .addOption( new CommandOption("--no-push", "Do not push the image after local builds").hideHelp() ) .addOption( new CommandOption( "--network ", "The networking mode for RUN instructions when building locally" ).hideHelp() ) .addOption( new CommandOption( "--builder ", "The builder to use when building locally" ).hideHelp() ) .addOption( new CommandOption( "--native-build", "Build the image on the native build server, ignoring the build path configured for this project on the server" ) .implies({ nativeBuildServer: true }) .conflicts(["localBuild", "forceLocalBuild", "depotBuild", "fromBundle"]) ) .addOption(new CommandOption("--native-build-server", "Alias for --native-build").hideHelp()) .addOption( new CommandOption( "--depot-build", "Build the image with Depot, ignoring the build path configured for this project on the server" ).conflicts([ "nativeBuild", "nativeBuildServer", "localBundle", "localBuild", "forceLocalBuild", "fromBundle", "detach", ]) ) .addOption( new CommandOption( "--local-bundle", "Experimental: install and bundle locally and upload only the build output. Requires --native-build." ).conflicts(["localBuild", "forceLocalBuild", "depotBuild"]) ) .addOption( new CommandOption( "--from-bundle ", "Internal: build the image from a pre-built bundle directory. Implies a local build." ) .implies({ localBuild: true }) .conflicts(["nativeBuild", "nativeBuildServer", "depotBuild", "localBundle", "detach"]) .hideHelp() ) .addOption( new CommandOption( "--detach", "Return immediately after the deployment is queued, do not wait for the build to complete. Requires --native-build." ) ) .addOption(new CommandOption("--plain", "Plain output").hideHelp()) .addOption( new CommandOption( "--build-logs ", "How to show the build logs: compact (a single updating line) or full (every line). CI and piped output always use full." ) .choices(["compact", "full"]) .default("compact") ) .action(async (path, options) => { await handleTelemetry(async () => { await printStandloneInitialBanner(true, options.profile); await deployCommand(path, options); }); }) ); } async function deployCommand(dir: string, options: unknown) { return await wrapCommandAction("deployCommand", DeployCommandOptions, options, async (opts) => { const environment = resolveDeployEnvironment({ env: opts.env, prod: opts.prod, accessToken: shouldUseGithubOidc(opts.authMethod, process.env.TRIGGER_ACCESS_TOKEN) ? undefined : process.env.TRIGGER_ACCESS_TOKEN, }); logger.debug("Resolved deploy environment", environment); return await _deployCommand(dir, { ...opts, env: environment.env }); }); } async function _deployCommand(dir: string, options: DeployCommandOptions) { const useOidc = shouldUseGithubOidc(options.authMethod, process.env.TRIGGER_ACCESS_TOKEN); if (useOidc) validateTrustedDeployOptions(options); const nativeOnlyError = nativeOnlyFlagError({ ...options, nativeBuildServer: options.nativeBuildServer || useOidc, }); if (nativeOnlyError) { throw new Error(nativeOnlyError); } if (options.externalId !== undefined) { options.externalId = options.externalId.trim(); if (options.externalId.length === 0) { throw new Error("--external-id must not be empty."); } if (options.externalId.length > EXTERNAL_DEPLOYMENT_ID_MAX_LENGTH) { throw new Error( `--external-id must be at most ${EXTERNAL_DEPLOYMENT_ID_MAX_LENGTH} characters.` ); } } if (options.force && !options.externalId) { throw new Error( "--force requires --external-id. Without an id there is no previous deployment for --force to build over." ); } if (!options.plain) { intro(`Deploying to ${options.env}${options.skipPromotion ? " (without promotion)" : ""}`); } if (!options.skipUpdateCheck) { await updateTriggerPackages(dir, { ...options }, true, true); } const cwd = process.cwd(); const projectPath = resolve(cwd, dir); verifyDirectory(dir, projectPath); if (useOidc) { await runTrustedDeploy(projectPath, options, handleNativeBuildServerDeploy); return; } const authorization = await authenticateForDeploy({ accessToken: process.env.TRIGGER_ACCESS_TOKEN, apiUrl: process.env.TRIGGER_API_URL ?? options.apiUrl, profile: options.profile, silent: options.plain, login, }); if (!authorization.ok) { if (authorization.error === "fetch failed") { throw new Error( `Failed to connect to ${authorization.auth?.apiUrl}. Are you sure it's the correct URL?` ); } else { throw new Error( `You must login first. Use the \`login\` CLI command.\n\n${authorization.error}` ); } } const envVars = resolveLocalEnvVars(options.envFile); if (envVars.TRIGGER_PROJECT_REF) { logger.debug("Using project ref from env", { ref: envVars.TRIGGER_PROJECT_REF }); } if (options.fromBundle) { await handleFromBundleDeploy({ bundleDir: options.fromBundle, options, dashboardUrl: authorization.dashboardUrl, auth: authorization.auth, existingDeploymentId: envVars.TRIGGER_EXISTING_DEPLOYMENT_ID, projectRefOverride: options.projectRef ?? envVars.TRIGGER_PROJECT_REF, }); return; } let resolvedConfig = await loadConfig({ cwd: projectPath, overrides: { project: options.projectRef ?? envVars.TRIGGER_PROJECT_REF }, configFile: options.config, }); logger.debug("Resolved config", resolvedConfig); const gitMeta = await createGitMeta(resolvedConfig.workspaceDir); logger.debug("gitMeta", gitMeta); const isAttachingToExistingDeployment = Boolean(envVars.TRIGGER_EXISTING_DEPLOYMENT_ID) || Boolean(gitMeta?.commitSha?.startsWith("deployment_")); if (isAttachingToExistingDeployment && (options.externalId || options.force)) { throw new Error( "--external-id and --force are not supported when attaching to an existing deployment. Remove the flags, or start a new deployment instead." ); } const branch = options.env === "preview" ? getBranch({ specified: options.branch, gitMeta }) : undefined; if (options.env === "preview" && !branch) { throw new Error( "Didn't auto-detect preview branch, so you need to specify one. Pass --branch ." ); } if (options.env === "preview" && branch) { //auto-archive a branch if the PR is merged or closed if (gitMeta?.pullRequestState === "merged" || gitMeta?.pullRequestState === "closed") { log.message(`Pull request ${gitMeta?.pullRequestNumber} is ${gitMeta?.pullRequestState}.`); await archivePreviewBranch( { type: "token", ...authorization.auth }, branch, resolvedConfig.project ); return; } logger.debug("Upserting branch", { env: options.env, branch }); const branchEnv = await upsertBranch({ accessToken: authorization.auth.accessToken, apiUrl: authorization.auth.apiUrl, projectRef: resolvedConfig.project, branch, gitMeta, }); logger.debug("Upserted branch env", branchEnv); if (!branchEnv) { throw new Error(`Failed to create branch "${branch}"`); } log.success(`Using preview branch "${branch}"`); } const projectClient = await getProjectClient({ accessToken: authorization.auth.accessToken, apiUrl: authorization.auth.apiUrl, projectRef: resolvedConfig.project, env: options.env, branch, profile: options.profile, }); if (!projectClient) { throw new Error("Failed to get project client"); } if (needsNodeRuntimeUpdate(resolvedConfig.runtime, undefined)) { prettyWarning( "This deployment uses Node.js 21, which is deprecated.", 'Set runtime: "node-24" in trigger.config.ts and deploy again.' ); } const fetchSettings = deploySettingsReader( projectClient.client, resolvedConfig.project, options.env ); const resolvedBuildPath = await resolveServerBuildPath(options, fetchSettings); warnAboutCliVersionPolicy( await readCliVersionPolicy(fetchSettings, { attachedToDeployment: isAttachingToExistingDeployment, }) ); const buildPath = applyBuildPathOptions(resolvedBuildPath, options); if (options.dryRun && resolvedBuildPath === "native" && buildPath === "depot") { log.info("Dry run is not supported on the native build server path, bundling locally instead"); } if (buildPath === "native_local_bundle") { await handleLocalBundleDeploy({ apiClient: projectClient.client, config: resolvedConfig, dashboardUrl: authorization.dashboardUrl, options, userId: userIdForDeploy(authorization), gitMeta, branch, }); return; } if (buildPath === "native") { await handleNativeBuildServerDeploy({ apiClient: projectClient.client, config: resolvedConfig, dashboardUrl: authorization.dashboardUrl, options, userId: userIdForDeploy(authorization), gitMeta, branch, }); return; } const serverEnvVars = await projectClient.client.getEnvironmentVariables(resolvedConfig.project); loadDotEnvVars(resolvedConfig.workingDir, options.envFile); const destination = getTmpDir(resolvedConfig.workingDir, "build", options.dryRun); const $buildSpinner = spinner({ plain: options.plain }); const forcedExternals = await resolveAlwaysExternal(projectClient.client); const { features } = resolvedConfig; const [error, buildManifest] = await tryCatch( buildWorker({ target: "deploy", environment: options.env, branch, destination: destination.path, resolvedConfig, rewritePaths: true, envVars: serverEnvVars.success ? serverEnvVars.data.variables : {}, allowEmptyEnvironmentVariableValues: serverEnvVars.success && serverEnvVars.data.allowEmptyEnvironmentVariableValues === true, forcedExternals, plain: options.plain, listener: { onBundleStart() { $buildSpinner.start("Building trigger code"); }, onBundleComplete(result) { $buildSpinner.stop("Successfully built code"); logger.debug("Bundle result", result); }, }, }) ); if (error) { $buildSpinner.stop("Failed to build code"); throw error; } logger.debug("Successfully built project to", destination.path); if (options.dryRun) { logger.info(`Dry run complete. View the built project at ${destination.path}`); return; } const deployment = await initializeOrAttachDeployment( projectClient.client, { contentHash: buildManifest.contentHash, userId: userIdForDeploy(authorization), gitMeta, type: features.run_engine_v2 ? "MANAGED" : "V1", runtime: buildManifest.runtime, isLocalBuild: options.localBuild, isNativeBuild: false, triggeredVia: getTriggeredVia(), externalId: options.externalId, force: options.force, supportsInstanceBaseImages: true, }, envVars.TRIGGER_EXISTING_DEPLOYMENT_ID ); if (deployment.outcome === "existing") { const { rawDeploymentLink, rawTestLink } = buildDeploymentLinks({ dashboardUrl: authorization.dashboardUrl, projectRef: resolvedConfig.project, env: options.env, branch, shortCode: deployment.shortCode, }); setDeploymentGithubActionsOutput({ version: deployment.version, shortCode: deployment.shortCode, rawDeploymentLink, rawTestLink, needsPromotion: !deployment.isPromoted, }); warnAboutSkippedBuild(options.externalId, deployment.isPromoted); const message = `Version ${deployment.version} was already deployed for --external-id ${options.externalId} — nothing to build`; if (options.plain) { console.log(message); if (process.env.TRIGGER_DEPLOYMENT_LINK_OUTPUT_DISABLED !== "1") { console.log(`Deployment: ${rawDeploymentLink}`); console.log(`Test: ${rawTestLink}`); } } else { outro( `${message} ${isLinksSupported ? `| ${cliLink("View deployment", rawDeploymentLink)}` : ""}` ); if (!isLinksSupported) { console.log("View deployment"); console.log(rawDeploymentLink); } } return; } warnAboutCanceledDeployments(deployment.canceledDeployments, options.externalId); await applyServerBaseImages({ baseImages: deployment.baseImages, outputPath: destination.path, buildManifest, options, }); // When `externalBuildData` is not present the deployment implicitly goes into the local build path // which is used in self-hosted setups. There are a few subtle differences between local builds for the cloud // and local builds for self-hosted setups. We need to make the separation of the two paths clearer to avoid confusion. const isLocalBuild = options.localBuild || !deployment.externalBuildData; const authenticateToTriggerRegistry = options.localBuild; const skipServerSideRegistryPush = options.localBuild; // Fail fast if we know local builds will fail if (isLocalBuild) { const result = await x("docker", ["buildx", "version"]); if (result.exitCode !== 0) { logger.debug(`"docker buildx version" failed (${result.exitCode}):`, result); throw new Error( "Failed to find docker buildx. Please install it: https://github.com/docker/buildx#installing." ); } } const childVars = buildManifest.deploy.sync?.env ?? {}; const parentVars = buildManifest.deploy.sync?.parentEnv ?? {}; const secretChildVars = buildManifest.deploy.sync?.secretEnv ?? {}; const secretParentVars = buildManifest.deploy.sync?.secretParentEnv ?? {}; const hasVarsToSync = Object.keys(childVars).length > 0 || Object.keys(secretChildVars).length > 0 || // Only sync parent variables if this is a branch environment (branch && (Object.keys(parentVars).length > 0 || Object.keys(secretParentVars).length > 0)); if (hasVarsToSync) { const numberOfEnvVars = Object.keys(childVars).length + Object.keys(parentVars).length + Object.keys(secretChildVars).length + Object.keys(secretParentVars).length; const vars = numberOfEnvVars === 1 ? "var" : "vars"; if (!options.skipSyncEnvVars) { const $spinner = spinner({ plain: options.plain }); $spinner.start(`Syncing ${numberOfEnvVars} env ${vars} with the server`); const uploadResult = await syncEnvVarsWithServer( projectClient.client, resolvedConfig.project, options.env, childVars, parentVars, secretChildVars, secretParentVars ); if (!uploadResult.success) { await failDeploy( projectClient.client, deployment, { name: "SyncEnvVarsError", message: `Failed to sync ${numberOfEnvVars} env ${vars} with the server: ${uploadResult.error}`, }, "", $spinner ); } else { $spinner.stop(`Successfully synced ${numberOfEnvVars} env ${vars} with the server`); } } else { logger.log( "Skipping syncing env vars. The environment variables in your project have changed, but the --skip-sync-env-vars flag was provided." ); } } const version = deployment.version; const { rawDeploymentLink, rawTestLink } = buildDeploymentLinks({ dashboardUrl: authorization.dashboardUrl, projectRef: resolvedConfig.project, env: options.env, branch, shortCode: deployment.shortCode, }); const deploymentLink = cliLink("View deployment", rawDeploymentLink); const testLink = cliLink("Test tasks", rawTestLink); const $spinner = spinner({ plain: options.plain }); const buildSuffix = isLocalBuild && process.env.TRIGGER_LOCAL_BUILD_LABEL_DISABLED !== "1" ? " (local)" : ""; const deploySuffix = isLocalBuild && process.env.TRIGGER_LOCAL_BUILD_LABEL_DISABLED !== "1" ? " (local build)" : ""; if (options.plain) { $spinner.start(`Building version ${version}${buildSuffix}`); } else if (showFullBuildLogs(options)) { log.step(`Building version ${version}\n`); } else { if (isLinksSupported) { $spinner.start(`Building version ${version}${buildSuffix} ${deploymentLink}`); } else { $spinner.start(`Building version ${version}${buildSuffix}`); } } const buildResult = await buildImage({ isLocalBuild, useRegistryCache: options.useRegistryCache, noCache: !options.cache, deploymentId: deployment.id, deploymentVersion: deployment.version, imageTag: deployment.imageTag, imagePlatform: deployment.imagePlatform, load: options.load, contentHash: deployment.contentHash, externalBuildId: deployment.externalBuildData?.buildId, externalBuildToken: deployment.externalBuildData?.buildToken, externalBuildProjectId: deployment.externalBuildData?.projectId, projectId: projectClient.id, projectRef: resolvedConfig.project, apiUrl: projectClient.client.apiURL, apiKey: projectClient.client.accessToken!, apiClient: projectClient.client, branchName: branch, authAccessToken: authorization.auth.accessToken, compilationPath: destination.path, buildEnvVars: buildManifest.build.env, buildsMachineImages: isLocalBuild && deployment.supportsMachineImages, compression: options.compression, cacheCompression: options.cacheCompression, compressionLevel: options.compressionLevel, forceCompression: options.forceCompression, onLog: (logMessage) => { if (showFullBuildLogs(options)) { console.log(logMessage); return; } if (isLinksSupported) { $spinner.message( `Building version ${version}${buildSuffix} ${deploymentLink}: ${logMessage}` ); } else { $spinner.message(`Building version ${version}${buildSuffix}: ${logMessage}`); } }, // Local build options network: options.network, builder: options.builder, push: options.push, authenticateToRegistry: authenticateToTriggerRegistry, }); logger.debug("Build result", buildResult); const warnings = checkLogsForWarnings(buildResult.logs); const canShowLocalBuildHint = !isLocalBuild && process.env.TRIGGER_LOCAL_BUILD_HINT_DISABLED !== "1"; const buildFailed = !warnings.ok || !buildResult.ok; if (buildFailed && canShowLocalBuildHint) { const providerStatus = await projectClient.client.getRemoteBuildProviderStatus(); if (providerStatus.success && providerStatus.data.status === "degraded") { prettyWarning(providerStatus.data.message + "\n"); } } if (!warnings.ok) { await failDeploy( projectClient.client, deployment, { name: "BuildError", message: warnings.summary }, buildResult.logs, $spinner, warnings.warnings, warnings.errors ); throw new SkipLoggingError("Failed to build image"); } if (!buildResult.ok) { await failDeploy( projectClient.client, deployment, { name: "BuildError", message: buildResult.error }, buildResult.logs, $spinner, warnings.warnings ); throw new SkipLoggingError("Failed to build image"); } const getDeploymentResponse = await projectClient.client.getDeployment(deployment.id); if (!getDeploymentResponse.success) { await failDeploy( projectClient.client, deployment, { name: "DeploymentError", message: getDeploymentResponse.error }, buildResult.logs, $spinner ); throw new SkipLoggingError(getDeploymentResponse.error); } const deploymentWithWorker = getDeploymentResponse.data; if (!deploymentWithWorker.worker) { const errorData = deploymentWithWorker.errorData ? prepareDeploymentError(deploymentWithWorker.errorData) : undefined; await failDeploy( projectClient.client, deployment, { name: "DeploymentError", message: errorData?.message ?? "Failed to get deployment with worker", }, buildResult.logs, $spinner ); throw new SkipLoggingError(errorData?.message ?? "Failed to get deployment with worker"); } if (options.plain) { $spinner.message(`Deploying version ${version}${deploySuffix}`); } else if (showFullBuildLogs(options)) { log.step(`Deploying version ${version}${deploySuffix}\n`); } else { if (isLinksSupported) { $spinner.message(`Deploying version ${version}${deploySuffix} ${deploymentLink}`); } else { $spinner.message(`Deploying version ${version}${deploySuffix}`); } } const finalizeResponse = await projectClient.client.finalizeDeployment( deployment.id, { imageDigest: buildResult.digest, machineImageDigests: buildResult.machineDigests, skipPromotion: options.skipPromotion, skipPushToRegistry: skipServerSideRegistryPush, }, (logMessage) => { if (showFullBuildLogs(options)) { console.log(logMessage); return; } if (isLinksSupported) { $spinner.message( `Deploying version ${version}${deploySuffix} ${deploymentLink}: ${logMessage}` ); } else { $spinner.message(`Deploying version ${version}${deploySuffix}: ${logMessage}`); } } ); if (!finalizeResponse.success) { await failDeploy( projectClient.client, deployment, { name: "FinalizeError", message: finalizeResponse.error }, buildResult.logs, $spinner ); throw new SkipLoggingError("Failed to finalize deployment"); } if (options.plain) { console.log(`Successfully deployed version ${version}${deploySuffix}`); } else if (showFullBuildLogs(options)) { log.step(`Successfully deployed version ${version}${deploySuffix}`); } else { $spinner.stop(`Successfully deployed version ${version}${deploySuffix}`); } const taskCount = deploymentWithWorker.worker?.tasks.length ?? 0; if (options.plain) { console.log( `Version ${version} deployed with ${taskCount} detected task${taskCount === 1 ? "" : "s"}` ); if (process.env.TRIGGER_DEPLOYMENT_LINK_OUTPUT_DISABLED !== "1") { console.log(`Deployment: ${rawDeploymentLink}`); console.log(`Test: ${rawTestLink}`); } } else { outro( `Version ${version} deployed with ${taskCount} detected task${taskCount === 1 ? "" : "s"} ${ isLinksSupported ? `| ${deploymentLink} | ${testLink}` : "" }` ); if (!isLinksSupported) { console.log("View deployment"); console.log(rawDeploymentLink); console.log(); // new line console.log("Test tasks"); console.log(rawTestLink); } } if (options.saveLogs) { const logPath = await saveLogs(deployment.shortCode, buildResult.logs); console.log(`Full build logs have been saved to ${logPath}`); } setDeploymentGithubActionsOutput({ version, shortCode: deployment.shortCode, rawDeploymentLink, rawTestLink, needsPromotion: options.skipPromotion, }); } async function syncEnvVarsWithServer( apiClient: CliApiClient, projectRef: string, environmentSlug: string, envVars: Record, parentEnvVars?: Record, secretEnvVars?: Record, secretParentEnvVars?: Record ) { const hasNonSecret = Object.keys(envVars).length > 0 || Object.keys(parentEnvVars ?? {}).length > 0; const hasSecret = Object.keys(secretEnvVars ?? {}).length > 0 || Object.keys(secretParentEnvVars ?? {}).length > 0; // The import API applies isSecret per call, so secret and non-secret vars go in separate calls. // Default to success so an all-empty call (no vars to sync) is a no-op, not undefined. let result: Awaited> = { success: true, data: { success: true }, }; if (hasNonSecret) { result = await apiClient.importEnvVars(projectRef, environmentSlug, { variables: envVars, parentVariables: parentEnvVars, override: true, }); } if (hasSecret && result.success) { result = await apiClient.importEnvVars(projectRef, environmentSlug, { variables: secretEnvVars ?? {}, parentVariables: secretParentEnvVars, override: true, isSecret: true, }); } return result; } async function failDeploy( client: CliApiClient, deployment: Pick, error: { name: string; message: string }, logs: string, $spinner: ReturnType, warnings?: string[], errors?: string[] ) { logger.debug("failDeploy", { error, logs, warnings, errors }); $spinner.stop(`Failed to deploy project`); const doOutputLogs = async (prefix: string = "Error") => { if (logs.trim() !== "") { const logPath = await saveLogs(deployment.shortCode, logs); printWarnings(warnings); printErrors(errors); checkLogsForErrors(logs); outro( `${chalkError(`${prefix}:`)} ${ error.message }. Full build logs have been saved to ${logPath}` ); // Display the last few lines of the logs, remove #-prefixed ones const lastFewLines = logs .split("\n") .filter((line) => !line.startsWith("#")) .filter((line) => line.trim() !== "") .slice(-5) .join("\n"); if (lastFewLines.trim() !== "") { console.log("Last few lines of logs:\n"); console.log(lastFewLines); } } else { outro(`${chalkError(`${prefix}:`)} ${error.message}`); } }; const exitCommand = (message: string) => { throw new SkipLoggingError(message); }; const deploymentResponse = await client.getDeployment(deployment.id); if (!deploymentResponse.success) { logger.debug(`Failed to get deployment with worker: ${deploymentResponse.error}`); } else { const serverDeployment = deploymentResponse.data; switch (serverDeployment.status) { case "PENDING": case "INSTALLING": case "DEPLOYING": case "BUILDING": { await doOutputLogs(); await client.failDeployment(deployment.id, { error, }); exitCommand("Failed to deploy project"); break; } case "CANCELED": { await doOutputLogs(serverDeployment.canceledReason ?? "Canceled"); exitCommand("Failed to deploy project"); break; } case "FAILED": { const errorData = serverDeployment.errorData ? prepareDeploymentError(serverDeployment.errorData) : undefined; if (errorData) { if (errorData.name === "SchedulePlanLimitError") { prettyError(SCHEDULE_PLAN_LIMIT_HEADER, errorData.message); } else { prettyError(errorData.message, errorData.stack, errorData.stderr); } if (logs.trim() !== "") { const logPath = await saveLogs(deployment.shortCode, logs); outro(`Aborting deployment. Full build logs have been saved to ${logPath}`); } else { outro(`Aborting deployment`); } } else { await doOutputLogs("Failed"); } exitCommand("Failed to deploy project"); break; } case "DEPLOYED": { await doOutputLogs("Deployed with errors"); exitCommand("Deployed with errors"); break; } case "TIMED_OUT": { await doOutputLogs("TimedOut"); exitCommand("Timed out"); break; } } } } async function initializeOrAttachDeployment( apiClient: CliApiClient, data: InitializeDeploymentRequestBody, existingDeploymentId?: string ): Promise { if (existingDeploymentId) { // In the build server we initialize the deployment before installing the project dependencies, // so that the status is correctly reflected in the dashboard. In this case, we need to attach // to the existing deployment and continue with the remote build process. // This is a workaround to avoid major changes in the deploy command and workflow. In the future, // we'll likely make the build server the entry point of the flow for building and deploying and also // adapt the related deployment API endpoints. const existingDeploymentOrError = await apiClient.getDeployment(existingDeploymentId); if (!existingDeploymentOrError.success) { throw new Error( `Failed to attach to existing deployment: ${existingDeploymentOrError.error}` ); } const { imageReference, status } = existingDeploymentOrError.data; if (!imageReference) { // this is just an artifact of our current DB schema // `imageReference` is stored as nullable, but it should always exist throw new Error("Existing deployment does not have an image reference"); } if ( status === "CANCELED" || status === "FAILED" || status === "TIMED_OUT" || status === "DEPLOYED" ) { throw new Error(`Existing deployment is in an unexpected state: ${status}`); } return { ...existingDeploymentOrError.data, imageTag: imageReference, outcome: "created" as const, }; } const newDeploymentOrError = await apiClient.initializeDeployment({ ...data, }); if (!newDeploymentOrError.success) { throw new Error(`Failed to start deployment: ${newDeploymentOrError.error}`); } return newDeploymentOrError.data; } function buildDeploymentLinks({ dashboardUrl, projectRef, env, branch, shortCode, }: { dashboardUrl: string; projectRef: string; env: DeployCommandOptions["env"]; branch: string | undefined; shortCode: string; }) { const testParams = new URLSearchParams({ environment: env === "staging" ? "stg" : env }); if (env === "preview" && branch) { testParams.set("branch", branch); } return { rawDeploymentLink: `${dashboardUrl}/projects/v3/${projectRef}/deployments/${shortCode}`, rawTestLink: `${dashboardUrl}/projects/v3/${projectRef}/test?${testParams}`, }; } async function applyServerBaseImages({ baseImages, outputPath, buildManifest, options, }: { baseImages: InitializeDeploymentResponseBody["baseImages"]; outputPath: string; buildManifest: BuildManifest; options: DeployCommandOptions; }) { if (!baseImages) { return; } const required = [ baseImages.base ? `base ${baseImages.base}` : undefined, baseImages.buildBase ? `build ${baseImages.buildBase}` : undefined, ].filter(Boolean); const message = `Building on base images required by this instance: ${required.join(", ")}`; if (options.plain) { console.log(message); } else { log.info(message); } await writeContainerfile(outputPath, { ...buildManifest, image: { ...buildManifest.image, ...baseImages }, }); } function warnAboutSkippedBuild(externalId: string | undefined, isPromoted: boolean | undefined) { prettyWarning( "Environment variables were not synced because nothing was built.", `If your environment variables have changed, deploy again with --force --external-id ${externalId} to rebuild this id and sync them.` ); if (isPromoted === false) { prettyWarning( "This version is not the current deployment.", "Promote it from the dashboard, or deploy again with --force to build a new version." ); } } function warnAboutCanceledDeployments( canceledDeployments: Array<{ version: string }> | undefined, externalId: string | undefined ) { if (!canceledDeployments?.length || !externalId) { return; } const versions = canceledDeployments.map((deployment) => deployment.version); const header = versions.length === 1 ? `--force canceled version ${versions[0]}, which was still building for --external-id ${externalId}` : `--force canceled ${versions.length} in-progress deployments for --external-id ${externalId}: ${versions.join(", ")}`; prettyWarning( header, "A canceled deployment can never be deployed. The build is signalled to stop, but a build running on another machine can keep going for a few minutes before it notices." ); } function setDeploymentGithubActionsOutput({ version, shortCode, rawDeploymentLink, rawTestLink, needsPromotion, }: { version: string; shortCode: string; rawDeploymentLink: string; rawTestLink: string; needsPromotion: boolean; }) { setGithubActionsOutputAndEnvVars({ envVars: { TRIGGER_DEPLOYMENT_VERSION: version, TRIGGER_VERSION: version, TRIGGER_DEPLOYMENT_SHORT_CODE: shortCode, TRIGGER_DEPLOYMENT_URL: rawDeploymentLink, TRIGGER_TEST_URL: rawTestLink, }, outputs: { deploymentVersion: version, workerVersion: version, deploymentShortCode: shortCode, deploymentUrl: rawDeploymentLink, testUrl: rawTestLink, needsPromotion: needsPromotion ? "true" : "false", }, }); } function getTriggeredVia(): DeploymentTriggeredVia { // Check specific CI providers first (most specific to least specific) if (isGitHubActions()) { return "cli:github_actions"; } if (process.env.GITLAB_CI === "true") { return "cli:gitlab_ci"; } if (process.env.CIRCLECI === "true") { return "cli:circleci"; } if (process.env.JENKINS_URL) { return "cli:jenkins"; } if (process.env.TF_BUILD === "True") { return "cli:azure_pipelines"; } if (process.env.BITBUCKET_BUILD_NUMBER) { return "cli:bitbucket_pipelines"; } if (process.env.TRAVIS === "true") { return "cli:travis_ci"; } if (process.env.BUILDKITE === "true") { return "cli:buildkite"; } // Fallback for other/unknown CI environments if (isCI) { return "cli:ci_other"; } return "cli:manual"; } const DEPLOY_SETTINGS_TIMEOUT_MS = 5_000; function deploySettingsReader(apiClient: CliApiClient, projectRef: string, env: string) { let settings: ReturnType | undefined; return () => (settings ??= apiClient.getDeploySettings( projectRef, env, AbortSignal.timeout(DEPLOY_SETTINGS_TIMEOUT_MS) )); } async function resolveServerBuildPath( options: DeployCommandOptions, fetchSettings: ReturnType ): Promise { const resolved = await resolveBuildPath(options, fetchSettings); switch (resolved.from) { case "flag": logger.debug(`Build path ${resolved.buildPath} from ${resolved.flag}`); break; case "fallback": logger.debug("Failed to fetch deploy settings", { failure: resolved.failure }); if (!resolved.silent) { log.warn("Could not read the deploy settings from the server, using the Depot build path"); } break; case "server": logger.debug(`Build path ${resolved.buildPath} (server)`); break; } return resolved.buildPath; } async function handleNativeBuildServerDeploy({ apiClient, options, config, dashboardUrl, userId, gitMeta, branch, }: { apiClient: NativeBuildClient; config: Awaited>; dashboardUrl: string; options: DeployCommandOptions; userId?: string; gitMeta?: GitMeta; branch?: string; }) { const tmpDir = join(config.workingDir, ".trigger", "tmp"); await mkdir(tmpDir, { recursive: true }); const archivePath = join(tmpDir, `deploy-${Date.now()}.tar.gz`); const $deploymentSpinner = spinner({ plain: options.plain }); $deploymentSpinner.start("Preparing deployment files"); await createContextArchive(config.workspaceDir, archivePath); const archiveSize = await getArchiveSize(archivePath); const sizeMB = (archiveSize / 1024 / 1024).toFixed(2); $deploymentSpinner.message(`Deployment files ready (${sizeMB} MB)`); const artifactResult = await apiClient.createArtifact({ type: "deployment_context", contentType: "application/gzip", contentLength: archiveSize, }); if (!artifactResult.success) { $deploymentSpinner.stop("Failed creating deployment artifact"); log.error(chalk.bold(chalkError(artifactResult.error))); throw new OutroCommandError(`Deployment failed`); } const { artifactKey, uploadUrl, uploadFields } = artifactResult.data; logger.debug("Artifact created", { artifactKey }); $deploymentSpinner.message("Uploading deployment files"); const [readError, fileBuffer] = await tryCatch(readFile(archivePath)); if (readError) { $deploymentSpinner.stop("Failed reading deployment archive"); log.error(chalk.bold(chalkError(readError.message))); throw new OutroCommandError(`Deployment failed`); } const formData = new FormData(); for (const [key, value] of Object.entries(uploadFields)) { formData.append(key, value); } const blob = new Blob([new Uint8Array(fileBuffer)], { type: "application/gzip" }); formData.append("file", blob, "deployment.tar.gz"); const [uploadError, uploadResponse] = await tryCatch( fetch(uploadUrl, { method: "POST", body: formData, }) ); if (uploadError || !uploadResponse?.ok) { $deploymentSpinner.stop("Failed to upload deployment files"); log.error( chalk.bold( chalkError( `${uploadError?.message} (${uploadResponse?.statusText} ${uploadResponse?.status})` ) ) ); throw new OutroCommandError(`Deployment failed`); } const [unlinkError] = await tryCatch(unlink(archivePath)); if (unlinkError) { logger.debug("Failed to delete deployment artifact file", { archivePath, error: unlinkError }); } $deploymentSpinner.message("Deployment files uploaded"); const configFilePath = config.configFile !== undefined ? relative(config.workspaceDir, config.configFile).replace(/\\/g, "/") : undefined; const initializeDeploymentResult = await apiClient.initializeDeployment({ contentHash: "-", userId, gitMeta, type: config.features.run_engine_v2 ? "MANAGED" : "V1", runtime: config.runtime, isNativeBuild: true, artifactKey, skipPromotion: options.skipPromotion, configFilePath, triggeredVia: getTriggeredVia(), externalId: options.externalId, force: options.force, }); if (!initializeDeploymentResult.success) { $deploymentSpinner.stop("Failed to initialize deployment"); log.error(chalk.bold(chalkError(initializeDeploymentResult.error))); throw new OutroCommandError(`Deployment failed`); } const deployment = initializeDeploymentResult.data; const { rawDeploymentLink, rawTestLink } = buildDeploymentLinks({ dashboardUrl, projectRef: config.project, env: options.env, branch, shortCode: deployment.shortCode, }); if (deployment.outcome === "existing") { $deploymentSpinner.stop(`Version ${deployment.version} was already deployed`); warnAboutCliVersionPolicy(artifactResult.data.cliVersionPolicy); setDeploymentGithubActionsOutput({ version: deployment.version, shortCode: deployment.shortCode, rawDeploymentLink, rawTestLink, needsPromotion: !deployment.isPromoted, }); warnAboutSkippedBuild(options.externalId, deployment.isPromoted); outro( `Version ${deployment.version} was already deployed for --external-id ${options.externalId} — nothing to build ${ isLinksSupported ? `| ${cliLink("View deployment", rawDeploymentLink)}` : rawDeploymentLink }` ); return; } const exposedDeploymentLink = isLinksSupported ? cliLink(chalk.bold(rawDeploymentLink), rawDeploymentLink) : chalk.bold(rawDeploymentLink); $deploymentSpinner.stop("Deployment initialized"); warnAboutCliVersionPolicy(artifactResult.data.cliVersionPolicy); log.info(`View deployment: ${exposedDeploymentLink}`); warnAboutCanceledDeployments(deployment.canceledDeployments, options.externalId); setDeploymentGithubActionsOutput({ version: deployment.version, shortCode: deployment.shortCode, rawDeploymentLink, rawTestLink, needsPromotion: options.skipPromotion, }); if (options.detach) { outro(`Version ${deployment.version} is being deployed`); return; } const { eventStream } = deployment; if (!eventStream) { log.warn(`Failed streaming build logs, open the deployment in the dashboard to view the logs`); outro(`Version ${deployment.version} is being deployed`); return exitAfterTracking(0); } await followBuildServerDeployment({ deployment, eventStream, options, rawDeploymentLink, rawTestLink, }); } export function verifyDirectory(dir: string, projectPath: string) { if (dir !== "." && !isDirectory(projectPath)) { if (dir === "staging" || dir === "prod" || dir === "preview") { throw new Error(`To deploy to ${dir}, you need to pass "--env ${dir}", not just "${dir}".`); } if (dir === "production") { throw new Error(`To deploy to production, you need to pass "--env prod", not "production".`); } if (dir === "stg") { throw new Error(`To deploy to staging, you need to pass "--env staging", not "stg".`); } throw new Error(`Directory "${dir}" not found at ${projectPath}`); } } // --local-bundle: install + bundling happen locally, only the build output is // uploaded, and the build server runs just the container build from it. async function handleLocalBundleDeploy({ apiClient, options, config, dashboardUrl, userId, gitMeta, branch, }: { apiClient: CliApiClient; config: Awaited>; dashboardUrl: string; options: DeployCommandOptions; userId?: string; gitMeta?: GitMeta; branch?: string; }) { const tmpDir = join(config.workingDir, ".trigger", "tmp"); await mkdir(tmpDir, { recursive: true }); const archivePath = join(tmpDir, `deploy-${Date.now()}.tar.gz`); const ignoredBuildFlags = [ options.compression !== "zstd" && "--compression", options.cacheCompression !== "zstd" && "--cache-compression", options.compressionLevel !== undefined && "--compression-level", !options.forceCompression && "--no-force-compression", !options.cache && "--no-cache", options.builder !== "trigger" && "--builder", options.network !== undefined && "--network", options.push !== undefined && "--push/--no-push", options.load !== undefined && "--load/--no-load", ].filter((flag): flag is string => Boolean(flag)); if (ignoredBuildFlags.length > 0) { log.warn( `The following flags are ignored with --local-bundle (the image is built remotely): ${ignoredBuildFlags.join(", ")}` ); } const serverEnvVars = await apiClient.getEnvironmentVariables(config.project); loadDotEnvVars(config.workingDir, options.envFile); // Keep the bundle dir around on dry runs so the printed path is inspectable const destination = getTmpDir(config.workingDir, "build", options.dryRun); const forcedExternals = await resolveAlwaysExternal(apiClient); const $buildSpinner = spinner({ plain: options.plain }); const [buildError, buildManifest] = await tryCatch( buildWorker({ target: "deploy", environment: options.env, branch, destination: destination.path, resolvedConfig: config, rewritePaths: true, envVars: serverEnvVars.success ? serverEnvVars.data.variables : {}, allowEmptyEnvironmentVariableValues: serverEnvVars.success && serverEnvVars.data.allowEmptyEnvironmentVariableValues === true, forcedExternals, plain: options.plain, listener: { onBundleStart() { $buildSpinner.start("Building trigger code"); }, onBundleComplete(result) { $buildSpinner.stop("Successfully built code"); logger.debug("Bundle result", result); }, }, }) ); if (buildError) { $buildSpinner.stop("Failed to build code"); throw buildError; } const bundleManifest = buildManifest; const bundleOutputPath = destination.path; // Extensions can set undefined values at runtime despite the manifest type const bundleBuildEnvVars = Object.fromEntries( Object.entries(buildManifest.build.env ?? {}).filter( (entry): entry is [string, string] => typeof entry[1] === "string" ) ); if (options.dryRun) { logger.info(`Dry run complete. View the built bundle at ${destination.path}`); return; } // Sync BEFORE init: init enqueues the build synchronously, so a post-init sync races a fast build const childVars = buildManifest.deploy.sync?.env ?? {}; const parentVars = buildManifest.deploy.sync?.parentEnv ?? {}; const secretChildVars = buildManifest.deploy.sync?.secretEnv ?? {}; const secretParentVars = buildManifest.deploy.sync?.secretParentEnv ?? {}; const hasVarsToSync = Object.keys(childVars).length > 0 || Object.keys(secretChildVars).length > 0 || // Only sync parent variables if this is a branch environment (branch && (Object.keys(parentVars).length > 0 || Object.keys(secretParentVars).length > 0)); if (!options.skipSyncEnvVars) { if (hasVarsToSync) { const uploadResult = await syncEnvVarsWithServer( apiClient, config.project, options.env, childVars, parentVars, secretChildVars, secretParentVars ); if (!uploadResult.success) { throw new Error(`Failed to sync env vars with the server: ${uploadResult.error}`); } logger.debug("Synced env vars with the server"); } } else if (hasVarsToSync) { logger.log( "Skipping syncing env vars. The environment variables in your project have changed, but the --skip-sync-env-vars flag was provided." ); } const $deploymentSpinner = spinner({ plain: options.plain }); $deploymentSpinner.start("Preparing deployment files"); await createBundleArchive(bundleOutputPath, archivePath); const archiveSize = await getArchiveSize(archivePath); const sizeMB = (archiveSize / 1024 / 1024).toFixed(2); $deploymentSpinner.message(`Deployment files ready (${sizeMB} MB)`); const artifactResult = await apiClient.createArtifact({ type: "deployment_bundle", contentType: "application/gzip", contentLength: archiveSize, }); if (!artifactResult.success) { $deploymentSpinner.stop("Failed creating deployment artifact"); log.error(chalk.bold(chalkError(artifactResult.error))); throw new OutroCommandError(`Deployment failed`); } const { artifactKey, uploadUrl, uploadFields } = artifactResult.data; logger.debug("Artifact created", { artifactKey }); // Defense in depth: current older servers already reject the deployment_bundle // type at createArtifact; this catches a server that accepts it but returns a // non-bundle key, which would make the remote build treat the bundle as source. if (!artifactKey.startsWith("bundles/")) { $deploymentSpinner.stop("Failed creating deployment artifact"); log.error( chalk.bold( chalkError( "This server does not support --local-bundle deploys yet. Deploy without --local-bundle instead." ) ) ); throw new OutroCommandError(`Deployment failed`); } $deploymentSpinner.message("Uploading deployment files"); const [readError, fileBuffer] = await tryCatch(readFile(archivePath)); if (readError) { $deploymentSpinner.stop("Failed reading deployment archive"); log.error(chalk.bold(chalkError(readError.message))); throw new OutroCommandError(`Deployment failed`); } const formData = new FormData(); for (const [key, value] of Object.entries(uploadFields)) { formData.append(key, value); } const blob = new Blob([new Uint8Array(fileBuffer)], { type: "application/gzip" }); formData.append("file", blob, "deployment.tar.gz"); const [uploadError, uploadResponse] = await tryCatch( fetch(uploadUrl, { method: "POST", body: formData, }) ); if (uploadError || !uploadResponse?.ok) { $deploymentSpinner.stop("Failed to upload deployment files"); log.error( chalk.bold( chalkError( `${uploadError?.message} (${uploadResponse?.statusText} ${uploadResponse?.status})` ) ) ); throw new OutroCommandError(`Deployment failed`); } const [unlinkError] = await tryCatch(unlink(archivePath)); if (unlinkError) { logger.debug("Failed to delete deployment artifact file", { archivePath, error: unlinkError }); } $deploymentSpinner.message("Deployment files uploaded"); const configFilePath = config.configFile !== undefined ? relative(config.workspaceDir, config.configFile).replace(/\\/g, "/") : undefined; const initializeDeploymentResult = await apiClient.initializeDeployment({ contentHash: bundleManifest.contentHash, userId, gitMeta, type: config.features.run_engine_v2 ? "MANAGED" : "V1", // config.runtime (not the manifest runtime) to match classic native deploys runtime: config.runtime, isNativeBuild: true, artifactKey, skipPromotion: options.skipPromotion, configFilePath, triggeredVia: getTriggeredVia(), externalId: options.externalId, force: options.force, fromBundle: true, buildEnvVars: Object.keys(bundleBuildEnvVars).length > 0 ? bundleBuildEnvVars : undefined, }); if (!initializeDeploymentResult.success) { $deploymentSpinner.stop("Failed to initialize deployment"); log.error(chalk.bold(chalkError(initializeDeploymentResult.error))); throw new OutroCommandError(`Deployment failed`); } const deployment = initializeDeploymentResult.data; const { rawDeploymentLink, rawTestLink } = buildDeploymentLinks({ dashboardUrl, projectRef: config.project, env: options.env, branch, shortCode: deployment.shortCode, }); if (deployment.outcome === "existing") { $deploymentSpinner.stop(`Version ${deployment.version} was already deployed`); setDeploymentGithubActionsOutput({ version: deployment.version, shortCode: deployment.shortCode, rawDeploymentLink, rawTestLink, needsPromotion: !deployment.isPromoted, }); warnAboutSkippedBuild(options.externalId, deployment.isPromoted); outro( `Version ${deployment.version} was already deployed for --external-id ${options.externalId} — nothing to build ${ isLinksSupported ? `| ${cliLink("View deployment", rawDeploymentLink)}` : rawDeploymentLink }` ); return; } const exposedDeploymentLink = isLinksSupported ? cliLink(chalk.bold(rawDeploymentLink), rawDeploymentLink) : chalk.bold(rawDeploymentLink); $deploymentSpinner.stop("Deployment initialized"); log.info(`View deployment: ${exposedDeploymentLink}`); warnAboutCanceledDeployments(deployment.canceledDeployments, options.externalId); setDeploymentGithubActionsOutput({ version: deployment.version, shortCode: deployment.shortCode, rawDeploymentLink, rawTestLink, needsPromotion: options.skipPromotion, }); if (options.detach) { outro(`Version ${deployment.version} is being deployed`); return; } const { eventStream } = deployment; if (!eventStream) { log.warn(`Failed streaming build logs, open the deployment in the dashboard to view the logs`); outro(`Version ${deployment.version} is being deployed`); return exitAfterTracking(0); } await followBuildServerDeployment({ deployment, eventStream, options, rawDeploymentLink, rawTestLink, }); } // Builds the image locally from the bundle and finalizes the deployment. async function buildAndFinalizeFromBundle({ apiClient, projectId, projectRef, deployment, options, dashboardUrl, authAccessToken, compilationPath, buildEnvVars, branch, isLocalBuild, }: { apiClient: CliApiClient; projectId: string; projectRef: string; deployment: Deployment; options: DeployCommandOptions; dashboardUrl: string; authAccessToken: string; compilationPath: string; buildEnvVars: Record | undefined; branch: string | undefined; isLocalBuild: boolean; }) { const authenticateToTriggerRegistry = options.localBuild; const skipServerSideRegistryPush = options.localBuild; const version = deployment.version; const { rawDeploymentLink, rawTestLink } = buildDeploymentLinks({ dashboardUrl, projectRef, env: options.env, branch, shortCode: deployment.shortCode, }); const deploymentLink = cliLink("View deployment", rawDeploymentLink); const testLink = cliLink("Test tasks", rawTestLink); const $spinner = spinner({ plain: options.plain }); const buildSuffix = isLocalBuild && process.env.TRIGGER_LOCAL_BUILD_LABEL_DISABLED !== "1" ? " (local)" : ""; const deploySuffix = isLocalBuild && process.env.TRIGGER_LOCAL_BUILD_LABEL_DISABLED !== "1" ? " (local build)" : ""; if (options.plain) { $spinner.start(`Building version ${version}${buildSuffix}`); } else if (showFullBuildLogs(options)) { log.step(`Building version ${version}\n`); } else { if (isLinksSupported) { $spinner.start(`Building version ${version}${buildSuffix} ${deploymentLink}`); } else { $spinner.start(`Building version ${version}${buildSuffix}`); } } const buildResult = await buildImage({ isLocalBuild, useRegistryCache: options.useRegistryCache, noCache: !options.cache, deploymentId: deployment.id, deploymentVersion: deployment.version, imageTag: deployment.imageTag, imagePlatform: deployment.imagePlatform, load: options.load, contentHash: deployment.contentHash, externalBuildId: deployment.externalBuildData?.buildId, externalBuildToken: deployment.externalBuildData?.buildToken, externalBuildProjectId: deployment.externalBuildData?.projectId, projectId, projectRef, apiUrl: apiClient.apiURL, apiKey: apiClient.accessToken!, apiClient, branchName: branch, authAccessToken, compilationPath, buildEnvVars, buildsMachineImages: isLocalBuild && deployment.supportsMachineImages, compression: options.compression, cacheCompression: options.cacheCompression, compressionLevel: options.compressionLevel, forceCompression: options.forceCompression, onLog: (logMessage) => { if (showFullBuildLogs(options)) { console.log(logMessage); return; } if (isLinksSupported) { $spinner.message( `Building version ${version}${buildSuffix} ${deploymentLink}: ${logMessage}` ); } else { $spinner.message(`Building version ${version}${buildSuffix}: ${logMessage}`); } }, // Local build options network: options.network, builder: options.builder, push: options.push, authenticateToRegistry: authenticateToTriggerRegistry, }); logger.debug("Build result", buildResult); const warnings = checkLogsForWarnings(buildResult.logs); const canShowLocalBuildHint = !isLocalBuild && process.env.TRIGGER_LOCAL_BUILD_HINT_DISABLED !== "1"; const buildFailed = !warnings.ok || !buildResult.ok; if (buildFailed && canShowLocalBuildHint) { const providerStatus = await apiClient.getRemoteBuildProviderStatus(); if (providerStatus.success && providerStatus.data.status === "degraded") { prettyWarning(providerStatus.data.message + "\n"); } } if (!warnings.ok) { await failDeploy( apiClient, deployment, { name: "BuildError", message: warnings.summary }, buildResult.logs, $spinner, warnings.warnings, warnings.errors ); throw new SkipLoggingError("Failed to build image"); } if (!buildResult.ok) { await failDeploy( apiClient, deployment, { name: "BuildError", message: buildResult.error }, buildResult.logs, $spinner, warnings.warnings ); throw new SkipLoggingError("Failed to build image"); } const getDeploymentResponse = await apiClient.getDeployment(deployment.id); if (!getDeploymentResponse.success) { await failDeploy( apiClient, deployment, { name: "DeploymentError", message: getDeploymentResponse.error }, buildResult.logs, $spinner ); throw new SkipLoggingError(getDeploymentResponse.error); } const deploymentWithWorker = getDeploymentResponse.data; if (!deploymentWithWorker.worker) { const errorData = deploymentWithWorker.errorData ? prepareDeploymentError(deploymentWithWorker.errorData) : undefined; await failDeploy( apiClient, deployment, { name: "DeploymentError", message: errorData?.message ?? "Failed to get deployment with worker", }, buildResult.logs, $spinner ); throw new SkipLoggingError(errorData?.message ?? "Failed to get deployment with worker"); } if (options.plain) { $spinner.message(`Deploying version ${version}${deploySuffix}`); } else if (showFullBuildLogs(options)) { log.step(`Deploying version ${version}${deploySuffix}\n`); } else { if (isLinksSupported) { $spinner.message(`Deploying version ${version}${deploySuffix} ${deploymentLink}`); } else { $spinner.message(`Deploying version ${version}${deploySuffix}`); } } const finalizeResponse = await apiClient.finalizeDeployment( deployment.id, { imageDigest: buildResult.digest, machineImageDigests: buildResult.machineDigests, skipPromotion: options.skipPromotion, skipPushToRegistry: skipServerSideRegistryPush, }, (logMessage) => { if (showFullBuildLogs(options)) { console.log(logMessage); return; } if (isLinksSupported) { $spinner.message( `Deploying version ${version}${deploySuffix} ${deploymentLink}: ${logMessage}` ); } else { $spinner.message(`Deploying version ${version}${deploySuffix}: ${logMessage}`); } } ); if (!finalizeResponse.success) { await failDeploy( apiClient, deployment, { name: "FinalizeError", message: finalizeResponse.error }, buildResult.logs, $spinner ); throw new SkipLoggingError("Failed to finalize deployment"); } if (options.plain) { console.log(`Successfully deployed version ${version}${deploySuffix}`); } else if (showFullBuildLogs(options)) { log.step(`Successfully deployed version ${version}${deploySuffix}`); } else { $spinner.stop(`Successfully deployed version ${version}${deploySuffix}`); } const taskCount = deploymentWithWorker.worker?.tasks.length ?? 0; if (options.plain) { console.log( `Version ${version} deployed with ${taskCount} detected task${taskCount === 1 ? "" : "s"}` ); if (process.env.TRIGGER_DEPLOYMENT_LINK_OUTPUT_DISABLED !== "1") { console.log(`Deployment: ${rawDeploymentLink}`); console.log(`Test: ${rawTestLink}`); } } else { outro( `Version ${version} deployed with ${taskCount} detected task${taskCount === 1 ? "" : "s"} ${ isLinksSupported ? `| ${deploymentLink} | ${testLink}` : "" }` ); if (!isLinksSupported) { console.log("View deployment"); console.log(rawDeploymentLink); console.log(); // new line console.log("Test tasks"); console.log(rawTestLink); } } if (options.saveLogs) { const logPath = await saveLogs(deployment.shortCode, buildResult.logs); console.log(`Full build logs have been saved to ${logPath}`); } setDeploymentGithubActionsOutput({ version, shortCode: deployment.shortCode, rawDeploymentLink, rawTestLink, needsPromotion: options.skipPromotion, }); } // Runs only the container build from a pre-built bundle dir, skipping config loading // entirely. Attach mode is the supported flow (build server); fresh-init is for testing. async function handleFromBundleDeploy({ bundleDir, options, dashboardUrl, auth, existingDeploymentId, projectRefOverride, }: { bundleDir: string; options: DeployCommandOptions; dashboardUrl: string; auth: { accessToken: string; apiUrl: string }; existingDeploymentId?: string; projectRefOverride?: string; }) { const bundlePath = resolve(process.cwd(), bundleDir); if (!isDirectory(bundlePath)) { throw new Error(`Bundle directory not found at ${bundlePath}`); } const [manifestReadError, manifestRaw] = await tryCatch( readFile(join(bundlePath, "build.json"), "utf-8") ); if (manifestReadError) { throw new Error( `Failed to read build.json in the bundle directory: ${manifestReadError.message}` ); } let manifestJson: unknown; try { manifestJson = JSON.parse(manifestRaw); } catch { throw new Error(`Invalid build.json in the bundle directory: not valid JSON`); } const manifestResult = BuildManifest.safeParse(manifestJson); if (!manifestResult.success) { throw new Error(`Invalid build.json in the bundle directory: ${manifestResult.error.message}`); } const bundleManifest = manifestResult.data; // --dry-run must never touch the server if (options.dryRun) { logger.info(`Dry run complete. Validated bundle at ${bundlePath}`); return; } const projectRef = projectRefOverride ?? bundleManifest.config.project; const branch = options.env === "preview" ? getBranch({ specified: options.branch }) : undefined; if (options.env === "preview" && !branch) { throw new Error( "Preview deploys from a bundle require an explicit branch. Pass --branch ." ); } // In attach mode the branch env already exists if (options.env === "preview" && branch && !existingDeploymentId) { await upsertBranch({ accessToken: auth.accessToken, apiUrl: auth.apiUrl, projectRef, branch, gitMeta: undefined, }); } const projectClient = await getProjectClient({ accessToken: auth.accessToken, apiUrl: auth.apiUrl, projectRef, env: options.env, branch, profile: options.profile, }); if (!projectClient) { throw new Error("Failed to get project client"); } warnAboutCliVersionPolicy( await readCliVersionPolicy( deploySettingsReader(projectClient.client, projectRef, options.env), { attachedToDeployment: Boolean(existingDeploymentId), } ) ); // In attach mode the build-arg values are stored encrypted on the deployment let buildEnvVars: Record | undefined; if (existingDeploymentId) { const buildEnvVarsResult = await projectClient.client.getDeploymentBuildEnvVars(existingDeploymentId); if (!buildEnvVarsResult.success) { throw new Error( `Failed to fetch the build environment variables for deployment ${existingDeploymentId}: ${buildEnvVarsResult.error}` ); } buildEnvVars = buildEnvVarsResult.data.variables; } else if (bundleManifest.build.env && Object.keys(bundleManifest.build.env).length > 0) { // Extensions can set undefined values at runtime despite the manifest type buildEnvVars = Object.fromEntries( Object.entries(bundleManifest.build.env).filter( (entry): entry is [string, string] => typeof entry[1] === "string" ) ); } if (!existingDeploymentId) { logger.warn( "No existing deployment to attach to — initializing a fresh local-build deployment from the bundle. This path is intended for testing." ); } const deployment = await initializeOrAttachDeployment( projectClient.client, { contentHash: bundleManifest.contentHash, type: "MANAGED", runtime: bundleManifest.runtime, isLocalBuild: true, isNativeBuild: false, triggeredVia: getTriggeredVia(), supportsInstanceBaseImages: true, }, existingDeploymentId ); if (deployment.baseImages) { const message = "This instance requires custom deploy base images, which --from-bundle deploys cannot apply. Deploy without --from-bundle."; await projectClient.client.failDeployment(deployment.id, { error: { name: "BuildError", message }, }); throw new Error(message); } // Fail fast if we know local builds will fail const buildxResult = await x("docker", ["buildx", "version"]); if (buildxResult.exitCode !== 0) { logger.debug(`"docker buildx version" failed (${buildxResult.exitCode}):`, buildxResult); throw new Error( "Failed to find docker buildx. Please install it: https://github.com/docker/buildx#installing." ); } await buildAndFinalizeFromBundle({ apiClient: projectClient.client, projectId: projectClient.id, projectRef, deployment, options, dashboardUrl, authAccessToken: auth.accessToken, compilationPath: bundlePath, buildEnvVars, branch, isLocalBuild: true, }); } function buildLogsEnv(options: DeployCommandOptions) { return { ...outputEnv(options.plain), windows: isWindows }; } function showFullBuildLogs(options: DeployCommandOptions) { return resolveBuildLogsMode(options.buildLogs, buildLogsEnv(options)) === "full"; } function buildLogStreamError( error: unknown, renderer: BuildLogRenderer, deployment: Pick, rawDeploymentLink: string ): OutroCommandError { renderer.finish("Log stream stopped", "failure"); logger.debug("Build log stream failed", { error }); const reason = (error instanceof Error ? error.message : String(error)) .replace(/\s+/g, " ") .slice(0, 200); log.error(`Build log stream failed: ${reason}`); log.info( "The deployment itself is unaffected and continues on the build server. Check the dashboard for the final status." ); if (!isLinksSupported) { log.info(`View deployment: ${rawDeploymentLink}`); } return new OutroCommandError( `Version ${deployment.version} ${ isLinksSupported ? `| ${cliLink("View deployment", rawDeploymentLink)}` : "" }` ); } async function followBuildServerDeployment({ deployment, eventStream, options, rawDeploymentLink, rawTestLink, }: { deployment: Pick; eventStream: NonNullable; options: DeployCommandOptions; rawDeploymentLink: string; rawTestLink: string; }): Promise { const renderer = createBuildLogRenderer({ mode: resolveBuildLogsMode(options.buildLogs, buildLogsEnv(options)), title: `Building version ${deployment.version}`, }); const abortController = new AbortController(); const s2 = new S2({ accessToken: eventStream.s2.accessToken }); const basin = s2.basin(eventStream.s2.basin); const stream = basin.stream(eventStream.s2.stream); const [readSessionError, readSession] = await tryCatch( stream.readSession( { start: { from: { seqNum: 0 }, clamp: true }, stop: { waitSecs: 60 * 20 }, // 20 minutes }, { signal: abortController.signal } ) ); if (readSessionError) { throw buildLogStreamError(readSessionError, renderer, deployment, rawDeploymentLink); } const [streamError, finalDeploymentEvent] = await tryCatch( streamDeploymentEvents(readSession, renderer, () => abortController.abort()) ); if (streamError) { throw buildLogStreamError(streamError, renderer, deployment, rawDeploymentLink); } if (!renderer.started && !finalDeploymentEvent) { // unlikely that it happens in practice, only in rare corner cases // the timeout would kick in earlier if the build server fails to dequeue the build renderer.finish("Log stream stopped", "failure"); log.error("Failed dequeueing build, please try again shortly"); throw new OutroCommandError( `Version ${deployment.version} ${ isLinksSupported ? `| ${cliLink("View deployment", rawDeploymentLink)}` : "" }` ); } if (!finalDeploymentEvent) { renderer.finish("Log stream stopped", "failure"); log.error( "Stopped receiving updates from the build server, please check the deployment status in the dashboard" ); if (!isLinksSupported) { log.info(`View deployment: ${rawDeploymentLink}`); } throw new OutroCommandError( `Version ${deployment.version} ${ isLinksSupported ? `| ${cliLink("View deployment", rawDeploymentLink)}` : "" }` ); } switch (finalDeploymentEvent.result) { case "succeeded": { renderer.finish("Deployment completed successfully", "success"); if (finalDeploymentEvent.message) { log.success(finalDeploymentEvent.message); } if (options.skipPromotion) { log.info( `This deployment was not automatically promoted. You can promote in the dashboard or via the promote command, e.g, \`npx trigger.dev promote ${deployment.version}\`.` ); } if (!isLinksSupported) { log.info(`Test tasks: ${rawTestLink}`); } outro( `Version ${deployment.version} was deployed ${ isLinksSupported ? `| ${cliLink("Test tasks", rawTestLink)} | ${cliLink( "View deployment", rawDeploymentLink )}` : "" }` ); return exitAfterTracking(0); } case "failed": { renderer.finish("Deployment failed", "failure"); log.error( chalk.bold( chalkError( "Deployment failed" + (finalDeploymentEvent.message ? `: ${finalDeploymentEvent.message}` : "") ) ) ); throw new OutroCommandError( `Version ${deployment.version} deployment failed ${ isLinksSupported ? `| ${cliLink("View deployment", rawDeploymentLink)}` : "" }` ); } case "timed_out": { renderer.finish("Deployment timed out", "failure"); log.error( chalk.bold( chalkError( "Deployment timed out" + (finalDeploymentEvent.message ? `: ${finalDeploymentEvent.message}` : "") ) ) ); throw new OutroCommandError( `Version ${deployment.version} deployment timed out ${ isLinksSupported ? `| ${cliLink("View deployment", rawDeploymentLink)}` : "" }` ); } case "canceled": { renderer.finish("Deployment was canceled", "failure"); log.error( chalk.bold( chalkError( "Deployment was canceled" + (finalDeploymentEvent.message ? `: ${finalDeploymentEvent.message}` : "") ) ) ); throw new OutroCommandError( `Version ${deployment.version} deployment canceled ${ isLinksSupported ? `| ${cliLink("View deployment", rawDeploymentLink)}` : "" }` ); } default: { // This case is only relevant in case we extend the enum in the future. // New enum values will not be treated as errors in older cli versions. renderer.finish("Log stream finished", "success"); if (finalDeploymentEvent.message) { log.message(finalDeploymentEvent.message); } if (!isLinksSupported) { log.info(`Test tasks: ${rawTestLink}`); } outro( `Version ${deployment.version} ${ isLinksSupported ? `| ${cliLink("Test tasks", rawTestLink)} | ${cliLink( "View deployment", rawDeploymentLink )}` : "" }` ); return exitAfterTracking(0); } } }