Pushed Images (No Repository Access)
Pushed images: sandboxes without repository access
An application whose team gives Cofactor no access to its repository
delivers its sandboxes as images: push your app's Dockerfile and its build
context, and Cofactor builds the image for linux/amd64 (no Docker on your
machine or in CI), or push an image you built yourself. Push with the
application's cofactor.sandbox.yaml whenever you have a new build
(nightly, per release, per merge — your cadence, your CI or your laptop). Each push is a version of the application. A tag names the newest
ready push that carries it: pushing --tag nightly again moves the tag to
the new push, and older pushes stay claimable by their id.
Tag your main builds main (or nightly). These two tags are reserved: once
the application has a push carrying either, its head (what the flow designer,
scheduled runs and claims without --at use) is the newest push tagged main
or nightly, and only those runs move the visual-review baseline. Pull
request and branch pushes are compared against them and change nothing. Until
then the newest push is the head. Claim any other push with
--at <push id or tag>.
Use tags that move (nightly, release, a branch name), not one per push
such as a date. Cofactor keeps the layers of each application's newest push,
its head, and its 10 newest tagged pushes. Older pushes stay claimable, but when the
sandbox platform is updated only kept pushes move to it on their own; an
older one says to push again.
cofactor sandbox validate --upload cofactor.sandbox.yaml cofactor sandbox push --application "Project Tracker" --context . \ --definition cofactor.sandbox.yaml --tag nightly --wait cofactor sandbox envs --application "Project Tracker" cofactor sandbox claim --application "Project Tracker" --environment default --wait
push --context <dir> (the default when no image is given) sends the
directory as docker build would: .dockerignore (or
<Dockerfile>.dockerignore) applies, --file names a Dockerfile other than
<dir>/Dockerfile, --target picks a stage of a multi-stage build, and
--build-arg KEY=VALUE sets build arguments (stored with the push and
visible in the image: never pass a secret as one). Only the files Cofactor
doesn't already hold for your organization are uploaded; Cofactor then
builds the image natively on linux/amd64 and push prints the build log
as it runs. A failed build fails the push with the failing step.
To push an image you built yourself, name it instead:
docker buildx build --platform linux/amd64 -t tracker:sandbox --load . cofactor sandbox push --application "Project Tracker" tracker:sandbox \ --definition cofactor.sandbox.yaml --tag nightly --wait
Either way, push adds Cofactor's layers to your image (its sandbox helper as the
entrypoint, plus the object storage server when the definition declares
object_storage) and a small layer with your definition. A local image is read out of
Docker (docker save), and only the layers Cofactor doesn't already have
for your organization are uploaded (see Fast re-pushes).
Cofactor boots it once until every service is healthy (--wait waits for
that). Then each environment in the definition
becomes a sandbox environment of the application, and each persona one of
its personas. The first claim of an environment builds its ready snapshot
(prepare + seeds, about a minute); later claims copy it in seconds.
Pushes made with a CLI release that adds the test browser, or any later
release, also carry Cofactor's test browser, so a test's browser runs inside
its own sandbox. A push from an older CLI does not carry it, and its tests
use Cofactor's shared browsers instead. Update the CLI
(npm install -g @runcofactor/cli) and push again to get it.
What the image must be
- linux/amd64. Cofactor builds a pushed Dockerfile for it. For an image
you build yourself on an Apple Silicon Mac, build with
--platform linux/amd64;pushrefuses any other platform. - glibc-based with bash (Debian, Ubuntu,
python:*-slim,node:*non-alpine…). Alpine/musl and distroless images do not work: the helper is a glibc binary and every command runs throughbash -lc. If only your final stage is Alpine, keep the build stages and change that oneFROMto its Debian equivalent (node:24-alpine→node:24-bookworm-slim,python:3.12-alpine→python:3.12-slim), withapt-get installin place ofapk add. Native dependencies compiled on Alpine don't run on glibc: install them in a glibc stage rather than copyingnode_modulesor wheels out of an Alpine one. - A WORKDIR other than
/. Services, prepare commands and actions run from it, and the helper gives it to the sandbox user (below). - USER left as root (the sandbox drops to its own user). A push refuses an image, local or built from a Dockerfile, that ends on another USER.
- Complete once built. Nothing builds at claim: everything the app
needs (dependencies, compiled assets, a database server if it uses one)
is in the image. Optional
lifecycle.buildsteps run only onsync --build(thelocaltopic), which needsgitinstalled in the image: without it the build fails withExecutable not found in $PATH: "git".
Fast re-pushes
A push uploads only what changed since an earlier push (in the last 30
days): a Dockerfile push the context files, an image push the layers;
push says how much it sent and how much Cofactor already had. A
Dockerfile push of an unchanged context reuses its earlier build. These
keep a re-push small:
-
Order the Dockerfile so what changes most comes last: system packages, then the dependency manifest and lockfile and the install step, then your source. A code change then rebuilds, and uploads, only the last layers:
dockerfileFROM python:3.12-slim RUN apt-get update && apt-get install -y --no-install-recommends curl jq WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . .
-
In CI, keep the build cache between runs. A runner that starts with an empty cache rebuilds every step, and every rebuilt layer is new to Cofactor even when nothing in it changed. With GitHub Actions, build with Docker's official actions, which pass the Actions cache to buildx for you:
yaml- uses: docker/setup-buildx-action@v4 - uses: docker/build-push-action@v7 with: context: . platforms: linux/amd64 load: true tags: app:sandbox cache-from: type=gha cache-to: type=gha,mode=max(or
type=registrywith a cache image of your own). Docker's own actions are pinned by major version here, likeactions/checkout; pin them to a commit if your policy requires it. A rawdocker buildx build ... --cache-from type=ghain arun:step can't see the Actions cache token and skips the cache without a warning. If you must use one, add the community actioncrazy-max/ghaction-github-runtimebefore it, pinned to a full commit (@04d248b84655b509d8c44dc1d6f990c879747487, v4.0.0): it exposes the Actions runtime token to every later step of the job. -
For compiled stacks (.NET, Java, Rust), build outside Docker and keep the Dockerfile thin.
RUN --mount=type=cache(NuGet, Maven, cargo, incrementalobj/ortarget/directories) is not exported by--cache-to type=gha, so inside a Dockerfile those caches are empty on every CI run and the whole compile reruns. Compile on the runner withactions/cachefor the compiler's incremental state, and have the Dockerfile onlyCOPYthe published output onto a base image holding what rarely changes (database server, runtimes, seed data; rebuild it only when its inputs change). Three details decide whether the re-push is actually small:- Incremental compilers (MSBuild, make, cargo) compare file mtimes.
A fresh
git clonestamps every file "now" and invalidates the cached state. Before compiling, set tracked files to an old date (git ls-files -z | xargs -0 touch -h -d '2000-01-01'), then touch the files changed since the commit you last built. Keep its SHA in the cache entry, check out withfetch-depth: 0so it is present, and skip this on a cache miss (that build is full anyway):git diff -z --name-only --diff-filter=d <last-sha> HEAD | xargs -0 -r touch -h. - Layers are digested by content including mtimes, and the parent
directories a COPY creates can get the build time, so identical
files can yield a new layer digest on every build. Stage the
output under its final path (
staged/opt/app/...), pin every mtime in it including the directories (find staged -exec touch -h -d '2000-01-01' {} +), and useCOPY --link staged/ /(BuildKit). - Split third-party files from your own into separate COPYs so a code change rewrites only the small one.
- Incremental compilers (MSBuild, make, cargo) compare file mtimes.
A fresh
CI authenticates with an API key (COFACTOR_API_TOKEN). Give it only
what the job does: sandbox:read, sandbox:publish and property:read
to push (plus property:create if CI's push may be the first, which
creates the application); add sandbox:create and sandbox:delete to claim and destroy,
sandbox:run to run actions, and sandbox:exec to run shell commands
(not included in sandbox:*). Create it from the CLI (the secret prints
once): cofactor api-key create --name "CI image push" --scope sandbox:read --scope sandbox:publish --scope property:read.
Push main builds with --tag main (or nightly). For pull requests, push
each with its own tag and claim it by that tag:
cofactor sandbox push --application "Project Tracker" app:sandbox --tag pr-123 --wait cofactor sandbox claim --application "Project Tracker" --environment default --at pr-123 --wait
A pull request's push shares every layer below the source with main's.
Build performance
A push that runs on every pull request has to be fast to be useful. Fast re-pushes (above) keep the upload small; this section keeps the build short.
-
Set a budget and measure it. A push meant to run on every pull request should go from commit to pushed image in about 10 minutes. Time each stage with
docker buildx build --progress=plain ..., which printsDONE <seconds>after every step (--metadata-file build.jsonrecords the result, such as the image digest, not per-step times). Then read whatcofactor sandbox pushreports as sent versus already present. A layer that rebuilds on a commit that didn't touch its inputs is a cache bug: fix it before tuning anything else. -
Copy only what a stage reads. A stage that copies the whole repository (
COPY . .orCOPY source/ ./) before a compile, codegen or schema step reruns on every commit, including commits that only touch tests or docs. Copy the directories that stage reads, and restore or install in its own step before the source. The manifests to copy first are csproj, sln,Directory.*.props,NuGet.config,package.jsonand its lockfile. For .NET, restore from the project file you publish:dockerfileWORKDIR /src COPY Directory.Build.props Directory.Packages.props NuGet.config ./ COPY src/Acme.Api/Acme.Api.csproj src/Acme.Api/ COPY src/Acme.Core/Acme.Core.csproj src/Acme.Core/ RUN dotnet restore src/Acme.Api/Acme.Api.csproj COPY src/ src/ RUN dotnet publish src/Acme.Api -c Release --no-restore -o /out
A source change skips the restore. A change under
tests/ordocs/skips both, because those directories are never copied in. RestoringAcme.slninstead would need every project in it, test projects included. -
Keep expensive, slow-changing state in a base image: the database server with its schema and seed data, language runtimes, browsers, and
node_modulesfor the test runner. Key the base image's tag on a hash of its inputs, here the schema directory and the seed scripts (use your own paths), and build it only when that tag doesn't exist:bashhash=$(git ls-files -z db/schema db/seed | xargs -0 sha256sum | sha256sum | cut -c1-12) base=registry.example.com/acme/base:$hash docker manifest inspect "$base" >/dev/null 2>&1 || docker buildx build -f Dockerfile.base -t "$base" --push .
Pull request images are then a thin layer on top:
FROMthe base, copy the built app. -
Use the build machine. Parallelism caps meant for a laptop slow a CI build down: MSBuild
-m:2, Angular or webpack worker limits, a fixed--jobs. Remove them or size them to the runner's cores (nproc). -
When you compile on the runner, keep the framework's build caches in
actions/cachenext to the package caches:.angular/cache, Next.js.next/cache, Gradle's~/.gradle/caches. Without them every run recompiles what it compiled yesterday. (Inside a Dockerfile these directories are not exported by--cache-to type=gha; see the compiled stacks bullet above.) -
Test discovery and listing steps scale with the suite (
dotnet test --list-testsis one). Run them on the test files only, in their own stage, so an app-only change doesn't rerun them and a test-only change doesn't recompile the app.
Nightly push from GitHub Actions
A complete .github/workflows/cofactor-nightly.yml for the example
application "Acme Tracker": it builds the image every night (and on
demand), pushes it tagged nightly, then runs your suite on that push.
Replace the application and suite names with yours.
Before the first run, create the CI key (see above) and store it as the
repository secret COFACTOR_API_TOKEN. For the last step add --scope test-run:run --scope test-run:read; without that step the push scopes
are enough.
name: cofactor-nightly
on:
schedule:
- cron: "0 3 * * *"
workflow_dispatch:
concurrency:
group: cofactor-nightly
cancel-in-progress: false
permissions:
contents: read
jobs:
nightly:
runs-on: ubuntu-latest
timeout-minutes: 180
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v4
with:
node-version: 22
- run: npm install -g @runcofactor/cli
- uses: docker/setup-buildx-action@v4
- name: Build the image
uses: docker/build-push-action@v7
with:
context: .
platforms: linux/amd64
load: true
tags: acme-tracker:sandbox
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Check the definition
run: cofactor sandbox validate --upload cofactor.sandbox.yaml
- name: Push the image
env:
COFACTOR_API_TOKEN: ${{ secrets.COFACTOR_API_TOKEN }}
run: |
cofactor sandbox push --application "Acme Tracker" acme-tracker:sandbox \
--definition cofactor.sandbox.yaml --tag nightly --wait
- name: Run the suite on the new push
env:
COFACTOR_API_TOKEN: ${{ secrets.COFACTOR_API_TOKEN }}
run: cofactor test-suite run --suite "Acme Tracker nightly"
The CLI reads COFACTOR_API_TOKEN from the environment (no login step);
only the steps that call Cofactor get it, and validate needs none.
The build step's cache settings are the ones under Fast re-pushes.
--wait follows the import for up to 50 minutes and fails the job if
Cofactor cannot boot the image. A push still importing after 50
minutes is not a failure: the job moves on and the suite runs on the
previous head. The suite runs on the application's head: the newest push
tagged main or nightly. Start the workflow by hand with "Run
workflow" (the workflow_dispatch trigger) to test a change to this
file. timeout-minutes covers a build, the longest import wait and the
suite; raise it if your suite runs longer.
The last step is optional: a suite can also run on a Cofactor schedule.
Making your image syncable
cofactor sandbox sync writes your working tree into a claimed sandbox
(see guide v2 local). For a pushed image it syncs the directory holding
cofactor.sandbox.yaml into the image's WORKDIR, so the image must keep
that directory's files at WORKDIR in the same layout. The rule: a
syncable image has your definition file at its WORKDIR, as COPY . .
gives you.
WORKDIR /app COPY . . RUN npm ci && npm run build
A multi-stage build that copies only some directories works too, as long as each lands at the same relative path under WORKDIR and the definition file is copied in:
WORKDIR /app COPY --from=build /src/api /app/api COPY cofactor.sandbox.yaml /app/cofactor.sandbox.yaml
Files the image doesn't ship (sources it compiles away, docs) are fine:
sync writes them as new files and nothing runs them.
Compiled output isn't rebuilt in the sandbox (nothing builds at push or claim; sync --build reruns a declared lifecycle.build):
build it locally, then sync the result to where the image serves it:
npm run build cofactor sandbox sync <id> dist --to public
push checks the rule on the local image and says one of:
Sync: syncable. Sync: syncable (the image is older than your working tree, which is fine). Sync: not syncable: cofactor.sandbox.yaml isn't at /app/cofactor.sandbox.yaml (...) Sync: not checked (...) (Docker, or git, couldn't answer)
It never stops a push. It reads the definition at your WORKDIR, not the
copy push adds for Cofactor itself.
How your image runs in a sandbox
- Its ENTRYPOINT and CMD are NOT used. Start each process yourself with a
services.<name>.runcommand (often your entrypoint script). - Its ENV and WORKDIR ARE applied, as
docker runwould, its PATH included: commands run through abash -lclogin shell, and the image's own PATH is put back in front of whatever the login profile sets. - Leave the image's USER as root (or unset). The sandbox starts as root
and drops to uid 1000 itself; an image that ended on another
USERhas failed to boot with a permission error on/etc/cofactor. - Everything — services, prepare commands, actions — runs as an
unprivileged user with uid 1000 (the sandbox adds that account if your
image has none). The WORKDIR tree is made theirs; anything the app
writes must be under the WORKDIR or
/tmp, not elsewhere in a root- owned path. - One machine, no Docker inside: a database is a process you run as a
service (Postgres installed in the image, or SQLite), not a container.
For S3 or GCS, declare
object_storageand the sandbox runs the server for you (see theobject-storagetopic). - Each public port gets its own https URL, unique per sandbox. Its
origin is a param —
params: { web_url: { port: web } }— that you pass to the app throughenv(e.g.PUBLIC_URL: ${params.web_url}).
The definition for pushed images
The same cofactor.sandbox.yaml as for a repository (see the
definition topic), minus installing the toolchain: no setup. lifecycle.build
and after_build are optional and run only on sync --build. A minimal one for the example app,
built as one image that serves its UI and API on port 8080:
version: 1
resources: { vcpus: 2, memory_mib: 4096 }
ports:
web: { port: 8080, access: public }
params:
web_url: { port: web }
env:
PUBLIC_URL: ${params.web_url}
services:
app:
run: exec ./start.sh
health: { http: 8080, path: /health }
reload_on_prepare: true
personas:
owner: { email: owner@example.test }
lifecycle:
prepare:
- bash sandbox/personas.sh
actions:
create-workspace:
description: A new workspace, created by the given persona, who becomes its owner.
run: sandbox/actions/create-workspace.sh
input:
name: { type: string, required: true }
as: { type: persona, required: true }
output:
workspace_id: { type: string }
environments:
default:
description: Workspace "Acme", owned by the owner persona.
seeds:
- id: acme
action: create-workspace
input: { name: Acme, as: owner }
From here, add what your tests need, in the order "What to build first"
in the seeds-and-actions topic gives: a persona per role, an
environment per plan or flag combination, and actions for the core
objects.
Keep the file anywhere in your repo: validate --upload <file> and
push --definition <file> read it from where you point them, and the
push puts it in the image. Paths inside it (run: of actions, prepare
commands) are relative to the WORKDIR, so ship your sandbox scripts in
the image (e.g. COPY sandbox/ sandbox/).
Services start when the image is first booted, BEFORE any sandbox exists,
with params at placeholder values. A service that needs the sandbox's own
values (its URL, a {secret: true} param) or reads a secret sets
reload_on_prepare: true: it is started (and restarted) by each
sandbox's prepare instead. In doubt, set it on your app's service.
health passes when the http path answers below 400 (or the command
exits 0) within 5 minutes of the service starting.
Personas: making the logins work
Cofactor generates a fresh password for every persona when a snapshot is
built; every sandbox copied from it has the same ones. Your
lifecycle.prepare commands (which also run on each copy) receive them as COFACTOR_PERSONAS, a
JSON map { "<persona>": { "email": "...", "password": "..." } }, and
must make each login work in your app: create or update that user with
that password (through your app's API, its CLI, or its database). A
persona whose login doesn't work breaks every test that signs in as it.
A persona's email is yours to choose: pick an account your app already
has (its seed data) and set its password, or have prepare create it.
Cofactor's test runs then sign in with these logins automatically, as
the application's persona of the same name.
Prepare runs while your app is up: the reload_on_prepare services are
running and healthy (restarted first when their environment changed),
the others have run since the image was booted. So write the logins
somewhere the running app reads on every sign-in (its database, or its
own API). If your app loads its accounts once at startup (a file or
config read into memory), change it to read them per request, or the new
passwords won't take. For an app whose logins are fixtures, a common fix
is a small JSON file the prepare script writes from COFACTOR_PERSONAS
and the app checks on each sign-in before its fixtures.
Actions and seeds
An action is a script in the image with typed input and output (the
seeds-and-actions topic covers designing them). Inputs arrive as
INPUT_<NAME> env vars (a persona input carries the persona's name,
e.g. owner), alongside the definition's env:, every param as
COFACTOR_PARAM_<NAME>, and COFACTOR_PERSONAS. The script prints its
result as a JSON object on the LAST stdout line that starts with {
(send everything else to stderr). It runs inside the sandbox, so it
reaches your app on localhost. An environment's seeds call actions in
order, once per version, before the snapshot every claim copies;
${<step id>.<output>} passes one step's output to a later step. Run
one against a live sandbox with cofactor sandbox run <id> <action> --input name=... --input as=owner.
An action that signs in as a persona and creates data through the app's
own API (here a cookie-session JSON API on port 8080; the port, paths and
JSON fields are the example app's, use your app's). It needs jq and
curl, which slim base images lack: add them to your Dockerfile
(apt-get install -y --no-install-recommends jq curl).
#!/usr/bin/env bash
# sandbox/actions/create-workspace.sh
set -euo pipefail
login=$(jq -c --arg p "$INPUT_AS" '.[$p] | {email, password}' <<<"$COFACTOR_PERSONAS")
jar=$(mktemp)
curl -fsS -c "$jar" -H 'content-type: application/json' \
-d "$login" http://127.0.0.1:8080/api/auth/login >&2
id=$(curl -fsS -b "$jar" -H 'content-type: application/json' \
-d "$(jq -nc --arg n "$INPUT_NAME" '{name: $n}')" \
http://127.0.0.1:8080/api/workspaces | jq -er '.id')
jq -nc --arg id "$id" '{workspace_id: $id}'
Troubleshooting
-
pushsays the image is not linux/amd64, or its WORKDIR can't be used: rebuild as it says. -
The push fails with "Cofactor could not start this image": a service exited or never turned healthy. The message ends with that service's log. To reproduce, open a shell in the image —
bashdocker run --rm -it --entrypoint bash <image>
— and run that service's
runcommand from the WORKDIR. In the sandbox it runs as uid 1000 with the WORKDIR made theirs; plaindocker run --user 1000leaves the WORKDIR root-owned, so a write failure there is not the sandbox's. -
A claim fails in prepare or a seed: the error carries the command's output. Fix the script, rebuild the image and push again.
-
To try prepare and actions before pushing, run the app as usual and call your scripts in it with the variables they read (a sandbox also sets the definition's
env:and runs them as uid 1000):bashdocker run -d --name try -p 8080:8080 <image> docker exec -e COFACTOR_PERSONAS='{"owner":{"email":"owner@example.test","password":"pw-1"}}' try bash sandbox/personas.sh docker exec -e COFACTOR_PERSONAS='…' -e INPUT_NAME=Acme -e INPUT_AS=owner try bash sandbox/actions/create-workspace.sh(
docker execstarts in the image's WORKDIR.) -
cofactor sandbox logsanddiagnosticsare for the older sandboxes; for these,cofactor sandbox show <id>has the status and any error. -
"push the image again": Cofactor's sandbox helper changed since that push; a fresh
cofactor sandbox pushpicks up the current one. -
A layer larger than 5 GB is refused; split or slim it.
-
Every push uploads the whole image: the build cache is missing (see Fast re-pushes), or the Dockerfile copies the source before installing dependencies.