Skip to content
Esc

    Search bindings, commands, errors and guides.

    Getting started / Deploy from code

    Getting started

    Deploy from code

    Build and roll out Workers on a Rivet cluster from your own deploy tooling with the open-orange TypeScript SDK.

    The open-orange package is also a TypeScript SDK for orange deploy. Your deploy pipeline builds and rolls out Workers with typed results and errors, and parses no CLI output.

    Quickstart

    You need what orange deploy needs: Docker running, a Rivet Cloud cluster, and a Worker project with its packages installed. See Deploy.

    1. Add the package

      pnpm add open-orange
    2. Write the deploy

      deploy takes the orange deploy flags as options and resolves to the same result as the last line of orange deploy --json. Save this as deploy.mts:

      import { deploy } from "open-orange";
      
      const result = await deploy({
        configPath: "workers/api/wrangler.jsonc",
        cluster: "my-cluster",
        onEvent: (event) => console.error(event.type),
      });
      if (result.type === "deployed") console.log(result.buildId, result.url);
    3. Run it

      Node.js 22.18 and later run TypeScript files directly.

      node deploy.mts

    Build once, roll out twice

    build makes the image and uploads it, and rolls out nothing. rollout puts a ready build on a runner pool. So a pipeline promotes the build it tested:

    import { build, rollout } from "open-orange";
    
    const target = { configPath: "wrangler.jsonc", cluster: "my-cluster" };
    const { buildId } = await build(target);
    await rollout({ ...target, build: buildId, pool: "staging" });
    await rollout({ ...target, build: buildId, pool: "production" });

    Handle errors

    A failed call rejects with a typed error. _tag names it, and reportable is true when the failure may be a bug in Orange or Rivet Cloud, so a retry or a bug report can help.

    import { deploy, RivetCloudError } from "open-orange";
    
    try {
      await deploy({ cluster: "my-cluster" });
    } catch (error) {
      if (error instanceof RivetCloudError && error.status === 401) console.error("Rotate RIVET_CLOUD_TOKEN.");
      throw error;
    }

    Use it from Effect

    deployEffect, buildEffect, and rolloutEffect are the same calls as Effect programs. Provide deployLiveLayer and an Output, and replace any service, such as ImageBuilder for a remote builder:

    import * as Effect from "effect/Effect";
    import * as Layer from "effect/Layer";
    import { deployEffect, deployLiveLayer, eventOutputLayer, ImageBuilder } from "open-orange";
    
    // `myBuilder` is your builder's client. Its methods return Effects.
    const remoteBuilder = Layer.succeed(
      ImageBuilder,
      ImageBuilder.of({
        preflight: () => Effect.void,
        build: (image) => myBuilder.build(image),
        save: (reference, tarballPath) => myBuilder.download(reference, tarballPath),
      }),
    );
    
    const result = await deployEffect({ cluster: "my-cluster" }).pipe(
      Effect.provide(remoteBuilder),
      Effect.provide(Layer.merge(deployLiveLayer, eventOutputLayer((event) => console.error(event.type)))),
      Effect.runPromise,
    );
    AI context Every call, option, result, event, error, and service

    Written for LLMs and coding agents: every edge case, measurement, and known deviation. You do not need it to use the feature.

    Calls

    PromiseEffectWhat it doesResolves to
    deploy(options)deployEffect(options)orange deploy: a build and a rollout, or a rollout of build, or a dry runDeployedResult or DryRunResult
    build(options)buildEffect(options)Builds the image, uploads it, and waits until Rivet Cloud has prepared itBuildResult
    rollout(options)rolloutEffect(options)Rolls a runner pool out to a ready build and waits until every node runs itDeployedResult
    runOrange(args, io?)Runs the orange CLI in this process, as orange <args>The exit code

    The Promise forms take onEvent too. The Effect forms need DeployServices: Credentials, RivetCloud, WorkerConfig, ImageBuilder, and Output. rolloutEffect builds nothing, so it needs no ImageBuilder.

    Options

    Every call takes these:

    OptionDefaultNotes
    cwdThe directory of configPath, else the current directoryRelative paths and the .rivet/project.json link that orange link saved are found from here.
    configPathThe nearest wrangler.json in cwd or a parent, else the nearest wrangler.jsonc, else wrangler.toml, as Wrangler and orange deploy find it
    environmentNoneThe Wrangler environment, as -e.
    clusterRIVET_CLUSTER, then the link in cwd
    orgRIVET_ORG, then the link
    tokenRIVET_CLOUD_TOKEN, then the orange login sessionA Rivet Cloud token.
    cloudApihttps://cloud-api.rivet.dev
    onEventNonePromise forms only. Receives each event.

    build and deploy also take:

    OptionDefaultNotes
    imageThe Worker’s name
    tagThe UTC time and 4 random hex digits, such as 20261028-213000-3f9a
    outdirA new directory for each call, removed after the image is savedWhere the Dockerfile and build context go.
    secretsFilesNoneTheir values never enter the image.

    rollout and deploy also take region (default: the cluster’s only region), pool (default default), machineType, nodeCount, vars (NAME:value), secrets (KEY or KEY=SECRET_NAME), and secretsFiles. They mean what the orange deploy flags of the same name mean. See Deploy.

    rollout needs build, the ID of a ready build. deploy takes build to roll one out instead of building, and dryRun to write the build context and return the plan. A dry run calls no API and needs no Docker. Without outdir, its context stays in a new directory that the result names.

    Results

    TypeFields
    DeployedResulttype: "deployed", buildId, reference (image:tag, unset when Rivet Cloud does not report the image of a build passed as build), cluster, region, pool, url
    BuildResultbuildId, reference
    DryRunResulttype: "dry_run", dockerfile, context, reference, build, cluster, region, pool, cloudApi, hasToken, variables, secrets, selfHost (build and run as argument lists)

    Events

    onEvent and eventOutputLayer receive the events that orange deploy --json prints, except failure, which is the rejected error instead:

    EventFields
    warningmessage
    startimage, cluster, region, pool
    image_buildreference
    uploadbuildId, bytes
    upload_retryattempt, reason, waitMs
    prepare, build_readybuildId
    secretname, changed, version
    unpinnames
    rolloutpool, buildId, created
    deployed, dry_runThe result

    build emits warning, image_build, upload, upload_retry, prepare, and build_ready. rollout emits warning, start, secret, unpin, rollout, and deployed.

    Orange does not wait for a listener. A listener that throws, or returns a promise that rejects, does not change the result. Orange reports it with process.emitWarning as an OpenOrangeEventListenerWarning, whose cause is the error.

    Errors

    ErrorWhen
    UsageErrorAn invalid option, such as nodeCount: 0
    CredentialsErrorNo cluster, no token, or a sign-in that cannot be read or refreshed
    WorkerConfigErrorThe Wrangler configuration cannot be read or is invalid
    DockerErrorDocker is missing, its daemon does not answer, or a Docker command failed
    RivetCloudErrorRivet Cloud refused a request, answered with something unexpected, or could not be reached. status holds the HTTP status.
    DeployErrorThe deploy itself failed, such as a build or a rollout

    Each is an Error with _tag and message. All but UsageError and CredentialsError have reportable. OrangeError is their union. A defect, any other thrown error, rejects as itself.

    Calls that run at once

    Calls in one process can run at the same time. Each one writes its build context to a directory of its own and tags its image with a random suffix. Two calls that pass the same outdir, or build the same image:tag, at the same time: the second rejects with a DeployError.

    Limits

    • Two calls that run a project’s build.command at the same time can each bundle what the other one wrote. Run one build of a project at a time.
    • Interrupting an Effect call while it writes its build context can leave that work running. Do not reuse its outdir until the process ends.
    • Two calls that reach one outdir through different paths, such as a symlink, are not seen as the same directory.

    Environment

    • RIVET_CLOUD_TOKEN, RIVET_CLUSTER, and RIVET_ORG are read through Effect’s Config. A ConfigProvider replaces them. A variable that the provider leaves out has no value: process.env is not read.
    • Docker’s output and the output of build.command go to stderr.
    • The SDK leaves WRANGLER_LOG as the caller set it.

    Services

    ServiceWhat it doesLive implementation in deployLiveLayer
    CredentialsResolves the cluster, organization, Cloud API, and tokenOptions, then the environment, the link, and orange login
    RivetCloudThe Rivet Cloud API client and the image uploadHTTP, with retries for GET, PUT, and the upload
    WorkerConfigLoads the Worker’s resolved configurationThe Wrangler configuration file
    ImageBuilderpreflight(), build({ contextDirectory, dockerfilePath, reference }), and save(reference, tarballPath), which writes a docker image save tarballdocker on PATH
    OutputReceives eventsNot in deployLiveLayer. Use eventOutputLayer(onEvent).