Skip to content

Build a template

Template.build submits the definition and waits for completion. It returns the template name, template ID, and build ID. Options can set CPU, memory, tags, skipCache, and a log callback.

For a long-running build, use buildInBackground in JavaScript or build_in_background in Python. It returns the identifiers immediately. Poll with getBuildStatus or get_build_status to receive the current state and new log entries.

mjs
import { Template } from "@abox-dev/sdk";

const templateName = process.env.TEMPLATE_NAME ?? "background-build";
const template = Template().fromBaseImage().runCmd("echo build-ready");

const build = await Template.buildInBackground(template, templateName);
let logsOffset = 0;

while (true) {
  const current = await Template.getBuildStatus(build, { logsOffset });
  logsOffset += current.logEntries.length;

  for (const entry of current.logEntries) {
    console.log(entry.toString());
  }

  if (current.status === "ready") {
    console.log(`Build ${build.buildId} is ready`);
    break;
  }
  if (current.status === "error") {
    throw new Error(current.reason?.message ?? "Template build failed");
  }

  await new Promise((resolve) => setTimeout(resolve, 2_000));
}

Build states are building, waiting, ready, and error. Increase logsOffset by the number of processed entries to avoid receiving them again.

Use build in a simple script. Background builds are useful in workers and CI jobs where your application owns the polling loop.