name: Release on: push: tags: - "v*" workflow_dispatch: inputs: tag: description: "Existing release tag to publish (e.g. v0.31.1). Use to re-run a failed release; npm publish is idempotent per package version." required: true type: string permissions: contents: write env: # Fully-qualified ref used by actions/checkout. For workflow_dispatch we # always force the ref to a tag so non-tag inputs (branches, SHAs) fail to # check out instead of silently publishing untagged code. RELEASE_REF: ${{ github.event_name == 'workflow_dispatch' && format('refs/tags/{0}', inputs.tag) || github.ref }} # Bare tag name (e.g. v0.31.1) for dist-tag selection and Release naming. RELEASE_TAG: ${{ github.event_name == 'workflow_dispatch' && inputs.tag || github.ref_name }} jobs: preflight-tag: name: Release Tag Preflight runs-on: ubuntu-latest permissions: contents: read steps: - name: Validate dispatch input if: github.event_name == 'workflow_dispatch' env: DISPATCH_TAG: ${{ inputs.tag }} run: | set -euo pipefail if [[ ! "$DISPATCH_TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[A-Za-z0-9.+-]+)?$ ]]; then echo "Refusing to dispatch release for '$DISPATCH_TAG'." >&2 echo "Tag input must match v..[-PRERELEASE], e.g. v0.31.1 or v0.32.0-rc.1." >&2 exit 1 fi - uses: actions/checkout@v6 with: fetch-depth: 0 ref: ${{ env.RELEASE_REF }} - name: Verify tag points at merged main or matching maintenance branch env: IS_DISPATCH: ${{ github.event_name == 'workflow_dispatch' }} PUSH_SHA: ${{ github.sha }} EXPECTED_TAG: ${{ env.RELEASE_TAG }} run: | set -euo pipefail # Confirm the checked-out HEAD is exactly the named tag's commit. # actions/checkout was given refs/tags/, but we verify the local # tag ref still resolves and matches HEAD as defense-in-depth. HEAD_SHA="$(git rev-parse HEAD)" TAG_SHA="$(git rev-parse "refs/tags/${EXPECTED_TAG}^{commit}" 2>/dev/null || true)" if [[ -z "$TAG_SHA" ]]; then echo "Refusing to publish: tag '${EXPECTED_TAG}' did not resolve in the checked-out repo." >&2 exit 1 fi if [[ "$HEAD_SHA" != "$TAG_SHA" ]]; then echo "Refusing to publish: HEAD ($HEAD_SHA) does not match refs/tags/${EXPECTED_TAG} ($TAG_SHA)." >&2 exit 1 fi if [[ "$IS_DISPATCH" == "true" ]]; then RELEASE_TAG_COMMIT="$HEAD_SHA" else RELEASE_TAG_COMMIT="$PUSH_SHA" fi export RELEASE_TAG_COMMIT bash scripts/release-tag-preflight.sh ci: name: CI needs: preflight-tag if: github.event_name == 'push' uses: ./.github/workflows/ci.yml publish-npm: name: Publish to npm needs: [preflight-tag, ci] outputs: dist-tag: ${{ steps.dist-tag.outputs.tag }} if: | always() && needs.preflight-tag.result == 'success' && (github.event_name != 'push' || needs.ci.result == 'success') runs-on: ubuntu-latest environment: release permissions: contents: read id-token: write steps: - uses: actions/checkout@v6 with: ref: ${{ env.RELEASE_REF }} fetch-depth: 0 - name: Install pnpm uses: pnpm/action-setup@v5 - name: Set up Node uses: actions/setup-node@v6 with: node-version: 24 cache: pnpm registry-url: https://registry.npmjs.org - name: Install dependencies run: pnpm install --frozen-lockfile - name: Copy viewer static assets run: | mkdir -p packages/viewer-server/static cp packages/ui/static/index.html packages/ui/static/favicon.svg packages/viewer-server/static/ - name: Build run: pnpm run build - name: Determine npm dist-tag id: dist-tag env: RELEASE_TAG: ${{ env.RELEASE_TAG }} run: | set -euo pipefail # Historical tags may contain a helper that silently ignores --from-git. # Refuse stable publication unless that helper supports history-aware routing. if [[ "$RELEASE_TAG" != *-* ]]; then node --input-type=module -e ' const helper = await import("./scripts/release-dist-tag.mjs"); if (helper.supportsGitStableTags !== true) { throw new Error("This tag lacks history-aware dist-tag routing. Backport the release tooling before publishing a stable release."); } ' fi TAG_OUTPUT="$(node scripts/release-dist-tag.mjs "$RELEASE_TAG" --from-git)" if [[ ! "$TAG_OUTPUT" =~ ^tag=(alpha|beta|rc|latest|maintenance-[0-9]+\.[0-9]+)$ ]]; then echo "Invalid or empty npm dist-tag output: $TAG_OUTPUT" >&2 exit 1 fi printf '%s\n' "$TAG_OUTPUT" >> "$GITHUB_OUTPUT" - name: Publish packages env: NODE_AUTH_TOKEN: "" DIST_TAG: ${{ steps.dist-tag.outputs.tag }} run: | set -euo pipefail # Idempotent publish: skip any package whose exact version is already on # the npm registry. Lets us safely re-run after a partial failure # (e.g. transient OIDC fluke) without choking on duplicate versions. publish_if_missing() { local filter="$1" local pkg_dir="$2" local name version view name="$(jq -r .name "${pkg_dir}/package.json")" version="$(jq -r .version "${pkg_dir}/package.json")" view="$(npm view "${name}@${version}" version 2>/dev/null || true)" if [[ -z "${view}" ]]; then sleep 2 view="$(npm view "${name}@${version}" version 2>/dev/null || true)" fi if [[ "${view}" == "${version}" ]]; then echo "skip: ${name}@${version} already published on npm (dist-tag=${DIST_TAG})" return 0 fi echo "publish: ${name}@${version} (dist-tag=${DIST_TAG})" pnpm --filter "${filter}" publish --tag "${DIST_TAG}" --provenance --access public --no-git-checks } # pnpm publish resolves workspace:^ to actual version ranges. # Publish embeddings before packages that consume or launch its pinned version. publish_if_missing "@codemem/embeddings" "packages/embeddings" publish_if_missing "@codemem/core" "packages/core" publish_if_missing "@codemem/mcp" "packages/mcp-server" publish_if_missing "@codemem/server" "packages/viewer-server" publish_if_missing "codemem" "packages/cli" publish_if_missing "@codemem/opencode-plugin" "packages/opencode-plugin" publish_if_missing "@codemem/pi-extension" "packages/pi-extension" - name: Verify latest dist-tag is absent or stable # npm assigns `latest` to a package's first-ever version regardless of # --tag, so a brand-new package whose debut is a prerelease exposes that # prerelease to untagged installs. OIDC trusted publishing only grants # publish, not dist-tag mutation, so this step cannot fix it; it # reports. The manual fix is in docs/versioning.md under "First # publication of a new package". Warning-only until every published # package is clean, then flip continue-on-error to false. continue-on-error: true run: | set -euo pipefail node scripts/release-latest-guard.mjs \ @codemem/embeddings @codemem/core @codemem/mcp @codemem/server codemem @codemem/opencode-plugin @codemem/pi-extension release: name: Create GitHub Release needs: [publish-npm] # Use always() to bypass the implicit success() check, otherwise this job # is skipped on workflow_dispatch reruns where the upstream `ci` job is # skipped — the implicit success() requires all transitive needs to have # succeeded, not just direct ones. if: always() && needs.publish-npm.result == 'success' runs-on: ubuntu-latest permissions: contents: write steps: - uses: actions/checkout@v6 with: ref: ${{ env.RELEASE_REF }} - name: Set up Node uses: actions/setup-node@v6 with: node-version: 24 - name: Create GitHub Release env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} RELEASE_TAG: ${{ env.RELEASE_TAG }} DIST_TAG: ${{ needs.publish-npm.outputs.dist-tag }} run: | set -euo pipefail PRERELEASE_FLAG="" LATEST_FLAG="" if [[ "$DIST_TAG" == maintenance-* ]]; then LATEST_FLAG="--latest=false" fi if [[ "$RELEASE_TAG" == *"-alpha"* ]] || \ [[ "$RELEASE_TAG" == *"-beta"* ]] || \ [[ "$RELEASE_TAG" == *"-rc"* ]]; then PRERELEASE_FLAG="--prerelease" fi if gh release view "$RELEASE_TAG" >/dev/null 2>&1; then echo "Release $RELEASE_TAG already exists" else gh release create "$RELEASE_TAG" \ --generate-notes \ --title "codemem $RELEASE_TAG" \ $LATEST_FLAG \ $PRERELEASE_FLAG fi - name: Create maintenance branch for stable minor release env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | if test -f scripts/release-maintenance-branch.mjs; then node scripts/release-maintenance-branch.mjs fi