# Workers CLI Reference

These instructions apply to the current Admin Console.

{% hint style="info" %}
Contextual Workers are in early access and may not be available in your account. [Contact Contextual support](mailto:help@contextual.io) to request that Workers be enabled for your tenant.
{% endhint %}

Use `ctxl workers` to create Workers and deploy changes. This documentation assumes you are running Contextual CLI 0.17.0 or later. Start with the [quickstart](https://docs.contextual.io/build/contextual-workers/quickstart.md) for CLI installation, tenant sign-in, Docker setup, and a complete application example.

For production deployments with Service-managed dependencies, follow [Coordinate Worker Deployments and Service Releases](https://docs.contextual.io/build/contextual-workers/releases.md).

Use the plural command group `workers`. Replace `my-config` with your CLI configuration ID and `hello-worker` with your Worker ID.

## Create from a Dockerfile

Run `workers create` from the application's Docker build context:

```sh
ctxl workers create hello-worker \
  --size small \
  --build . \
  --config-id my-config \
  --pretty
```

The `workers create` command builds and pushes a `linux/amd64` image, saves the Worker, and starts deployment. `--size` is required on create. To specify CPU and memory requests and limits, see [Set the Size](#set-the-size). Use `--dockerfile <path>` with `--build` when the Dockerfile has another name or location.

## Build and Push Separately

To prepare an image before creating or updating a Worker:

```sh
ctxl workers images push hello-worker \
  --context . \
  --tag 1.0.0 \
  --config-id my-config \
  --pretty
```

The `workers images push` command builds and pushes the image to your organization's image host without creating or updating a Worker. It prints the image's reference with its digest:

```json
{
  "image": "images.<org>.my.contextual.io/hello-worker:1.0.0@sha256:…"
}
```

Copy the returned `image` reference into `workers create`:

```sh
ctxl workers create hello-worker \
  --size small \
  --image <returned-image-reference> \
  --config-id my-config \
  --pretty
```

Worker images must come from your organization's image host or the platform's. See [Image References](https://docs.contextual.io/build/contextual-workers/about-workers.md#image-references). A local Docker image ID is not an image reference.

## Rebuild and Deploy Code

After editing the application, run:

```sh
ctxl workers update hello-worker \
  --build . \
  --config-id my-config \
  --pretty
```

The `workers update` command with `--build` rebuilds and pushes the image, then saves and deploys a new Worker version. To use an image you already pushed, replace `--build .` with `--image <returned-image-reference>`.

Images are pinned to registry digests when saved. Pushing a new image under an existing tag does not change the image pinned in an existing Worker configuration. Submit an update to select the intended image.

## Deploy from GitHub

A GitHub Connection links your Contextual tenant to a Contextual GitHub App installation on your own GitHub account or organization. The installation controls which repositories the App can access; the connection's deployment client controls its Contextual permissions.

To connect a repository:

1. Select the intended tenant in the console, open **Connections**, and choose **New connection > GitHub connection**. Create the Connection.
2. Open its **GitHub** tab and select **Connect**. On GitHub, choose your account or organization and authorize the App for the repositories you want to use. Organization policy may require an owner to approve the installation.
3. Return to the Connection and confirm **Connected** and the expected account. Use **Manage on GitHub** to review or change repository access.
4. Open **Deploy key permissions** to review the Contextual permissions of the connection's deployment client for the intended deployment.

Installing the App does not deploy a Worker or add a deployment workflow to a repository. Add a workflow using the public [Deploy Contextual Workers Action](https://github.com/ContextualIO/deploy-workers-action). It exchanges the job's OpenID Connect (OIDC) identity for a short-lived Contextual deploy token, configures the CLI and Docker, and builds, pushes, and deploys the selected Workers. The repository must be covered by the connected App installation.

For example, this manually triggered workflow updates an existing Worker from the repository root's Dockerfile. Replace `my-organization` with the target tenant identifier and `my-worker` with the Worker ID:

```yaml
name: Deploy Worker
on: workflow_dispatch

jobs:
  deploy:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: read
    steps:
      - uses: actions/checkout@v7
      - uses: ContextualIO/deploy-workers-action@v1
        with:
          organization: my-organization
          worker: my-worker
          folders: .
          ctxl-version: 0.17.0
          args: --build . --tag ${{ github.sha }}
```

Review the target tenant, Worker ID, image selection, and trigger before running the workflow. To create a missing Worker, supply `worker-size`, such as `small`, and any required template arguments in `args`. Managed templates retain their saved selection on update. Use the [Site deployment inputs](https://docs.contextual.io/build/sites/releases.md#deploy-from-github) for a Site Worker, and the appropriate packaging directory for a managed handler application.

To create a Site starter repository or download its source with the CLI, follow [Initialize Site Source](https://docs.contextual.io/build/sites/contextual-cli.md#initialize-site-source). For a starter repository with the **Publish** and **Deploy a release** workflows, follow [Deploy a Published Release](https://docs.contextual.io/build/sites/releases.md#deploy-a-published-release).

The action reuses images with the selected tag and skips a Worker that already runs that tag. Use a unique tag for changed code; the example uses the commit SHA. Without an explicit `--tag`, it derives `v<version>` from `package.json`. Set `command: images` to build and push without saving a Worker. See the Action's [inputs and multi-folder guidance](https://github.com/ContextualIO/deploy-workers-action#inputs) for other configurations.

For custom CLI steps, [Set up ctxl](https://github.com/ContextualIO/setup-ctxl-action) installs the CLI and, with its `organization` input, creates and selects the target configuration and sets `CONTEXTUAL_TOKEN`. The CLI uses that token in place of stored sign-in credentials. A job that supplies only `CONTEXTUAL_TOKEN` must still create and select its CLI configuration. Keep tokens out of source files, command arguments, logs, and application images.

After the workflow finishes, [inspect the saved configuration and verify the application](#inspect-and-verify). A successful image push alone does not confirm deployment. You can also deploy reviewed code manually with the CLI commands on this page.

Repository access and CI deployment authorization are separate from the running Worker's [platform identity](https://docs.contextual.io/build/contextual-workers/managed-workers/platform-services.md#worker-identity). For a Site, they are also separate from [Site visitor authentication and authorization](https://docs.contextual.io/build/sites/manage-site-users.md#understand-site-user-roles-and-authorization).

## Change Configuration

Update the quickstart's greeting without rebuilding:

```sh
ctxl workers update hello-worker \
  --env "GREETING=Updated worker" \
  --config-id my-config \
  --pretty
```

Only settings supplied in the update change. Repeated `--env KEY=VALUE` flags set the listed variables and preserve existing variables omitted from the update. Use `--env` for non-secret values. Set secret values in the admin console under **Environment variables**, where they are stored encrypted. Do not put credentials in shell arguments, chat, or application images.

For an HTTP Worker using CPU scaling, set `--size`, `--min-replicas`, `--max-replicas`, and `--target-cpu` as needed. Size and scaling updates can replace running containers or change the amount of compute in use. Event backlog scaling is configured in the consuming Compose service or managed template's options. Scheduled Workers start one run per tick. Keep replica settings at 1.

## Set the Size

Each replica gets the CPU and memory you select with `--size`. Use a preset such as `small`, or choose custom requests and limits that meet the [sizing rules](https://docs.contextual.io/build/contextual-workers/configure-worker-size.md#sizing-rules).

To create a Worker with a custom size or switch from a preset, pass `--size custom` and all four resource flags. This example switches the existing `hello-worker` to a custom size.

```sh
ctxl workers update hello-worker \
  --size custom \
  --cpu-request 125m --cpu-limit 500m \
  --memory-request 256Mi --memory-limit 384Mi \
  --config-id my-config
```

Once a Worker uses custom sizing, pass only the values you want to change. For example, this raises the memory limit and keeps the other three values.

```sh
ctxl workers update hello-worker --memory-limit 512Mi --config-id my-config
```

To return to a preset, run `ctxl workers update hello-worker --size small --config-id my-config`. The preset replaces the custom values. Saving a size change starts a deployment. [Check readiness](#inspect-and-verify) when the new replicas start.

## Use Compose or a Managed Template

For a custom Compose file, use `--compose compose.yaml` on create or update. The CLI builds and pushes services that declare `build:` and submits the resulting service image references. It also reads each config's `file:` source into the definition. The Compose file must declare `x-contextual.version: 1` and use the supported Compose subset.

A managed template is selected with `--managed <name>:<version>`. Find available names and versions in the console's **Managed worker** fields. Set the template's declared options with repeated `--option NAME=VALUE` flags. To read an option's value from a file, such as a JSON configuration, use `--option-from-file NAME=PATH`. With a template that declares exactly one application image option, `--build <directory>` builds and supplies the application image.

To add services of your own, pass `--compose compose.yaml` with `--managed` or `--template` instead of the image option. The file's service named like the template's application service is the application, for example `app` in the `managed-event-worker` template, and its other services run in the same Pod. The CLI builds and pushes services that declare `build:`, as for a custom Compose file. See [Add Services With a Compose File](https://docs.contextual.io/build/contextual-workers/managed-workers/templates-and-options.md#add-services-with-a-compose-file).

A `workers update` keeps the saved Compose file while the template name stays the same. To rebuild the application, pass `--compose` again. A `--build` update stops with an error if the Worker was previously built with `--compose`. Add `--force` to override the configuration, which will replace the Compose services with the image option instead:

```sh
ctxl workers update my-events \
  --build .worker-image \
  --force \
  --config-id my-config \
  --pretty
```

When updating the same managed template, supplied `--option` and `--option-from-file` values replace the specified template options and preserve saved options omitted from the update. A `--build` update also retains the stored managed template selection. See [Connect to Contextual](https://docs.contextual.io/build/contextual-workers/connect-to-contextual.md) before selecting an event or scheduled template. For the `managed-event-worker`, `managed-http-worker`, and `managed-scheduled-worker` handler templates, build with `--build .worker-image` after packaging your app. See [Build Your First Managed Worker](https://docs.contextual.io/build/contextual-workers/managed-workers/get-started.md).

## Use a Template

A template is a Compose file that your organization publishes. Declare its options under `x-contextual.options` and use them in the services. An option can declare `native_object: <type>` to name a record of that type in the Worker's organization. See [Options](https://docs.contextual.io/build/contextual-workers/x-contextual-specification.md#options). This template takes the application image as an option and runs a fixed cache image from your organization's image host beside it:

```yaml
x-contextual:
  version: 1
  options:
    IMAGE:
      description: The application image
      required: true
services:
  web:
    image: ${IMAGE}
    x-contextual:
      public: 8080
  cache:
    image: images.<org>.my.contextual.io/orders-cache:2.1.0
```

Publish it as a new version:

```sh
ctxl workers templates push orders:1.0.0 \
  --file compose.yaml \
  --config-id my-config \
  --pretty
```

A template cannot build images. Each service image must be one of these:

* An image option that is the whole image, such as `${IMAGE}`.
* An image on your organization's image host, such as one printed by `workers images push`.
* A platform image, `images.contextual.io/worker/platform/<name>:<tag>`.

When it pushes the template, the CLI checks that each fixed image exists and pins it to its digest. The template keeps the image host references. A version cannot be pushed twice.

Create a Worker from the template. Select the template and version with `--template <name>:<version>` and set its options as you would for a managed template. With exactly one image option, `--build` builds and supplies the image:

```sh
ctxl workers create my-orders \
  --size small \
  --template orders:1.0.0 \
  --build . \
  --config-id my-config \
  --pretty
```

List your organization's templates and versions with `ctxl workers templates list`. Delete a template, or one version, with `ctxl workers templates delete <name>[:<version>]`. A template or version that a Worker uses cannot be deleted.

## Inspect and Verify

Read configuration, list Workers, and inspect deployment instances:

```sh
ctxl records list native-object:worker --config-id my-config --pretty
ctxl records get native-object:worker/hello-worker --config-id my-config --pretty
ctxl agents instances hello-worker --config-id my-config --pretty
```

The Worker record's version and pinned image identify what was saved. The record shows the image as a registry path, not under your image host. See [Image References](https://docs.contextual.io/build/contextual-workers/about-workers.md#image-references). Inspect **Workers > hello-worker > Deployment** in the admin console and test the application to confirm what is serving.

## Read Logs

A Worker has two kinds of logs:

* **Container output:** what each container writes to standard output and standard error. Every Worker has container output. Read it with `ctxl workers logs`.
* **Platform-routed logs:** log sessions that a Worker sends to the tenant's log pipeline, the same pipeline that Flows and Agents use. Managed Workers can send them. Read them with `ctxl logs` or the console's **View Worker message logs and sessions** link.

A custom container's output, such as the quickstart's `Handled GET /`, is not sent to the log pipeline. Read it with `ctxl workers logs`. That command also works for managed Workers when you want their raw output.

### Container Output

```sh
ctxl workers logs hello-worker --config-id my-config
```

The command prints the last 100 lines of each container in each replica, then exits. Each line starts with `[pod/container]`. Each replica is a separate pod. The container name is the Compose service name, or `worker` for a single-image Worker.

| Flag | Effect |
| --- | --- |
| `-f`, `--follow` | Keep streaming, including replicas and containers that start later. |
| `-c`, `--container <name>` | Show only this container. Repeat the flag for more. An unknown name returns an error that lists the Worker's containers. |
| `--since <duration>` | Show only lines newer than a duration, such as `30s`, `10m`, or `2h`. |
| `-t`, `--tail <lines>` | Show this many lines from the end of each container's output. The default is 100. Use `-1` for all lines. |
| `-p`, `--previous` | Show each container's previous run, for example the run before a crash and restart. Cannot be used with `--follow`. |
| `--timestamps` | Start each line with its timestamp. |

For example, to follow the output of one service with timestamps:

```sh
ctxl workers logs hello-worker -f -c worker --timestamps --config-id my-config
```

Container output exists only while the replica's pod exists. A deployment or scale-down that removes a replica also removes its output. In follow mode, earlier output from different containers can print out of order. Use `--timestamps` to see the actual order.

Reading container output needs read permission on the Worker.

### Platform-Routed Logs

```sh
ctxl logs hello-worker --since 10m --config-id my-config --pretty
```

`ctxl logs` reads log sessions for the Worker ID from the tenant's log pipeline. A managed Worker in a tenant with its own log topic writes logs to this pipeline. Any worker's raw container output can be read with `ctxl workers logs`.

## Return to a Previous Image

For a single-image Worker, use `workers update` with the earlier pinned image reference:

```sh
ctxl workers update hello-worker \
  --image <previous-image-reference> \
  --config-id my-config \
  --pretty
```

The `workers update` command creates a new Worker version with the earlier image and retains the existing Worker configuration settings. Check the configuration settings for compatibility with the earlier application code. Verify deployment and behavior again. For Compose or managed Workers, retain the Compose definition or managed template selection and options when preparing an update.

## Remove a Worker

```sh
ctxl records delete native-object:worker/hello-worker --config-id my-config
```

The `records delete` command sends the Worker delete request immediately, without a confirmation prompt. Deleting the Worker requests removal of the Worker's deployment. Check the tenant and Worker ID before running `records delete`.
