# Build
<subtitle>How to build a template</subtitle>

## Build and wait for completion

The `build` method builds the template and waits for the build to complete. It returns build information including template ID and build ID.

```python
build_info = Template.build(
    template,
    'my-template',
    cpu_count=2, # Number of CPU cores
    memory_mb=2048, # Memory (MB)
    skip_cache=False, # Configure cache skip (except files)
    on_build_logs=default_build_logger(), # The log callback receives the LogEntry object
    api_key="your-api-key", # Override API key
    domain="your-domain", # Cover domain name
)

# build_info contains: BuildInfo(name, template_id, build_id)
```

## Background build

The `build_in_background` method starts the build process and returns immediately without waiting for completion. This is useful when you want to trigger a build and check its status later.

```python
build_info = Template.build_in_background(
    template,
    'my-template',
    cpu_count=2,
    memory_mb=2048,
)

# Return immediately: BuildInfo(name, template_id, build_id)
```

## Check build status

Use `get_build_status` to check the status of a build started with `build_in_background`.

```python
status = Template.get_build_status(
    build_info,
    logs_offset=0, # Optional: Get the offset of the log
)

# status contains build status and logs
```

## Example: Background build with status polling

```python
# Start the build in the background
build_info = Template.build_in_background(
    template,
    'my-template',
    cpu_count=2,
    memory_mb=2048,
)

# Poll build status
import time

logs_offset = 0
status = "building"

while status == "building":
    build_status = Template.get_build_status(
        build_info,
        logs_offset=logs_offset,
    )

    logs_offset += len(build_status.log_entries)
    status = build_status.status.value

    for log_entry in build_status.log_entries:
        print(log_entry)

    # Wait a short time before checking status again
    time.sleep(2)

if status == "ready":
    print("Build completed successfully")
else:
    print("Build failed")
```
