# Branches and CI

> Move changes from feature branches to development, qa and main through pull requests, and validate every pull request against the public JSON Schemas before anything reaches an installation.

Source: https://docs.sommatic.ai/en/git-sync/ci/ (updated 2026-11-26)

Git Sync gives you a deployment pipeline made of the tools you already use: branches decide what each installation
can see, pull requests are the review, and CI checks catch broken files before a person previews them in Sommatic AI.

## Branch flow

```text
feature/<change>  ──PR──▶  development  ──PR──▶  qa  ──PR──▶  main
                             │                    │             │
                        dev installation     review / QA   prod installation
```

- Work on a `feature/*` branch and open a pull request to `development`.
- Each installation reads **one** branch, set in its environment file and its connection: for example dev reads
  `development` and prod reads `main`.
- Promote a change with a pull request from `development` to `qa`, and from `qa` to `main`.
- Deploy to an installation only after the pull request into its branch is merged, by
  [previewing and applying](https://docs.sommatic.ai/en/git-sync/preview-and-apply.md) the new head.
- Protect `development`, `qa` and `main`: no direct pushes, required reviews and required checks.

> **Note: Apply follows the branch, not the pull request**
>
> Apply only deploys the head of the branch an installation reads. A commit of a feature branch cannot be applied, so
> the pull request is always the gate.

## What to check on every pull request

| Check                                                                                    | Why                                                      |
| ---------------------------------------------------------------------------------------- | -------------------------------------------------------- |
| Every `sommatic.json` validates against the package schema.                              | The manifest is what Sommatic AI reads first.            |
| Every file listed in a manifest exists and validates against the resource header schema. | It also rejects runtime fields such as `id` or `status`. |
| The header `key` of each file equals its manifest key.                                   | Preview reports a mismatch as an error.                  |
| Keys are unique across the repository.                                                   | One key maps to one resource per installation.           |
| Every environment file validates and declares an id for every key.                       | Apply fails for resources that are not declared.         |
| No `@@secret.REPLACE_ME@@` remains.                                                      | Export leaves it when it could not name a secret.        |

The schemas are public:

- [`https://docs.sommatic.ai/schemas/git-sync/package.schema.json`](https://docs.sommatic.ai/schemas/git-sync/package.schema.json)
- [`https://docs.sommatic.ai/schemas/git-sync/environment.schema.json`](https://docs.sommatic.ai/schemas/git-sync/environment.schema.json)
- [`https://docs.sommatic.ai/schemas/git-sync/resource-header.schema.json`](https://docs.sommatic.ai/schemas/git-sync/resource-header.schema.json)

They use JSON Schema draft 2020-12, so any validator that supports it works.

## A generic validator

This script runs every check above with Node.js and Ajv 8. Save it in your repository, for
example as `tools/validate-sommatic.mjs`:

```javascript
// Usage: node tools/validate-sommatic.mjs <repo-root> <schemas-dir>
import { readFileSync, readdirSync, statSync } from 'node:fs';
import { join, relative, dirname } from 'node:path';
import Ajv2020 from 'ajv/dist/2020.js';

const root = process.argv[2] || '.';
const schemaDir = process.argv[3] || 'schemas';
const ajv = new Ajv2020({ allErrors: true, strict: false });
const load = (path) => JSON.parse(readFileSync(path, 'utf8'));
const validate = {
  package: ajv.compile(load(join(schemaDir, 'package.schema.json'))),
  environment: ajv.compile(load(join(schemaDir, 'environment.schema.json'))),
  resource: ajv.compile(load(join(schemaDir, 'resource-header.schema.json'))),
};

const errors = [];
const check = (kind, file, data) => {
  if (!validate[kind](data)) {
    validate[kind].errors.forEach((error) => errors.push(`${file}: ${error.instancePath || '/'} ${error.message}`));
  }
};

const walk = (folder) =>
  readdirSync(folder).flatMap((name) => {
    const path = join(folder, name);
    if (['.git', 'node_modules'].includes(name)) return [];
    return statSync(path).isDirectory() ? walk(path) : [path];
  });

const files = walk(root).map((path) => relative(root, path));
const ignored = (path) => path.split('/').some((segment) => ['templates', '.github'].includes(segment));
const manifests = files.filter((path) => path.endsWith('sommatic.json') && !ignored(path));
const environments = files.filter((path) => /^environments\/[^/]+\.json$/.test(path));

const keys = new Set();
for (const manifestPath of manifests) {
  const manifest = load(join(root, manifestPath));
  check('package', manifestPath, manifest);
  const packageDir = dirname(manifestPath);
  for (const entry of [...(manifest.resources?.workflows || []), ...(manifest.resources?.knowledge || [])]) {
    if (keys.has(entry.key)) errors.push(`${manifestPath}: duplicated key ${entry.key}`);
    keys.add(entry.key);
    const filePath = join(packageDir, entry.path);
    if (!files.includes(filePath)) {
      errors.push(`${manifestPath}: ${entry.key} points to a missing file ${filePath}`);
      continue;
    }
    const text = readFileSync(join(root, filePath), 'utf8');
    const resource = JSON.parse(text);
    check('resource', filePath, resource);
    if (resource.sommatic?.key !== entry.key) errors.push(`${filePath}: header key is not ${entry.key}`);
    if (text.includes('@@secret.REPLACE_ME@@')) errors.push(`${filePath}: name the secret that replaced @@secret.REPLACE_ME@@`);
  }
}

for (const environmentPath of environments) {
  const environment = load(join(root, environmentPath));
  check('environment', environmentPath, environment);
  for (const key of keys) {
    if (!environment.resources?.[key]) errors.push(`${environmentPath}: resources has no id for ${key}`);
  }
}

if (errors.length) {
  console.error(errors.join('\n'));
  process.exit(1);
}
console.log(`OK: ${manifests.length} package(s), ${keys.size} resource(s), ${environments.length} environment(s)`);
```

The script assumes packages and `environments/` sit at the repository root. If your connection uses a root path, pass
that folder as `<repo-root>`.

## Run it on pull requests

A GitHub Actions workflow that downloads the schemas and runs the validator on every pull request to a deployment
branch:

```yaml
name: Validate Sommatic packages
on:
  pull_request:
    branches: [development, qa, main]

jobs:
  validate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
      - run: npm install --no-save ajv@8
      - name: Download the Git Sync schemas
        run: |
          mkdir -p .sommatic-schemas
          for name in package environment resource-header; do
            curl -fsSL "https://docs.sommatic.ai/schemas/git-sync/$name.schema.json" -o ".sommatic-schemas/$name.schema.json"
          done
      - run: node tools/validate-sommatic.mjs . .sommatic-schemas
```

## Optional: preview the pull request

Static checks cannot know whether each resource exists in the installation or what a change does to the live workflow. A preview can, and it writes nothing. A CI job with a token for the dev installation
can call preview with `ref` set to the pull request's head commit and fail when any item has `unresolved` tokens or
`exists: false`:

```bash
curl -s -X POST "$SOMMATIC_API/platform-infrastructure/git-sync/preview" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN" \
  -H 'Content-Type: application/json' \
  -d '{ "repository_id": "<repository_id>", "path": "support", "ref": "<pull request head commit>", "include_content": false }'
```

Store the token as a CI secret, never in the repository.

## Next steps

- [Playbook for coding agents](https://docs.sommatic.ai/en/git-sync/agents.md): The same flow, written for an agent.
- [Preview and apply](https://docs.sommatic.ai/en/git-sync/preview-and-apply.md): Deploy after the merge.
