# Release and npm Trusted Publishing

This repository publishes the package named by `package.json` through the GitHub Actions workflow at `.github/workflows/publish.yml`.

The current package identity is the unscoped `pi-herdr-orchestration`. Do not run trust commands against a different scoped name unless `package.json`, documentation, and the release plan have been changed together.

## One-time prerequisites

1. Enable account-level two-factor authentication on npm.
2. Authenticate interactively from the maintainer machine:

   ```bash
   npm login --auth-type=web --registry=https://registry.npmjs.org/
   npm whoami --registry=https://registry.npmjs.org/
   ```

   `npm auth login` is not a login command; `npm auth` is an alias for package-owner operations.
3. Ensure the intended package name and version are committed on `dev` before the release merge. The workflow rejects a tag that does not equal `v<package.json version>`.

## Package bootstrap (one-time exception)

npm requires the package to exist before a trusted publisher can be configured, so the first version of a new package is the single documented exception to the rule that publishing runs only from the tag-triggered workflow. For `pi-herdr-orchestration` this step is already complete and must not be repeated. Bootstrap a first version once, from a clean, reviewed release checkout using the interactive npm login above:

```bash
PACKAGE=$(node -p "require('./package.json').name")
VERSION=$(node -p "require('./package.json').version")
npm pack --dry-run
npm publish --access public
npm view "$PACKAGE@$VERSION" version
```

This one-time bootstrap is the only local `npm publish` this repository permits, and it exists only because npm cannot grant a trusted publisher to a package that does not exist yet. Every later release publishes from the tag-triggered workflow.

After the first version is visible, configure GitHub Actions as the trusted publisher. This command requires npm 11.15+, account-level 2FA, and write access:

```bash
npm install --global 'npm@^11.15.0'
npm trust github "$(node -p "require('./package.json').name")" \
  --file publish.yml \
  --repo darkrei08/pi-herdr-orchestration \
  --allow-publish \
  --yes
npm trust list "$(node -p "require('./package.json').name")"
```

Do not create a bypass-2FA granular access token for this. GitHub's npm security changes restrict those tokens for account/package management and are deprecating their use for direct publishing; OIDC is the durable path.

## Branch protection

`main` and `dev` are protected on GitHub so the policy above is enforced by the host, not by habit. Both carry the same rules:

- required status checks `test (ubuntu-latest)`, `test (macos-latest)` and `test (windows-latest)`, with "Require branches to be up to date before merging";
- no force push, no branch deletion;
- linear history is not required, because a deliberate `--no-ff` merge from `dev` to `main` is allowed;
- administrators keep the bypass, because the documented release path is a direct fast-forward of `dev` into `main` after the checks pass. Tighten this to PR-only by setting `enforce_admins` to `true` and requiring pull request reviews; do that only together with a change to this runbook.

Reproduce or audit the settings with:

```bash
gh api repos/OWNER/REPOSITORY/branches/main/protection
gh api repos/OWNER/REPOSITORY/branches/dev/protection
```

## Normal releases

The integration branch is `dev` and the release branch is `main`. A release is the moment `dev` becomes `main`; before that moment nothing on `main` may exist that `dev` lacks.

1. Update `package.json` and `CHANGELOG.md` on `dev`, through a task worktree and a task branch merged into `dev` after that branch's checks pass.
2. Run the repository checks on the merged `dev` commit:

   ```bash
   npm test
   node scripts/check.mjs
   npm pack --dry-run
   git diff --check
   ```

3. Assert the integration branch contains the release branch:

   ```bash
   git fetch origin
   git log --oneline origin/dev..origin/main
   ```

   Any output is a defect: reconcile it before releasing, never tag over it.

4. Merge `dev` into `main` by fast-forward or an explicit `--no-ff` merge commit. Never squash and never rebase-merge `dev` into `main`: both rewrite the commits `main` is supposed to contain and orphan the release tag from `dev`.
5. Create and push an annotated tag matching the package version:

   ```bash
   VERSION=$(node -p "require('./package.json').version")
   git tag -a "v$VERSION" -m "Release v$VERSION"
   git push origin "v$VERSION"
   ```

6. The workflow validates the tag/version pair, creates or updates the GitHub Release, and runs `npm publish --access public --provenance` using GitHub OIDC. No `NPM_TOKEN` secret or local publish is used for steady-state releases.
7. Verify the public package:

   ```bash
   npm view "$(node -p "require('./package.json').name")@$(node -p "require('./package.json').version")" version
   ```

### Hotfix releases

Cut the fix in its own worktree from `origin/main`. This hotfix is the one task type that starts from the release baseline instead of `origin/dev`, because the released code must not wait for `dev`. Merge it to `main`, tag the patch release, then merge `main` back into `dev` in the same session and assert that `git log --oneline origin/dev..origin/main` prints nothing. A hotfix left only on `main` is drift, and the next release silently reverts it.

## Reusable setup for another npm project

Replace the package and repository values, then repeat this sequence:

```bash
npm login --auth-type=web --registry=https://registry.npmjs.org/
npm whoami --registry=https://registry.npmjs.org/
npm install --global 'npm@^11.15.0'

PACKAGE=$(node -p "require('./package.json').name")
npm trust github "$PACKAGE" \
  --file publish.yml \
  --repo OWNER/REPOSITORY \
  --allow-publish \
  --yes
npm trust list "$PACKAGE"
```

A brand-new package still needs its first version on the registry before this trusted-publisher setup can be created. That first version is the one-time bootstrap exception documented in [Package bootstrap](#package-bootstrap-one-time-exception); after it, every version publishes from the tag-triggered workflow and never from a maintainer shell.

The project's workflow must also contain:

```yaml
permissions:
  contents: read
  id-token: write

- uses: actions/setup-node@v4
  with:
    node-version: 22
    registry-url: https://registry.npmjs.org

- run: npm install --global 'npm@^11.15.0'
- run: npm publish --access public --provenance
```

Keep the one-time bootstrap separate from normal releases. Never commit `.npmrc`, tokens, OTPs, or credentials. Never paste them into issues, logs, or chat.
