Build from GitHub
Connect a GitHub repository and Cofactor builds a sandbox of your app from every commit, with a preview on every pull request.
Cofactor builds sandboxes (live copies of your app, also called test environments) straight from your repository. A cofactor.sandbox.yaml file in the repo says how to build, start and seed the app. You can claim any commit, and every pull request gets a preview.
To give Cofactor no repository access, build from a pushed image instead. The examples use an invented app, Project Tracker, with a web front end on port 3000.
Connect your repository
-
Install the GitHub App. You must be an organization admin.
Open Settings, then GitHub, select Install GitHub App and grant it your repository.
-
Plan before you build. Your agent reads your code and writes one plan. You approve it once.
bashcofactor sandbox guide plan
-
Write
cofactor.sandbox.yamlat the repository root. It says how to start the app, which logins to create, and what data to seed through your app's API.yamlversion: 1 resources: { vcpus: 2, memory_mib: 4096 } setup: | apt-get update -qq apt-get install -y -qq nodejs npm jq curl >/dev/null ports: web: { port: 3000, access: public } params: web_url: { port: web } env: PUBLIC_URL: ${params.web_url} services: web: run: cd web && exec npm start health: { http: 3000, path: / } after_build: true reload_on_prepare: true personas: owner: { email: owner@example.test } member: { email: member@example.test } lifecycle: build: - name: install run: npm ci inputs: [package-lock.json, "**/package.json"] prepare: - bash sandbox/personas.sh actions: create-organization: description: A new organization, created by the given persona. run: sandbox/actions/create-organization.sh input: name: { type: string, required: true } as: { type: persona, required: true } output: organization_id: { type: string } environments: default: description: Organization "Acme", owned by the owner persona. seeds: - { id: acme, action: create-organization, input: { name: Acme, as: owner } }Seed data and logins explains personas,
prepare, actions and seeds. The definition reference lists every field. -
Check the file. This runs offline.
bashcofactor sandbox validate
-
Connect the repository. Commit the file to your default branch, then run this.
bashcofactor sandbox connect --application "Project Tracker" --repo acme/project-tracker
-
Claim and prove it.
bashcofactor sandbox claim --application "Project Tracker" --wait cofactor sandbox verify-env --application "Project Tracker" cofactor sandbox destroy <sandbox id>
The first claim of a commit takes longest because it builds everything. Later claims copy the snapshot.
The claim output for a sandbox: ready in 25 seconds, the web and emulation URLs, an admin and a manager login with the passwords masked, and the seed output.
Check it worked
cofactor sandbox envs --application "Project Tracker"
envs shows a current version and your environments. The claimed URL serves the app over https. Each login signs in with its role and the seeded data is there. verify-env claims each environment, runs its seeds and destroys the copy. Once your definition has an authentication script, it also prints signed in for each persona.
Go deeper
- Preview environments
- Emulators
- Seed data and logins
- Reference:
cofactor.sandbox.yaml, Sandbox CLI, GitHub integration
Build layers, monorepos, versions and common errors.
Build layers. setup is a root shell script that installs your toolchain. It reruns only when setup, resources or ports change. lifecycle.build steps rerun only when their inputs changed, so list only the lockfile for an install step. after_build: true holds a service until the build finishes. Commands run from the definition's directory. A value baked into build output can't vary by environment.
Monorepos. Add --dir apps/tracker to connect. Each definition directory is its own application.
Secrets. Set a test key in your own shell and never in chat: printf %s "$KEY" | cofactor sandbox secret set NAME --application "Project Tracker".
Versions. A claim without --at copies the default branch's snapshot. Cofactor rebuilds it every 6 hours while the environment is in use, and builds the head when it is more than 12 hours old. --at <40-character sha> claims a commit. cofactor sandbox refresh --application "Project Tracker" --wait moves the head now.
Errors. validate names the field to fix. A failed claim shows the script's stderr. If a persona can't sign in, make prepare set the login and make the app read accounts on each sign-in.