Docs/Build from GitHub
#

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.

Set up sandboxes from GitHub

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

  1. Install the GitHub App. You must be an organization admin.

    Open Settings, then GitHub, select Install GitHub App and grant it your repository.

  2. Plan before you build. Your agent reads your code and writes one plan. You approve it once.

    bash
    cofactor sandbox guide plan
    
  3. Write cofactor.sandbox.yaml at the repository root. It says how to start the app, which logins to create, and what data to seed through your app's API.

    yaml
    version: 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.

  4. Check the file. This runs offline.

    bash
    cofactor sandbox validate
    
  5. Connect the repository. Commit the file to your default branch, then run this.

    bash
    cofactor sandbox connect --application "Project Tracker" --repo acme/project-tracker
    
  6. Claim and prove it.

    bash
    cofactor 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

bash
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

Advanced

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.