From 8db50791d6ae175e3988cd9e03a3d19e5536ff1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Almeida?= Date: Sun, 3 May 2026 17:46:48 +0100 Subject: [PATCH] feat(release): unified vX.Y.Z release flow with brew + npm sibling channels MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GitHub release artifacts become the canonical build output. npm and Homebrew are independent sibling channels — both consume the same GitHub release tarballs; neither depends on the other. Workflow changes (.github/workflows/release.yml): - Trigger is now workflow_dispatch only (maintainer-only, gated by repo write access). Inputs: bump dropdown (patch/minor/major) + dry_run checkbox. - Single job graph orchestrates everything in one run: prepare (bump, PR, merge, tag) → build-web/build-cli → build-bundles (5 platforms) → docker → publish-release → publish-npm + update-homebrew-tap. - The prepare job inlines version bump, optionalDependencies sync, CHANGELOG generation, PR create + squash-merge, and tag push. Replaces what release-pr.sh used to do locally. - Tag-push trigger removed; everything runs from the dispatch flow. New scripts (called by the workflow): - package-cli.sh: builds + esbuild-bundles the CLI for inclusion in release bundles (cli/bin/cli.js + cli/dist/cli.bundle.js + package.json). - package-npm-runtime.sh: generates per-platform @kdlbs/runtime-* npm packages from release bundles. Sets npm os/cpu fields so npm 7+ skips non-matching platforms during npx kandev install. - publish-npm.sh: verifies GitHub assets exist, downloads them, repackages, publishes all 5 @kdlbs/runtime-* packages + main kandev@X.Y.Z. - update-homebrew-tap.sh: clones kdlbs/homebrew-kandev via SSH using the HOMEBREW_TAP_DEPLOY_KEY repo secret, generates Formula/kandev.rb pointing at the GitHub release assets, pushes directly to main. Removed scripts: - release.sh and release-pr.sh — both are redundant with the new workflow. Required CI secrets: - NPM_TOKEN: granular access token with publish access to kandev + @kdlbs/* - HOMEBREW_TAP_DEPLOY_KEY: SSH private key for kdlbs/homebrew-kandev --- .github/workflows/release.yml | 318 +++++++++- scripts/release/package-cli.sh | 41 ++ scripts/release/package-npm-runtime.sh | 110 ++++ scripts/release/publish-npm.sh | 128 ++++ scripts/release/release-pr.sh | 812 ------------------------- scripts/release/release.sh | 648 -------------------- scripts/release/update-homebrew-tap.sh | 186 ++++++ 7 files changed, 768 insertions(+), 1475 deletions(-) create mode 100755 scripts/release/package-cli.sh create mode 100755 scripts/release/package-npm-runtime.sh create mode 100755 scripts/release/publish-npm.sh delete mode 100755 scripts/release/release-pr.sh delete mode 100755 scripts/release/release.sh create mode 100755 scripts/release/update-homebrew-tap.sh diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5a1ab444a0..686e787a74 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,20 +1,217 @@ name: release +# Maintainer-only release workflow. +# Trigger: GitHub Actions UI → "Run workflow" (workflow_dispatch). +# +# Flow (one click → everything below): +# 1. prepare — bump version, regen CHANGELOG, open + merge release PR, push tag vX.Y.Z +# 2. build-web — build Next.js standalone bundle +# 3. build-cli — build + esbuild-bundle CLI +# 4. build-bundles — assemble bin/ + web/ + cli/ tarballs for 5 platforms +# 5. docker — build & push multi-arch container image to GHCR +# 6. publish-release — create GH release with .tar.gz + .sha256 assets + auto-generated notes +# 7. publish-npm — publish kandev@X.Y.Z + @kdlbs/runtime-*@X.Y.Z to npmjs +# 8. update-homebrew-tap — push updated Formula/kandev.rb to kdlbs/homebrew-kandev +# +# Required secrets: +# NPM_TOKEN — npm publish token with access to `kandev` and `@kdlbs/*` +# HOMEBREW_TAP_DEPLOY_KEY — SSH private key for kdlbs/homebrew-kandev deploy key + on: - push: - tags: - - "v*" workflow_dispatch: + inputs: + bump: + description: "Version bump" + required: true + type: choice + default: minor + options: + - patch + - minor + - major + dry_run: + description: "Dry run (no PR, tag, npm publish, or tap update)" + type: boolean + default: false permissions: contents: write packages: write + pull-requests: write jobs: + prepare: + name: Prepare release (bump version, PR, tag) + runs-on: ubuntu-latest + outputs: + version: ${{ steps.compute.outputs.version }} + tag: ${{ steps.compute.outputs.tag }} + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + token: ${{ secrets.GITHUB_TOKEN }} + + - uses: pnpm/action-setup@v4 + with: + version: "9" + + - uses: actions/setup-node@v6 + with: + node-version: "24" + + - name: Install git-cliff + run: | + curl -sSL "https://github.com/orhun/git-cliff/releases/download/v2.6.1/git-cliff-2.6.1-x86_64-unknown-linux-gnu.tar.gz" \ + | tar -xz -C /tmp + sudo mv /tmp/git-cliff-2.6.1/git-cliff /usr/local/bin/ + git-cliff --version + + - name: Configure git identity + run: | + git config user.email "release-bot@kandev" + git config user.name "kandev release bot" + + - name: Compute next version + id: compute + env: + BUMP: ${{ inputs.bump }} + run: | + set -euo pipefail + PKG_VERSION="$(node -p "require('./apps/cli/package.json').version")" + + # Find latest git tag, normalizing legacy two-part tags (vM.m → M.m.0) + LATEST_TAG="$(git tag --list 'v*' | python3 -c " + import sys, re + best = (0, 0, 0) + for t in (l.strip() for l in sys.stdin if l.strip()): + s = t.lstrip('v') + m3 = re.match(r'^(\d+)\.(\d+)\.(\d+)$', s) + m2 = re.match(r'^(\d+)\.(\d+)$', s) + if m3: + v = tuple(int(x) for x in m3.groups()) + elif m2: + v = (int(m2.group(1)), int(m2.group(2)), 0) + else: + continue + if v > best: + best = v + print(f'{best[0]}.{best[1]}.{best[2]}') + ")" + + # Use the higher of package.json version and the latest tag as baseline + BASELINE="$(python3 -c " + a = '$PKG_VERSION'.split('.') + b = '$LATEST_TAG'.split('.') + ai = tuple(int(x) for x in a) + bi = tuple(int(x) for x in b) + print('$PKG_VERSION' if ai >= bi else '$LATEST_TAG') + ")" + + IFS=. read -r MAJ MIN PAT <<<"$BASELINE" + case "$BUMP" in + patch) NEXT="${MAJ}.${MIN}.$((PAT + 1))" ;; + minor) NEXT="${MAJ}.$((MIN + 1)).0" ;; + major) NEXT="$((MAJ + 1)).0.0" ;; + *) echo "Invalid bump '$BUMP'" >&2; exit 1 ;; + esac + + TAG="v${NEXT}" + echo "Current package.json version: $PKG_VERSION" + echo "Latest git tag (normalized): $LATEST_TAG" + echo "Baseline: $BASELINE" + echo "Bump: $BUMP" + echo "Next version: $NEXT" + echo "Next tag: $TAG" + echo "version=$NEXT" >> "$GITHUB_OUTPUT" + echo "tag=$TAG" >> "$GITHUB_OUTPUT" + + # Sanity: tag must not already exist + if git rev-parse "$TAG" >/dev/null 2>&1; then + echo "Tag $TAG already exists locally" >&2 + exit 1 + fi + if git ls-remote --tags origin "refs/tags/$TAG" | grep -q .; then + echo "Tag $TAG already exists on origin" >&2 + exit 1 + fi + + - name: Bump version + optionalDependencies + env: + NEXT: ${{ steps.compute.outputs.version }} + run: | + set -euo pipefail + (cd apps/cli && npm version --no-git-tag-version "$NEXT") + node -e " + const fs = require('fs'); + const path = 'apps/cli/package.json'; + const pkg = JSON.parse(fs.readFileSync(path, 'utf8')); + if (pkg.optionalDependencies) { + for (const k of Object.keys(pkg.optionalDependencies)) { + pkg.optionalDependencies[k] = '$NEXT'; + } + } + fs.writeFileSync(path, JSON.stringify(pkg, null, 2) + '\n'); + " + pnpm -C apps install --no-frozen-lockfile + + - name: Generate CHANGELOG + env: + TAG: ${{ steps.compute.outputs.tag }} + run: git-cliff --tag "$TAG" -o CHANGELOG.md + + - name: Create release PR + merge + tag (or dry-run) + if: ${{ !inputs.dry_run }} + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NEXT: ${{ steps.compute.outputs.version }} + TAG: ${{ steps.compute.outputs.tag }} + run: | + set -euo pipefail + BRANCH="release/$TAG" + MSG="release: $NEXT" + + git checkout -b "$BRANCH" + git add apps/cli/package.json CHANGELOG.md apps/pnpm-lock.yaml || true + if git diff --cached --quiet; then + echo "No changes to commit; aborting." >&2 + exit 1 + fi + git commit -m "$MSG" + git push -u origin "$BRANCH" + + PR_URL="$(gh pr create \ + --title "$MSG" \ + --body "Automated release: \`$NEXT\`" \ + --head "$BRANCH" \ + --base main)" + echo "PR: $PR_URL" + + gh pr merge "$BRANCH" --squash --delete-branch --subject "$MSG" --body "" + + # Pull the squashed commit into local main, then tag from it. + git fetch origin main + git checkout main + git reset --hard origin/main + git tag -a "$TAG" -m "release: $NEXT" + git push origin "$TAG" + + - name: Dry-run summary + if: ${{ inputs.dry_run }} + run: | + echo "DRY RUN: Would have created PR + tag ${{ steps.compute.outputs.tag }}" + echo "DRY RUN: Would have updated CHANGELOG.md" + echo "DRY RUN: Would have bumped apps/cli/package.json to ${{ steps.compute.outputs.version }}" + build-web: + name: Build web standalone + needs: prepare + if: ${{ !inputs.dry_run }} runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + with: + ref: ${{ needs.prepare.outputs.tag }} - uses: pnpm/action-setup@v4 with: version: "9" @@ -25,7 +222,7 @@ jobs: run: pnpm -C apps install --frozen-lockfile - name: Build web (standalone) env: - KANDEV_VERSION: ${{ github.ref_name }} + KANDEV_VERSION: ${{ needs.prepare.outputs.tag }} run: pnpm -C apps --filter @kandev/web build - name: Package web bundle run: bash scripts/release/package-web.sh @@ -35,8 +232,34 @@ jobs: path: dist/web include-hidden-files: true + build-cli: + name: Build & bundle CLI + needs: prepare + if: ${{ !inputs.dry_run }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ needs.prepare.outputs.tag }} + - uses: pnpm/action-setup@v4 + with: + version: "9" + - uses: actions/setup-node@v6 + with: + node-version: "24" + - name: Install dependencies + run: pnpm -C apps install --frozen-lockfile + - name: Build and bundle CLI + run: bash scripts/release/package-cli.sh + - uses: actions/upload-artifact@v4 + with: + name: cli-bundle + path: dist/kandev/cli + build-bundles: - needs: build-web + name: Build platform bundle (${{ matrix.platform }}) + needs: [build-web, build-cli] + if: ${{ !inputs.dry_run }} strategy: matrix: include: @@ -69,6 +292,8 @@ jobs: GOARCH: ${{ matrix.goarch }} steps: - uses: actions/checkout@v4 + with: + ref: ${{ needs.prepare.outputs.tag }} - uses: actions/setup-go@v6 with: go-version-file: apps/backend/go.mod @@ -77,6 +302,10 @@ jobs: with: name: web-bundle path: dist/web + - uses: actions/download-artifact@v4 + with: + name: cli-bundle + path: dist/kandev/cli - name: Install cross-compilation tools if: matrix.cc @@ -96,19 +325,19 @@ jobs: - name: Package bundle run: | - rm -rf dist/kandev + rm -rf dist/kandev/bin dist/kandev/web mkdir -p dist/kandev/bin EXT=${{ matrix.goos == 'windows' && '.exe' || '' }} cp apps/backend/bin/kandev${EXT} dist/kandev/bin/ cp apps/backend/bin/agentctl${EXT} dist/kandev/bin/ mkdir -p dist/kandev/web cp -R dist/web/. dist/kandev/web/ + # cli/ was already placed by the build-cli job (downloaded above) cd dist tar -czf "kandev-${{ matrix.platform }}.tar.gz" kandev shasum -a 256 "kandev-${{ matrix.platform }}.tar.gz" > "kandev-${{ matrix.platform }}.tar.gz.sha256" - - name: Upload bundle artifacts - uses: actions/upload-artifact@v4 + - uses: actions/upload-artifact@v4 with: name: bundle-${{ matrix.platform }} path: | @@ -116,9 +345,14 @@ jobs: dist/kandev-${{ matrix.platform }}.tar.gz.sha256 docker: + name: Build & push container image + needs: prepare + if: ${{ !inputs.dry_run }} runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + with: + ref: ${{ needs.prepare.outputs.tag }} - name: Docker meta id: meta @@ -126,10 +360,10 @@ jobs: with: images: ghcr.io/kdlbs/kandev tags: | - type=semver,pattern={{version}} - type=semver,pattern={{major}}.{{minor}} + type=raw,value=${{ needs.prepare.outputs.version }} + type=raw,value=${{ needs.prepare.outputs.tag }} type=sha - type=raw,value=latest,enable=${{ github.ref_type == 'tag' && startsWith(github.ref_name, 'v') }} + type=raw,value=latest - uses: docker/setup-buildx-action@v3 @@ -152,11 +386,14 @@ jobs: cache-to: type=gha,mode=max publish-release: - needs: build-bundles + name: Publish GitHub release + needs: [prepare, build-bundles] + if: ${{ !inputs.dry_run }} runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: + ref: ${{ needs.prepare.outputs.tag }} fetch-depth: 0 - name: Generate release notes @@ -167,8 +404,7 @@ jobs: env: OUTPUT: RELEASE_NOTES.md - - name: Download bundle artifacts - uses: actions/download-artifact@v4 + - uses: actions/download-artifact@v4 with: pattern: bundle-* path: dist/release-assets @@ -192,10 +428,62 @@ jobs: done ls -lh dist/release-assets - - name: Publish release assets + - name: Publish release uses: softprops/action-gh-release@v2 with: + tag_name: ${{ needs.prepare.outputs.tag }} + name: ${{ needs.prepare.outputs.tag }} body_path: RELEASE_NOTES.md files: | dist/release-assets/kandev-*.tar.gz dist/release-assets/kandev-*.tar.gz.sha256 + + publish-npm: + name: Publish npm packages + needs: [prepare, publish-release] + if: ${{ !inputs.dry_run }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ needs.prepare.outputs.tag }} + + - uses: pnpm/action-setup@v4 + with: + version: "9" + + - uses: actions/setup-node@v6 + with: + node-version: "24" + registry-url: "https://registry.npmjs.org" + + - name: Install dependencies + run: pnpm -C apps install --frozen-lockfile + + - name: Publish npm packages + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + bash scripts/release/publish-npm.sh \ + "${{ needs.prepare.outputs.version }}" \ + "${{ needs.prepare.outputs.tag }}" + + update-homebrew-tap: + name: Update Homebrew tap + needs: [prepare, publish-release] + if: ${{ !inputs.dry_run }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ needs.prepare.outputs.tag }} + + - name: Update Homebrew tap formula + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + HOMEBREW_TAP_DEPLOY_KEY: ${{ secrets.HOMEBREW_TAP_DEPLOY_KEY }} + run: | + bash scripts/release/update-homebrew-tap.sh \ + "${{ needs.prepare.outputs.version }}" \ + "${{ needs.prepare.outputs.tag }}" diff --git a/scripts/release/package-cli.sh b/scripts/release/package-cli.sh new file mode 100755 index 0000000000..13c36fc874 --- /dev/null +++ b/scripts/release/package-cli.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash +# Build the CLI and copy artifacts into dist/kandev/cli/ for inclusion in release bundles. +# +# The CLI bundle (dist/cli.bundle.js) is a single-file esbuild output with all Node +# dependencies inlined. This is required for Homebrew installs where there is no +# node_modules directory in the Cellar. +# +# Output layout inside dist/kandev/cli/: +# bin/cli.js - Node entrypoint (requires #!/usr/bin/env node shebang + chmod +x) +# dist/cli.bundle.js - Self-contained bundle with inlined deps (used by Homebrew) +# package.json - Package metadata (used to read version at runtime) +set -euo pipefail + +ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +CLI_DIR="$ROOT_DIR/apps/cli" +OUT_DIR="$ROOT_DIR/dist/kandev/cli" + +echo "Building CLI for release bundle..." + +# Build TypeScript +pnpm -C "$ROOT_DIR/apps" --filter kandev build +echo " TypeScript build complete" + +# Bundle with esbuild (inlines tree-kill and all other deps) +pnpm -C "$ROOT_DIR/apps/cli" bundle +echo " esbuild bundle complete" + +# Copy artifacts into dist/kandev/cli/ +rm -rf "$OUT_DIR" +mkdir -p "$OUT_DIR/bin" "$OUT_DIR/dist" + +cp "$CLI_DIR/bin/cli.js" "$OUT_DIR/bin/cli.js" +chmod +x "$OUT_DIR/bin/cli.js" + +cp "$CLI_DIR/dist/cli.bundle.js" "$OUT_DIR/dist/cli.bundle.js" +cp "$CLI_DIR/package.json" "$OUT_DIR/package.json" + +echo "CLI artifacts packaged at $OUT_DIR" +echo " bin/cli.js (entrypoint, chmod +x)" +echo " dist/cli.bundle.js (self-contained bundle)" +echo " package.json" diff --git a/scripts/release/package-npm-runtime.sh b/scripts/release/package-npm-runtime.sh new file mode 100755 index 0000000000..2bf4004eb0 --- /dev/null +++ b/scripts/release/package-npm-runtime.sh @@ -0,0 +1,110 @@ +#!/usr/bin/env bash +# Generate per-platform npm runtime packages from built dist/release-assets. +# +# Each platform bundle (kandev-{platform}.tar.gz) is repackaged as an npm package +# containing only bin/ and web/ (no cli/). These are the @kdlbs/runtime-* packages +# that the main kandev npm package declares as optionalDependencies. +# +# Usage: +# package-npm-runtime.sh +# +# Arguments: +# version SemVer string (e.g. 0.17.0) +# release-assets-dir Directory containing kandev-*.tar.gz files +# output-dir Directory where per-platform npm packages are written +# +# Output (one directory per platform ready for npm publish): +# /@kdlbs/runtime-linux-x64/ +# /@kdlbs/runtime-linux-arm64/ +# /@kdlbs/runtime-darwin-x64/ +# /@kdlbs/runtime-darwin-arm64/ +# /@kdlbs/runtime-win32-x64/ +set -euo pipefail + +VERSION="${1:?Usage: $0 }" +ASSETS_DIR="${2:?Usage: $0 }" +OUT_DIR="${3:?Usage: $0 }" + +# Maps platform dir name → npm package name + npm os/cpu fields +declare -A PLATFORM_TO_PACKAGE=( + ["linux-x64"]="@kdlbs/runtime-linux-x64" + ["linux-arm64"]="@kdlbs/runtime-linux-arm64" + ["macos-x64"]="@kdlbs/runtime-darwin-x64" + ["macos-arm64"]="@kdlbs/runtime-darwin-arm64" + ["windows-x64"]="@kdlbs/runtime-win32-x64" +) + +declare -A PLATFORM_TO_OS=( + ["linux-x64"]='["linux"]' + ["linux-arm64"]='["linux"]' + ["macos-x64"]='["darwin"]' + ["macos-arm64"]='["darwin"]' + ["windows-x64"]='["win32"]' +) + +declare -A PLATFORM_TO_CPU=( + ["linux-x64"]='["x64"]' + ["linux-arm64"]='["arm64"]' + ["macos-x64"]='["x64"]' + ["macos-arm64"]='["arm64"]' + ["windows-x64"]='["x64"]' +) + +echo "Packaging npm runtime packages for version $VERSION..." +echo " assets dir: $ASSETS_DIR" +echo " output dir: $OUT_DIR" + +for platform in linux-x64 linux-arm64 macos-x64 macos-arm64 windows-x64; do + archive="$ASSETS_DIR/kandev-${platform}.tar.gz" + if [[ ! -f "$archive" ]]; then + echo "Error: missing archive $archive" >&2 + exit 1 + fi + + package_name="${PLATFORM_TO_PACKAGE[$platform]}" + # @kdlbs/runtime-linux-x64 → scope=@kdlbs, name=runtime-linux-x64 + scope_dir="${package_name%%/*}" # @kdlbs + pkg_dir="${package_name##*/}" # runtime-linux-x64 + pkg_out="$OUT_DIR/${scope_dir}/${pkg_dir}" + + rm -rf "$pkg_out" + mkdir -p "$pkg_out" + + # Extract only bin/ and web/ from the archive (skip cli/) + local_tmp="$pkg_out/.extract_tmp" + mkdir -p "$local_tmp" + tar -xzf "$archive" -C "$local_tmp" + + # Find the extracted bundle root (named "kandev" inside the archive) + bundle_root="$local_tmp/kandev" + if [[ ! -d "$bundle_root" ]]; then + echo "Error: expected kandev/ directory in $archive" >&2 + exit 1 + fi + + cp -R "$bundle_root/bin" "$pkg_out/bin" + cp -R "$bundle_root/web" "$pkg_out/web" + rm -rf "$local_tmp" + + os_field="${PLATFORM_TO_OS[$platform]}" + cpu_field="${PLATFORM_TO_CPU[$platform]}" + + cat > "$pkg_out/package.json" < must already exist (verified before publishing). +# - Caller must be logged into npm with access to kandev and @kdlbs/* packages. +# - The @kdlbs npm org must exist with publish rights granted to the CI token. +# +# Usage: +# publish-npm.sh +# +# Arguments: +# version SemVer string (e.g. 0.17.0) +# tag Git tag (e.g. v0.17.0) — used to verify GitHub release assets exist +set -euo pipefail + +ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" + +VERSION="${1:?Usage: $0 }" +TAG="${2:?Usage: $0 }" + +bold() { printf '\033[1m%s\033[0m' "$*"; } +green() { printf '\033[32m%s\033[0m' "$*"; } +red() { printf '\033[31m%s\033[0m' "$*"; } +yellow(){ printf '\033[33m%s\033[0m' "$*"; } + +log() { echo " >> $*"; } +log_ok() { echo " $(green "ok") $*"; } + +die() { + echo "$(red "Error:") $*" >&2 + exit 1 +} + +# -- Verify GitHub release assets exist before publishing --------------------- + +REQUIRED_PLATFORMS=(linux-x64 linux-arm64 macos-x64 macos-arm64 windows-x64) + +log "Verifying GitHub release assets exist for $TAG..." +for platform in "${REQUIRED_PLATFORMS[@]}"; do + asset="kandev-${platform}.tar.gz" + if ! gh release view "$TAG" --json assets --jq ".assets[].name" 2>/dev/null | grep -q "^${asset}$"; then + die "GitHub release asset missing: $asset in release $TAG. Run release workflow first." + fi +done +log_ok "All 5 platform assets present in GitHub release $TAG" + +# -- Download release assets for packaging ------------------------------------ + +WORK_DIR="$(mktemp -d)" +ASSETS_DIR="$WORK_DIR/assets" +mkdir -p "$ASSETS_DIR" + +log "Downloading release assets for $TAG..." +for platform in "${REQUIRED_PLATFORMS[@]}"; do + asset="kandev-${platform}.tar.gz" + log " downloading $asset..." + gh release download "$TAG" --pattern "$asset" --dir "$ASSETS_DIR" +done +log_ok "Assets downloaded to $ASSETS_DIR" + +# -- Generate npm runtime packages -------------------------------------------- + +NPM_PKG_DIR="$WORK_DIR/npm-packages" +bash "$ROOT_DIR/scripts/release/package-npm-runtime.sh" "$VERSION" "$ASSETS_DIR" "$NPM_PKG_DIR" + +# -- Publish @kdlbs/runtime-* packages first --------------------------------- + +RUNTIME_PACKAGES=( + "@kdlbs/runtime-linux-x64" + "@kdlbs/runtime-linux-arm64" + "@kdlbs/runtime-darwin-x64" + "@kdlbs/runtime-darwin-arm64" + "@kdlbs/runtime-win32-x64" +) + +echo +echo "$(bold "Publishing @kdlbs/runtime-* packages...")" +FAILED_PACKAGES=() + +for pkg in "${RUNTIME_PACKAGES[@]}"; do + scope="${pkg%%/*}" # @kdlbs + name="${pkg##*/}" # runtime-linux-x64 + pkg_dir="$NPM_PKG_DIR/${scope}/${name}" + + if [[ ! -d "$pkg_dir" ]]; then + echo " $(red "missing") $pkg_dir" >&2 + FAILED_PACKAGES+=("$pkg") + continue + fi + + log "Publishing $pkg@$VERSION..." + if (cd "$pkg_dir" && npm publish --access public 2>&1); then + log_ok "$pkg@$VERSION published" + else + echo " $(yellow "warn") Failed to publish $pkg@$VERSION (may already exist)" >&2 + FAILED_PACKAGES+=("$pkg") + fi +done + +# -- Publish main kandev package ---------------------------------------------- + +echo +echo "$(bold "Publishing kandev@$VERSION...")" +( + cd "$ROOT_DIR/apps/cli" + # Build before publishing + pnpm build + npm publish --access public +) +log_ok "kandev@$VERSION published" + +# -- Cleanup ------------------------------------------------------------------ + +rm -rf "$WORK_DIR" + +# -- Report ------------------------------------------------------------------- + +echo +if [[ "${#FAILED_PACKAGES[@]}" -gt 0 ]]; then + echo "$(yellow "Warning:") Some runtime packages failed to publish:" + for pkg in "${FAILED_PACKAGES[@]}"; do + echo " - $pkg" + done + echo " These may already exist at version $VERSION, or may need to be published manually." +else + echo "$(green "$(bold "All npm packages published successfully!")")" +fi diff --git a/scripts/release/release-pr.sh b/scripts/release/release-pr.sh deleted file mode 100755 index cd3d1d7e32..0000000000 --- a/scripts/release/release-pr.sh +++ /dev/null @@ -1,812 +0,0 @@ -#!/usr/bin/env bash -# ----------------------------------------------------------------------------- -# release-pr.sh -# -# Unified release script for: -# - CLI npm release (apps/cli) -# - App runtime release (git tag that triggers GitHub Actions release workflow) -# - Both in one run -# -# Versioning model: -# - App tag format: vM.m -# - CLI npm version format: M.m.0 -# - Default bump mode: minor (major bump is explicit) -# -# High-level flow: -# 1) Validate preconditions (tools, clean tree, main branch, fetch tags) -# 2) Select release target (cli | app | both) -# 3) Auto-detect current versions/tags and compute next versions -# 4) Show a release plan and require confirmation -# 5) Execute release actions: -# - CLI: bump package version, install deps, build -# - App: generate CHANGELOG.md -# - Create release branch, commit, open PR, squash-merge -# - App: create/push release tag (after PR merge) -# - CLI: npm publish (after PR merge) -# -# Safety: -# - Use --dry-run to preview every action without mutating repo or publishing. -# - Use --yes for non-interactive confirmation. -# ----------------------------------------------------------------------------- -set -euo pipefail - -ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" -CLI_PACKAGE_JSON="$ROOT_DIR/apps/cli/package.json" - -TARGET="" -DEFAULT_BUMP="minor" -AUTO_YES=0 -DRY_RUN=0 - -CLI_SELECTED=0 -APP_SELECTED=0 - -CURRENT_BRANCH="" -CURRENT_CLI_VERSION="" -CURRENT_CLI_MAJOR=0 -CURRENT_CLI_MINOR=0 -CURRENT_CLI_PATCH=0 -CURRENT_APP_TAG="" -CURRENT_APP_TAG_FOUND=0 -CURRENT_APP_MAJOR=0 -CURRENT_APP_MINOR=0 - -CLI_BUMP="" -APP_BUMP="" -NEXT_CLI_VERSION="" -NEXT_APP_TAG="" -RELEASE_BRANCH="" - -CURRENT_STEP=0 -TOTAL_STEPS=0 - -# -- Formatting helpers ------------------------------------------------------- - -bold() { printf '\033[1m%s\033[0m' "$*"; } -dim() { printf '\033[2m%s\033[0m' "$*"; } -green() { printf '\033[32m%s\033[0m' "$*"; } -yellow() { printf '\033[33m%s\033[0m' "$*"; } -red() { printf '\033[31m%s\033[0m' "$*"; } -cyan() { printf '\033[36m%s\033[0m' "$*"; } - -log() { echo " $(dim ">>") $*"; } -phase() { echo; echo "$(bold "$*")"; } -log_ok() { echo " $(green "ok") $*"; } -log_skip() { echo " $(dim "skip") $*"; } - -step() { - ((CURRENT_STEP++)) - echo - echo "$(bold "[$CURRENT_STEP/$TOTAL_STEPS]") $1" -} - -# -- Core helpers -------------------------------------------------------------- - -usage() { - cat <<'EOF' -Usage: scripts/release/release-pr.sh [options] - -Options: - --target Release target. If omitted, prompt interactively. - --bump Bump mode. Default: minor. - --yes Skip confirmation prompts. - --dry-run Print actions without making changes/publishes. - --help, -h Show help. - -Versioning: - - App tags: vM.m - - CLI package version: M.m.0 - -Prerequisites: - - Run from a clean working tree on branch main. - - Required tools: git, node, npm, pnpm, git-cliff, gh. - -Examples: - # Fully interactive release wizard - scripts/release/release-pr.sh - - # Non-interactive CLI minor release - scripts/release/release-pr.sh --target cli --bump minor --yes - - # Preview both releases with major bump - scripts/release/release-pr.sh --target both --bump major --dry-run -EOF -} - -die() { - echo "$(red "Error:") $*" >&2 - exit 1 -} - -command_exists() { - command -v "$1" >/dev/null 2>&1 -} - -run_cmd() { - if [[ "$DRY_RUN" -eq 1 ]]; then - echo " $(yellow "[dry-run]") $*" - return 0 - fi - "$@" -} - -confirm() { - local prompt="$1" - if [[ "$AUTO_YES" -eq 1 ]]; then - return 0 - fi - echo - local answer - read -r -p "$(bold "$prompt") [y/N]: " answer - answer="$(echo "${answer:-}" | tr '[:upper:]' '[:lower:]')" - [[ "$answer" == "y" || "$answer" == "yes" ]] -} - -validate_bump() { - case "$1" in - minor|major) ;; - *) die "Invalid bump '$1'. Use 'minor' or 'major'." ;; - esac -} - -compute_total_steps() { - TOTAL_STEPS=1 # create release PR (always) - [[ "$CLI_SELECTED" -eq 1 ]] && ((TOTAL_STEPS++)) # build CLI - [[ "$APP_SELECTED" -eq 1 ]] && ((TOTAL_STEPS++)) # create app tag - [[ "$CLI_SELECTED" -eq 1 ]] && ((TOTAL_STEPS++)) # publish CLI -} - -release_branch_name() { - if [[ "$CLI_SELECTED" -eq 1 && "$APP_SELECTED" -eq 1 ]]; then - echo "release/app-${NEXT_APP_TAG}-cli-${NEXT_CLI_VERSION}" - elif [[ "$CLI_SELECTED" -eq 1 ]]; then - echo "release/cli-${NEXT_CLI_VERSION}" - else - echo "release/app-${NEXT_APP_TAG}" - fi -} - -# -- Target selection ---------------------------------------------------------- - -parse_target() { - case "$1" in - cli) - CLI_SELECTED=1 - APP_SELECTED=0 - ;; - app) - CLI_SELECTED=0 - APP_SELECTED=1 - ;; - both) - CLI_SELECTED=1 - APP_SELECTED=1 - ;; - *) - die "Invalid target '$1'. Use cli, app, or both." - ;; - esac -} - -select_target_interactive() { - echo - echo "$(bold "What do you want to release?")" - echo " $(cyan "1)") cli $(dim "- npm publish apps/cli")" - echo " $(cyan "2)") app $(dim "- git tag for GitHub Actions release")" - echo " $(cyan "3)") both $(dim "- cli + app together")" - echo - local choice - read -r -p "$(bold "Choice") [1=cli / 2=app / 3=both]: " choice - case "$choice" in - 1|cli) parse_target "cli" ;; - 2|app) parse_target "app" ;; - 3|both) parse_target "both" ;; - *) die "Invalid choice '$choice'. Enter 1, 2, 3, cli, app, or both." ;; - esac - - local selected="" - [[ "$CLI_SELECTED" -eq 1 ]] && selected+="cli " - [[ "$APP_SELECTED" -eq 1 ]] && selected+="app " - log "Selected target: $(bold "$selected")" -} - -# -- Version parsing ----------------------------------------------------------- - -parse_cli_version() { - local version="$1" - if [[ ! "$version" =~ ^([0-9]+)\.([0-9]+)(\.([0-9]+))?$ ]]; then - die "CLI version '$version' is not valid. Expected M.m.0 format." - fi - CURRENT_CLI_MAJOR="${BASH_REMATCH[1]}" - CURRENT_CLI_MINOR="${BASH_REMATCH[2]}" - CURRENT_CLI_PATCH="${BASH_REMATCH[4]:-0}" -} - -detect_current_cli_version() { - [[ -f "$CLI_PACKAGE_JSON" ]] || die "Missing $CLI_PACKAGE_JSON" - CURRENT_CLI_VERSION="$(node -p "require('$CLI_PACKAGE_JSON').version")" - parse_cli_version "$CURRENT_CLI_VERSION" - log "Current CLI version: $(bold "$CURRENT_CLI_VERSION") $(dim "(from apps/cli/package.json)")" -} - -detect_current_app_version() { - local tags - tags="$(git -C "$ROOT_DIR" tag --list 'v*')" - - CURRENT_APP_TAG_FOUND=0 - local found=0 - local best_major=0 - local best_minor=0 - local tag - - while IFS= read -r tag; do - [[ -z "$tag" ]] && continue - if [[ "$tag" =~ ^v([0-9]+)\.([0-9]+)$ ]]; then - local major="${BASH_REMATCH[1]}" - local minor="${BASH_REMATCH[2]}" - if [[ "$found" -eq 0 ]] || (( 10#$major > 10#$best_major )) || \ - (( 10#$major == 10#$best_major && 10#$minor > 10#$best_minor )); then - found=1 - best_major="$major" - best_minor="$minor" - fi - fi - done <<<"$tags" - - CURRENT_APP_MAJOR="$best_major" - CURRENT_APP_MINOR="$best_minor" - CURRENT_APP_TAG="v${CURRENT_APP_MAJOR}.${CURRENT_APP_MINOR}" - CURRENT_APP_TAG_FOUND="$found" - - if [[ "$found" -eq 1 ]]; then - log "Current app tag: $(bold "$CURRENT_APP_TAG") $(dim "(latest git tag matching vM.m)")" - else - log "Current app tag: $(dim "none found") $(dim "(will start at v0.1)")" - fi -} - -# -- Bump selection ------------------------------------------------------------ - -next_minor_pair() { - local major="$1" - local minor="$2" - echo "${major}.$((10#$minor + 1))" -} - -next_major_pair() { - local major="$1" - echo "$((10#$major + 1)).0" -} - -choose_bump() { - local subject="$1" - local current="$2" - local next_minor="$3" - local next_major="$4" - - if [[ -n "$DEFAULT_BUMP" ]]; then - validate_bump "$DEFAULT_BUMP" - fi - - if [[ "$AUTO_YES" -eq 1 && -n "$DEFAULT_BUMP" ]]; then - echo "$DEFAULT_BUMP" - return - fi - - if [[ -n "$DEFAULT_BUMP" && "$TARGET" != "" ]]; then - echo "$DEFAULT_BUMP" - return - fi - - echo >&2 - echo "$(bold "$subject version bump") $(dim "(current: $current)")" >&2 - echo " $(cyan "1)") minor -> $(bold "$next_minor") $(dim "(default)")" >&2 - echo " $(cyan "2)") major -> $(bold "$next_major")" >&2 - local choice - read -r -p "$(bold "Bump") [1=minor / 2=major]: " choice - case "${choice:-1}" in - 1|minor) echo "minor" ;; - 2|major) echo "major" ;; - *) die "Invalid bump choice '$choice'. Enter 1, 2, minor, or major." ;; - esac -} - -# -- Version computation ------------------------------------------------------- - -compute_next_versions() { - if [[ "$CLI_SELECTED" -eq 1 ]]; then - local cli_minor_pair cli_major_pair - cli_minor_pair="$(next_minor_pair "$CURRENT_CLI_MAJOR" "$CURRENT_CLI_MINOR")" - cli_major_pair="$(next_major_pair "$CURRENT_CLI_MAJOR")" - - CLI_BUMP="$(choose_bump "CLI" "${CURRENT_CLI_MAJOR}.${CURRENT_CLI_MINOR}.0" \ - "${cli_minor_pair}.0" "${cli_major_pair}.0")" - validate_bump "$CLI_BUMP" - - if [[ "$CLI_BUMP" == "minor" ]]; then - NEXT_CLI_VERSION="${cli_minor_pair}.0" - else - NEXT_CLI_VERSION="${cli_major_pair}.0" - fi - log "CLI bump: $CLI_BUMP -> $(bold "$NEXT_CLI_VERSION")" - fi - - if [[ "$APP_SELECTED" -eq 1 ]]; then - local app_minor_pair app_major_pair - app_minor_pair="$(next_minor_pair "$CURRENT_APP_MAJOR" "$CURRENT_APP_MINOR")" - app_major_pair="$(next_major_pair "$CURRENT_APP_MAJOR")" - - APP_BUMP="$(choose_bump "App" "$CURRENT_APP_TAG" "v${app_minor_pair}" "v${app_major_pair}")" - validate_bump "$APP_BUMP" - - if [[ "$APP_BUMP" == "minor" ]]; then - NEXT_APP_TAG="v${app_minor_pair}" - else - NEXT_APP_TAG="v${app_major_pair}" - fi - log "App bump: $APP_BUMP -> $(bold "$NEXT_APP_TAG")" - fi -} - -# -- Preconditions ------------------------------------------------------------- - -ensure_prerequisites() { - phase "Checking prerequisites" - - for tool in git node npm pnpm git-cliff gh; do - if command_exists "$tool"; then - log_ok "$tool" - else - die "$tool is required but not found in PATH." - fi - done - - CURRENT_BRANCH="$(git -C "$ROOT_DIR" rev-parse --abbrev-ref HEAD)" - if [[ "$CURRENT_BRANCH" == "main" ]]; then - log_ok "On branch $(bold "main")" - else - die "Release must run from main branch (current: $CURRENT_BRANCH)." - fi - - if [[ -n "$(git -C "$ROOT_DIR" status --porcelain)" ]]; then - die "Working tree is not clean. Commit or stash changes before releasing." - fi - log_ok "Working tree is clean" - - log "Fetching tags from origin..." - run_cmd git -C "$ROOT_DIR" fetch --tags origin - log_ok "Tags fetched" -} - -ensure_npm_auth() { - [[ "$CLI_SELECTED" -eq 1 ]] || return 0 - [[ "$DRY_RUN" -eq 1 ]] && return 0 - - log "Checking npm authentication..." - local npm_user - if npm_user="$(npm whoami 2>/dev/null)"; then - log_ok "Logged in to npm as $(bold "$npm_user")" - else - echo - echo " $(yellow "npm auth required") — you are not logged in to npm." - echo " $(dim "This is needed to publish the CLI package.")" - echo - if [[ "$AUTO_YES" -eq 1 ]]; then - die "npm login required but --yes was specified. Run 'npm login' first." - fi - local answer - read -r -p "$(bold "Run npm login now?") [Y/n]: " answer - answer="$(echo "${answer:-y}" | tr '[:upper:]' '[:lower:]')" - if [[ "$answer" == "y" || "$answer" == "yes" || -z "$answer" ]]; then - npm login - # Verify login succeeded - if npm_user="$(npm whoami 2>/dev/null)"; then - log_ok "Logged in to npm as $(bold "$npm_user")" - else - die "npm login failed. Cannot publish CLI." - fi - else - die "npm login required to publish CLI. Run 'npm login' and try again." - fi - fi -} - -ensure_app_tag_available() { - [[ "$APP_SELECTED" -eq 1 ]] || return 0 - - log "Checking tag $(bold "$NEXT_APP_TAG") is available..." - if git -C "$ROOT_DIR" rev-parse "$NEXT_APP_TAG" >/dev/null 2>&1; then - die "Tag $NEXT_APP_TAG already exists locally." - fi - - if git -C "$ROOT_DIR" ls-remote --tags origin "refs/tags/$NEXT_APP_TAG" | grep -q .; then - die "Tag $NEXT_APP_TAG already exists on origin." - fi - log_ok "Tag $(bold "$NEXT_APP_TAG") is available" -} - -latest_origin_app_tag() { - local refs - if ! refs="$(git -C "$ROOT_DIR" ls-remote --tags origin 'refs/tags/v*')"; then - return 2 - fi - - local found=0 - local best_major=0 - local best_minor=0 - local ref tag - - while read -r _ ref; do - [[ -z "${ref:-}" ]] && continue - tag="${ref#refs/tags/}" - tag="${tag%\^\{\}}" - if [[ "$tag" =~ ^v([0-9]+)\.([0-9]+)$ ]]; then - local major="${BASH_REMATCH[1]}" - local minor="${BASH_REMATCH[2]}" - if [[ "$found" -eq 0 ]] || (( 10#$major > 10#$best_major )) || \ - (( 10#$major == 10#$best_major && 10#$minor > 10#$best_minor )); then - found=1 - best_major="$major" - best_minor="$minor" - fi - fi - done <<<"$refs" - - [[ "$found" -eq 1 ]] || return 1 - echo "v${best_major}.${best_minor}" -} - -ensure_latest_app_tag_present() { - [[ "$APP_SELECTED" -eq 1 ]] || return 0 - [[ "$DRY_RUN" -eq 1 ]] && return 0 - - log "Verifying latest app tag $(bold "$CURRENT_APP_TAG") is present locally..." - if [[ "$CURRENT_APP_TAG_FOUND" -eq 0 ]]; then - local origin_status - if latest_origin_app_tag >/dev/null; then - origin_status=0 - else - origin_status="$?" - fi - if [[ "$origin_status" -eq 0 ]]; then - die "No local app tag found, but origin has app tags. Fetch origin tags and retry." - fi - if [[ "$origin_status" -ne 1 ]]; then - die "Could not reach origin to verify app tags. Check your connection and retry." - fi - log_ok "No prior app tags on origin or locally" - return 0 - fi - - local origin_app_tag - if ! origin_app_tag="$(latest_origin_app_tag)"; then - die "Could not determine latest app tag from origin. Refusing to generate CHANGELOG.md." - fi - - if [[ "$CURRENT_APP_TAG" != "$origin_app_tag" ]]; then - die "Latest local app tag is $CURRENT_APP_TAG, but origin latest is $origin_app_tag. Refusing to generate CHANGELOG.md." - fi - - if ! git -C "$ROOT_DIR" rev-parse --verify --quiet "refs/tags/$CURRENT_APP_TAG" >/dev/null; then - die "Latest app tag $CURRENT_APP_TAG is missing locally after fetching origin tags. Refusing to generate CHANGELOG.md." - fi - - log_ok "Latest app tag $(bold "$CURRENT_APP_TAG") is present" -} - -# -- Release plan -------------------------------------------------------------- - -print_plan() { - echo - echo "$(bold "=== Release Plan ===")" - echo " branch: $(bold "$CURRENT_BRANCH")" - if [[ "$CLI_SELECTED" -eq 1 ]]; then - echo " cli: $(bold "$CURRENT_CLI_VERSION") -> $(green "$NEXT_CLI_VERSION") ($CLI_BUMP bump)" - fi - if [[ "$APP_SELECTED" -eq 1 ]]; then - echo " app tag: $(bold "$CURRENT_APP_TAG") -> $(green "$NEXT_APP_TAG") ($APP_BUMP bump)" - fi - if [[ "$DRY_RUN" -eq 1 ]]; then - echo " mode: $(yellow "DRY RUN") $(dim "(no changes will be made)")" - fi - echo - - local n=0 - if [[ "$CLI_SELECTED" -eq 1 ]]; then - echo " $(dim "Steps for CLI:")" - echo " $(dim "1.") Bump version in apps/cli/package.json" - echo " $(dim "2.") Install dependencies (pnpm install)" - echo " $(dim "3.") Build CLI (pnpm build)" - fi - if [[ "$APP_SELECTED" -eq 1 ]]; then - echo " $(dim "Steps for App:")" - echo " $(dim "1.") Generate CHANGELOG.md (git-cliff)" - fi - echo " $(dim "Steps for Release:")" - ((n++)); echo " $(dim "$n.") Create release branch and open PR" - ((n++)); echo " $(dim "$n.") Squash-merge PR into main" - if [[ "$APP_SELECTED" -eq 1 ]]; then - ((n++)); echo " $(dim "$n.") Create and push annotated git tag $NEXT_APP_TAG" - fi - if [[ "$CLI_SELECTED" -eq 1 ]]; then - ((n++)); echo " $(dim "$n.") Publish CLI to npm" - fi - echo -} - -# -- Release actions ----------------------------------------------------------- - -commit_message() { - if [[ "$CLI_SELECTED" -eq 1 && "$APP_SELECTED" -eq 1 ]]; then - echo "release: app $NEXT_APP_TAG, cli $NEXT_CLI_VERSION" - elif [[ "$CLI_SELECTED" -eq 1 ]]; then - echo "release: cli $NEXT_CLI_VERSION" - elif [[ "$APP_SELECTED" -eq 1 ]]; then - echo "release: app $NEXT_APP_TAG" - else - echo "" - fi -} - -apply_cli_release() { - [[ "$CLI_SELECTED" -eq 1 ]] || return 0 - - step "Building CLI release" - - log "Bumping CLI version to $(bold "$NEXT_CLI_VERSION")..." - if [[ "$DRY_RUN" -eq 1 ]]; then - echo " $(yellow "[dry-run]") npm version --no-git-tag-version $NEXT_CLI_VERSION" - else - (cd "$ROOT_DIR/apps/cli" && npm version --no-git-tag-version "$NEXT_CLI_VERSION") - fi - log_ok "Version bumped in package.json" - - log "Installing dependencies..." - if [[ "$DRY_RUN" -eq 1 ]]; then - echo " $(yellow "[dry-run]") pnpm install --frozen-lockfile" - else - pnpm -C "$ROOT_DIR/apps" install --frozen-lockfile - fi - log_ok "Dependencies installed" - - log "Building CLI..." - if [[ "$DRY_RUN" -eq 1 ]]; then - echo " $(yellow "[dry-run]") pnpm -C apps/cli build" - else - pnpm -C "$ROOT_DIR/apps/cli" build - fi - log_ok "CLI built" -} - -generate_changelog() { - [[ "$APP_SELECTED" -eq 1 ]] || return 0 - - log "Refreshing origin tags before changelog generation..." - run_cmd git -C "$ROOT_DIR" fetch --tags origin - ensure_latest_app_tag_present - - log "Generating changelog for $(bold "$NEXT_APP_TAG")..." - if [[ "$DRY_RUN" -eq 1 ]]; then - echo " $(yellow "[dry-run]") git-cliff --tag $NEXT_APP_TAG -o CHANGELOG.md" - return 0 - fi - git-cliff --tag "$NEXT_APP_TAG" -o "$ROOT_DIR/CHANGELOG.md" - log_ok "CHANGELOG.md updated" -} - -create_release_pr() { - local msg - msg="$(commit_message)" - [[ -n "$msg" ]] || return 0 - - step "Creating release PR" - - RELEASE_BRANCH="$(release_branch_name)" - - if [[ "$DRY_RUN" -eq 1 ]]; then - echo " $(yellow "[dry-run]") git checkout -b $RELEASE_BRANCH" - [[ "$CLI_SELECTED" -eq 1 ]] && echo " $(yellow "[dry-run]") git add apps/cli/package.json" - [[ "$APP_SELECTED" -eq 1 ]] && echo " $(yellow "[dry-run]") git add CHANGELOG.md" - echo " $(yellow "[dry-run]") git commit -m \"$msg\"" - echo " $(yellow "[dry-run]") git push -u origin $RELEASE_BRANCH" - echo " $(yellow "[dry-run]") gh pr create --title \"$msg\"" - echo " $(yellow "[dry-run]") gh pr merge --squash --delete-branch" - echo " $(yellow "[dry-run]") git checkout main && git pull origin main" - return 0 - fi - - log "Creating release branch $(bold "$RELEASE_BRANCH")..." - git -C "$ROOT_DIR" checkout -b "$RELEASE_BRANCH" - log_ok "Branch created" - - log "Staging release files..." - [[ "$CLI_SELECTED" -eq 1 ]] && git -C "$ROOT_DIR" add apps/cli/package.json - [[ "$APP_SELECTED" -eq 1 && -f "$ROOT_DIR/CHANGELOG.md" ]] && git -C "$ROOT_DIR" add CHANGELOG.md - - if git -C "$ROOT_DIR" diff --cached --quiet; then - log_skip "No staged changes to commit" - git -C "$ROOT_DIR" checkout main - git -C "$ROOT_DIR" branch -d "$RELEASE_BRANCH" - return 0 - fi - - log "Committing: $(dim "$msg")" - git -C "$ROOT_DIR" commit -m "$msg" - log_ok "Committed" - - log "Pushing release branch..." - git -C "$ROOT_DIR" push -u origin "$RELEASE_BRANCH" - log_ok "Branch pushed" - - local pr_body - pr_body="Automated release PR."$'\n\n'"**Changes:**" - [[ "$CLI_SELECTED" -eq 1 ]] && pr_body+=$'\n'"- CLI: \`$CURRENT_CLI_VERSION\` → \`$NEXT_CLI_VERSION\`" - [[ "$APP_SELECTED" -eq 1 ]] && pr_body+=$'\n'"- App: \`$CURRENT_APP_TAG\` → \`$NEXT_APP_TAG\`" - - log "Creating pull request..." - local pr_url - pr_url="$(gh pr create \ - --title "$msg" \ - --body "$pr_body" \ - --head "$RELEASE_BRANCH" \ - --base main)" - log_ok "PR created: $(bold "$pr_url")" - - log "Merging PR (squash)..." - if gh pr merge "$RELEASE_BRANCH" --squash --delete-branch --subject "$msg" --body ""; then - log_ok "PR merged" - else - echo - echo " $(yellow "Auto-merge failed.") Merge the PR manually:" - echo " $(cyan "$pr_url")" - echo - if [[ "$AUTO_YES" -eq 1 ]]; then - die "Cannot proceed without PR merge in --yes mode. Merge the PR manually and re-run." - fi - read -r -p "$(bold "Press Enter after merging the PR...")" - # Verify PR was merged - local pr_state - pr_state="$(gh pr view "$RELEASE_BRANCH" --json state --jq '.state')" - if [[ "$pr_state" != "MERGED" ]]; then - die "PR is not merged (state: $pr_state). Cannot continue." - fi - log_ok "PR merged" - fi - - log "Switching to main and pulling..." - git -C "$ROOT_DIR" checkout main - git -C "$ROOT_DIR" pull origin main - # Clean up local release branch if it still exists - git -C "$ROOT_DIR" branch -d "$RELEASE_BRANCH" 2>/dev/null || true - log_ok "main is up to date" -} - -create_and_push_app_tag() { - [[ "$APP_SELECTED" -eq 1 ]] || return 0 - - step "Creating app tag" - - log "Creating annotated tag $(bold "$NEXT_APP_TAG")..." - if [[ "$DRY_RUN" -eq 1 ]]; then - echo " $(yellow "[dry-run]") git tag -a $NEXT_APP_TAG -m \"release: $NEXT_APP_TAG\"" - echo " $(yellow "[dry-run]") git push origin $NEXT_APP_TAG" - return 0 - fi - - git -C "$ROOT_DIR" tag -a "$NEXT_APP_TAG" -m "release: $NEXT_APP_TAG" - log_ok "Tag created" - - log "Pushing tag to origin..." - git -C "$ROOT_DIR" push origin "$NEXT_APP_TAG" - log_ok "Tag pushed (GitHub Actions release workflow will trigger)" -} - -publish_cli() { - [[ "$CLI_SELECTED" -eq 1 ]] || return 0 - - step "Publishing CLI to npm" - - log "Running npm publish --access public..." - if [[ "$DRY_RUN" -eq 1 ]]; then - echo " $(yellow "[dry-run]") npm publish --access public" - return 0 - fi - (cd "$ROOT_DIR/apps/cli" && npm publish --access public) - log_ok "Published to npm" -} - -# -- Argument parsing ---------------------------------------------------------- - -parse_args() { - while [[ $# -gt 0 ]]; do - case "$1" in - --target) - [[ $# -ge 2 ]] || die "--target requires a value." - TARGET="$2" - shift 2 - ;; - --bump) - [[ $# -ge 2 ]] || die "--bump requires a value." - DEFAULT_BUMP="$2" - shift 2 - ;; - --yes) - AUTO_YES=1 - shift - ;; - --dry-run) - DRY_RUN=1 - shift - ;; - --help|-h) - usage - exit 0 - ;; - *) - die "Unknown argument '$1'." - ;; - esac - done -} - -# -- Main ---------------------------------------------------------------------- - -main() { - parse_args "$@" - validate_bump "$DEFAULT_BUMP" - - echo - echo "$(bold "Kandev Release") $(dim "(PR mode)")" - if [[ "$DRY_RUN" -eq 1 ]]; then - echo "$(yellow " DRY RUN MODE - no changes will be made")" - fi - - ensure_prerequisites - - phase "Selecting target and version" - - if [[ -n "$TARGET" ]]; then - parse_target "$TARGET" - local selected="" - [[ "$CLI_SELECTED" -eq 1 ]] && selected+="cli " - [[ "$APP_SELECTED" -eq 1 ]] && selected+="app " - log "Target: $(bold "$selected") $(dim "(from --target flag)")" - else - # Interactive mode should prompt for target and bump choices. - DEFAULT_BUMP="" - select_target_interactive - fi - - ensure_npm_auth - detect_current_cli_version - detect_current_app_version - compute_next_versions - ensure_latest_app_tag_present - ensure_app_tag_available - compute_total_steps - print_plan - - if ! confirm "Proceed with release?"; then - echo - echo "Release cancelled." - exit 0 - fi - - apply_cli_release - generate_changelog - create_release_pr - create_and_push_app_tag - publish_cli - - echo - echo "$(green "$(bold "Release complete!")")" - if [[ "$CLI_SELECTED" -eq 1 ]]; then - echo " $(green "cli:") published $NEXT_CLI_VERSION to npm" - fi - if [[ "$APP_SELECTED" -eq 1 ]]; then - echo " $(green "app:") pushed tag $NEXT_APP_TAG to origin" - fi - echo -} - -main "$@" diff --git a/scripts/release/release.sh b/scripts/release/release.sh deleted file mode 100755 index ce0e75d013..0000000000 --- a/scripts/release/release.sh +++ /dev/null @@ -1,648 +0,0 @@ -#!/usr/bin/env bash -# ----------------------------------------------------------------------------- -# release.sh -# -# Unified release script for: -# - CLI npm release (apps/cli) -# - App runtime release (git tag that triggers GitHub Actions release workflow) -# - Both in one run -# -# Versioning model: -# - App tag format: vM.m -# - CLI npm version format: M.m.0 -# - Default bump mode: minor (major bump is explicit) -# -# High-level flow: -# 1) Validate preconditions (tools, clean tree, main branch, fetch tags) -# 2) Select release target (cli | app | both) -# 3) Auto-detect current versions/tags and compute next versions -# 4) Show a release plan and require confirmation -# 5) Execute release actions: -# - CLI: bump package version, install deps, build, commit/push, npm publish -# - App: create/push release tag -# -# Safety: -# - Use --dry-run to preview every action without mutating repo or publishing. -# - Use --yes for non-interactive confirmation. -# ----------------------------------------------------------------------------- -set -euo pipefail - -ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" -CLI_PACKAGE_JSON="$ROOT_DIR/apps/cli/package.json" - -TARGET="" -DEFAULT_BUMP="minor" -AUTO_YES=0 -DRY_RUN=0 - -CLI_SELECTED=0 -APP_SELECTED=0 - -CURRENT_BRANCH="" -CURRENT_CLI_VERSION="" -CURRENT_CLI_MAJOR=0 -CURRENT_CLI_MINOR=0 -CURRENT_CLI_PATCH=0 -CURRENT_APP_TAG="" -CURRENT_APP_MAJOR=0 -CURRENT_APP_MINOR=0 - -CLI_BUMP="" -APP_BUMP="" -NEXT_CLI_VERSION="" -NEXT_APP_TAG="" - -# -- Formatting helpers ------------------------------------------------------- - -bold() { printf '\033[1m%s\033[0m' "$*"; } -dim() { printf '\033[2m%s\033[0m' "$*"; } -green() { printf '\033[32m%s\033[0m' "$*"; } -yellow() { printf '\033[33m%s\033[0m' "$*"; } -red() { printf '\033[31m%s\033[0m' "$*"; } -cyan() { printf '\033[36m%s\033[0m' "$*"; } - -log() { echo " $(dim ">>") $*"; } -log_step() { echo; echo "$(bold "[$1]") $2"; } -log_ok() { echo " $(green "ok") $*"; } -log_skip() { echo " $(dim "skip") $*"; } - -# -- Core helpers -------------------------------------------------------------- - -usage() { - cat <<'EOF' -Usage: scripts/release/release.sh [options] - -Options: - --target Release target. If omitted, prompt interactively. - --bump Bump mode. Default: minor. - --yes Skip confirmation prompts. - --dry-run Print actions without making changes/publishes. - --help, -h Show help. - -Versioning: - - App tags: vM.m - - CLI package version: M.m.0 - -Prerequisites: - - Run from a clean working tree on branch main. - - Required tools: git, node, npm, pnpm, git-cliff. - -Examples: - # Fully interactive release wizard - scripts/release/release.sh - - # Non-interactive CLI minor release - scripts/release/release.sh --target cli --bump minor --yes - - # Preview both releases with major bump - scripts/release/release.sh --target both --bump major --dry-run -EOF -} - -die() { - echo "$(red "Error:") $*" >&2 - exit 1 -} - -command_exists() { - command -v "$1" >/dev/null 2>&1 -} - -run_cmd() { - if [[ "$DRY_RUN" -eq 1 ]]; then - echo " $(yellow "[dry-run]") $*" - return 0 - fi - "$@" -} - -confirm() { - local prompt="$1" - if [[ "$AUTO_YES" -eq 1 ]]; then - return 0 - fi - echo - local answer - read -r -p "$(bold "$prompt") [y/N]: " answer - answer="$(echo "${answer:-}" | tr '[:upper:]' '[:lower:]')" - [[ "$answer" == "y" || "$answer" == "yes" ]] -} - -validate_bump() { - case "$1" in - minor|major) ;; - *) die "Invalid bump '$1'. Use 'minor' or 'major'." ;; - esac -} - -# -- Target selection ---------------------------------------------------------- - -parse_target() { - case "$1" in - cli) - CLI_SELECTED=1 - APP_SELECTED=0 - ;; - app) - CLI_SELECTED=0 - APP_SELECTED=1 - ;; - both) - CLI_SELECTED=1 - APP_SELECTED=1 - ;; - *) - die "Invalid target '$1'. Use cli, app, or both." - ;; - esac -} - -select_target_interactive() { - echo - echo "$(bold "What do you want to release?")" - echo " $(cyan "1)") cli $(dim "- npm publish apps/cli")" - echo " $(cyan "2)") app $(dim "- git tag for GitHub Actions release")" - echo " $(cyan "3)") both $(dim "- cli + app together")" - echo - local choice - read -r -p "$(bold "Choice") [1=cli / 2=app / 3=both]: " choice - case "$choice" in - 1|cli) parse_target "cli" ;; - 2|app) parse_target "app" ;; - 3|both) parse_target "both" ;; - *) die "Invalid choice '$choice'. Enter 1, 2, 3, cli, app, or both." ;; - esac - - local selected="" - [[ "$CLI_SELECTED" -eq 1 ]] && selected+="cli " - [[ "$APP_SELECTED" -eq 1 ]] && selected+="app " - log "Selected target: $(bold "$selected")" -} - -# -- Version parsing ----------------------------------------------------------- - -parse_cli_version() { - local version="$1" - if [[ ! "$version" =~ ^([0-9]+)\.([0-9]+)(\.([0-9]+))?$ ]]; then - die "CLI version '$version' is not valid. Expected M.m.0 format." - fi - CURRENT_CLI_MAJOR="${BASH_REMATCH[1]}" - CURRENT_CLI_MINOR="${BASH_REMATCH[2]}" - CURRENT_CLI_PATCH="${BASH_REMATCH[4]:-0}" -} - -detect_current_cli_version() { - [[ -f "$CLI_PACKAGE_JSON" ]] || die "Missing $CLI_PACKAGE_JSON" - CURRENT_CLI_VERSION="$(node -p "require('$CLI_PACKAGE_JSON').version")" - parse_cli_version "$CURRENT_CLI_VERSION" - log "Current CLI version: $(bold "$CURRENT_CLI_VERSION") $(dim "(from apps/cli/package.json)")" -} - -detect_current_app_version() { - local tags - tags="$(git -C "$ROOT_DIR" tag --list 'v*')" - - local found=0 - local best_major=0 - local best_minor=0 - local tag - - while IFS= read -r tag; do - [[ -z "$tag" ]] && continue - if [[ "$tag" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?$ ]]; then - local major="${BASH_REMATCH[1]}" - local minor="${BASH_REMATCH[2]}" - if [[ "$found" -eq 0 ]] || (( 10#$major > 10#$best_major )) || \ - (( 10#$major == 10#$best_major && 10#$minor > 10#$best_minor )); then - found=1 - best_major="$major" - best_minor="$minor" - fi - fi - done <<<"$tags" - - CURRENT_APP_MAJOR="$best_major" - CURRENT_APP_MINOR="$best_minor" - CURRENT_APP_TAG="v${CURRENT_APP_MAJOR}.${CURRENT_APP_MINOR}" - - if [[ "$found" -eq 1 ]]; then - log "Current app tag: $(bold "$CURRENT_APP_TAG") $(dim "(latest git tag matching vM.m)")" - else - log "Current app tag: $(dim "none found") $(dim "(will start at v0.1)")" - fi -} - -# -- Bump selection ------------------------------------------------------------ - -next_minor_pair() { - local major="$1" - local minor="$2" - echo "${major}.$((10#$minor + 1))" -} - -next_major_pair() { - local major="$1" - echo "$((10#$major + 1)).0" -} - -choose_bump() { - local subject="$1" - local current="$2" - local next_minor="$3" - local next_major="$4" - - if [[ -n "$DEFAULT_BUMP" ]]; then - validate_bump "$DEFAULT_BUMP" - fi - - if [[ "$AUTO_YES" -eq 1 && -n "$DEFAULT_BUMP" ]]; then - echo "$DEFAULT_BUMP" - return - fi - - if [[ -n "$DEFAULT_BUMP" && "$TARGET" != "" ]]; then - echo "$DEFAULT_BUMP" - return - fi - - echo >&2 - echo "$(bold "$subject version bump") $(dim "(current: $current)")" >&2 - echo " $(cyan "1)") minor -> $(bold "$next_minor") $(dim "(default)")" >&2 - echo " $(cyan "2)") major -> $(bold "$next_major")" >&2 - local choice - read -r -p "$(bold "Bump") [1=minor / 2=major]: " choice - case "${choice:-1}" in - 1|minor) echo "minor" ;; - 2|major) echo "major" ;; - *) die "Invalid bump choice '$choice'. Enter 1, 2, minor, or major." ;; - esac -} - -# -- Version computation ------------------------------------------------------- - -compute_next_versions() { - if [[ "$CLI_SELECTED" -eq 1 ]]; then - local cli_minor_pair cli_major_pair - cli_minor_pair="$(next_minor_pair "$CURRENT_CLI_MAJOR" "$CURRENT_CLI_MINOR")" - cli_major_pair="$(next_major_pair "$CURRENT_CLI_MAJOR")" - - CLI_BUMP="$(choose_bump "CLI" "${CURRENT_CLI_MAJOR}.${CURRENT_CLI_MINOR}.0" \ - "${cli_minor_pair}.0" "${cli_major_pair}.0")" - validate_bump "$CLI_BUMP" - - if [[ "$CLI_BUMP" == "minor" ]]; then - NEXT_CLI_VERSION="${cli_minor_pair}.0" - else - NEXT_CLI_VERSION="${cli_major_pair}.0" - fi - log "CLI bump: $CLI_BUMP -> $(bold "$NEXT_CLI_VERSION")" - fi - - if [[ "$APP_SELECTED" -eq 1 ]]; then - local app_minor_pair app_major_pair - app_minor_pair="$(next_minor_pair "$CURRENT_APP_MAJOR" "$CURRENT_APP_MINOR")" - app_major_pair="$(next_major_pair "$CURRENT_APP_MAJOR")" - - APP_BUMP="$(choose_bump "App" "$CURRENT_APP_TAG" "v${app_minor_pair}" "v${app_major_pair}")" - validate_bump "$APP_BUMP" - - if [[ "$APP_BUMP" == "minor" ]]; then - NEXT_APP_TAG="v${app_minor_pair}" - else - NEXT_APP_TAG="v${app_major_pair}" - fi - log "App bump: $APP_BUMP -> $(bold "$NEXT_APP_TAG")" - fi -} - -# -- Preconditions ------------------------------------------------------------- - -ensure_prerequisites() { - log_step "1/5" "Checking prerequisites" - - for tool in git node npm pnpm git-cliff; do - if command_exists "$tool"; then - log_ok "$tool" - else - die "$tool is required but not found in PATH." - fi - done - - CURRENT_BRANCH="$(git -C "$ROOT_DIR" rev-parse --abbrev-ref HEAD)" - if [[ "$CURRENT_BRANCH" == "main" ]]; then - log_ok "On branch $(bold "main")" - else - die "Release must run from main branch (current: $CURRENT_BRANCH)." - fi - - if [[ -n "$(git -C "$ROOT_DIR" status --porcelain)" ]]; then - die "Working tree is not clean. Commit or stash changes before releasing." - fi - log_ok "Working tree is clean" - - log "Fetching tags from origin..." - run_cmd git -C "$ROOT_DIR" fetch --tags origin - log_ok "Tags fetched" -} - -ensure_npm_auth() { - [[ "$CLI_SELECTED" -eq 1 ]] || return 0 - [[ "$DRY_RUN" -eq 1 ]] && return 0 - - log "Checking npm authentication..." - local npm_user - if npm_user="$(npm whoami 2>/dev/null)"; then - log_ok "Logged in to npm as $(bold "$npm_user")" - else - echo - echo " $(yellow "npm auth required") — you are not logged in to npm." - echo " $(dim "This is needed to publish the CLI package.")" - echo - if [[ "$AUTO_YES" -eq 1 ]]; then - die "npm login required but --yes was specified. Run 'npm login' first." - fi - local answer - read -r -p "$(bold "Run npm login now?") [Y/n]: " answer - answer="$(echo "${answer:-y}" | tr '[:upper:]' '[:lower:]')" - if [[ "$answer" == "y" || "$answer" == "yes" || -z "$answer" ]]; then - npm login - # Verify login succeeded - if npm_user="$(npm whoami 2>/dev/null)"; then - log_ok "Logged in to npm as $(bold "$npm_user")" - else - die "npm login failed. Cannot publish CLI." - fi - else - die "npm login required to publish CLI. Run 'npm login' and try again." - fi - fi -} - -ensure_app_tag_available() { - [[ "$APP_SELECTED" -eq 1 ]] || return 0 - - log "Checking tag $(bold "$NEXT_APP_TAG") is available..." - if git -C "$ROOT_DIR" rev-parse "$NEXT_APP_TAG" >/dev/null 2>&1; then - die "Tag $NEXT_APP_TAG already exists locally." - fi - - if git -C "$ROOT_DIR" ls-remote --tags origin "refs/tags/$NEXT_APP_TAG" | grep -q .; then - die "Tag $NEXT_APP_TAG already exists on origin." - fi - log_ok "Tag $(bold "$NEXT_APP_TAG") is available" -} - -# -- Release plan -------------------------------------------------------------- - -print_plan() { - echo - echo "$(bold "=== Release Plan ===")" - echo " branch: $(bold "$CURRENT_BRANCH")" - if [[ "$CLI_SELECTED" -eq 1 ]]; then - echo " cli: $(bold "$CURRENT_CLI_VERSION") -> $(green "$NEXT_CLI_VERSION") ($CLI_BUMP bump)" - fi - if [[ "$APP_SELECTED" -eq 1 ]]; then - echo " app tag: $(bold "$CURRENT_APP_TAG") -> $(green "$NEXT_APP_TAG") ($APP_BUMP bump)" - fi - if [[ "$DRY_RUN" -eq 1 ]]; then - echo " mode: $(yellow "DRY RUN") $(dim "(no changes will be made)")" - fi - echo - - if [[ "$CLI_SELECTED" -eq 1 ]]; then - echo " $(dim "Steps for CLI:")" - echo " $(dim "1.") Bump version in apps/cli/package.json" - echo " $(dim "2.") Install dependencies (pnpm install)" - echo " $(dim "3.") Build CLI (pnpm build)" - echo " $(dim "4.") Commit and push to main" - echo " $(dim "5.") Publish to npm" - fi - if [[ "$APP_SELECTED" -eq 1 ]]; then - echo " $(dim "Steps for App:")" - echo " $(dim "1.") Generate CHANGELOG.md (git-cliff)" - echo " $(dim "2.") Commit and push changelog" - echo " $(dim "3.") Create annotated git tag $NEXT_APP_TAG" - echo " $(dim "4.") Push tag to origin (triggers GitHub Actions)" - fi - echo -} - -# -- Release actions ----------------------------------------------------------- - -commit_message() { - if [[ "$CLI_SELECTED" -eq 1 && "$APP_SELECTED" -eq 1 ]]; then - echo "release: app $NEXT_APP_TAG, cli $NEXT_CLI_VERSION" - elif [[ "$CLI_SELECTED" -eq 1 ]]; then - echo "release: cli $NEXT_CLI_VERSION" - elif [[ "$APP_SELECTED" -eq 1 ]]; then - echo "release: app $NEXT_APP_TAG" - else - echo "" - fi -} - -apply_cli_release() { - [[ "$CLI_SELECTED" -eq 1 ]] || return 0 - - log_step "3/5" "Building CLI release" - - log "Bumping CLI version to $(bold "$NEXT_CLI_VERSION")..." - if [[ "$DRY_RUN" -eq 1 ]]; then - echo " $(yellow "[dry-run]") npm version --no-git-tag-version $NEXT_CLI_VERSION" - else - (cd "$ROOT_DIR/apps/cli" && npm version --no-git-tag-version "$NEXT_CLI_VERSION") - fi - log_ok "Version bumped in package.json" - - log "Installing dependencies..." - if [[ "$DRY_RUN" -eq 1 ]]; then - echo " $(yellow "[dry-run]") pnpm install --frozen-lockfile" - else - pnpm -C "$ROOT_DIR/apps" install --frozen-lockfile - fi - log_ok "Dependencies installed" - - log "Building CLI..." - if [[ "$DRY_RUN" -eq 1 ]]; then - echo " $(yellow "[dry-run]") pnpm -C apps/cli build" - else - pnpm -C "$ROOT_DIR/apps/cli" build - fi - log_ok "CLI built" -} - -generate_changelog() { - [[ "$APP_SELECTED" -eq 1 ]] || return 0 - - log "Generating changelog for $(bold "$NEXT_APP_TAG")..." - if [[ "$DRY_RUN" -eq 1 ]]; then - echo " $(yellow "[dry-run]") git-cliff --tag $NEXT_APP_TAG -o CHANGELOG.md" - return 0 - fi - git-cliff --tag "$NEXT_APP_TAG" -o "$ROOT_DIR/CHANGELOG.md" - log_ok "CHANGELOG.md updated" -} - -commit_and_push_if_needed() { - local msg - msg="$(commit_message)" - [[ -n "$msg" ]] || return 0 - - log_step "4/5" "Committing and pushing" - - if [[ "$DRY_RUN" -eq 1 ]]; then - [[ "$CLI_SELECTED" -eq 1 ]] && echo " $(yellow "[dry-run]") git add apps/cli/package.json" - [[ "$APP_SELECTED" -eq 1 ]] && echo " $(yellow "[dry-run]") git add CHANGELOG.md" - echo " $(yellow "[dry-run]") git commit -m \"$msg\"" - echo " $(yellow "[dry-run]") git push origin $CURRENT_BRANCH" - return 0 - fi - - log "Staging release files..." - [[ "$CLI_SELECTED" -eq 1 ]] && git -C "$ROOT_DIR" add apps/cli/package.json - [[ "$APP_SELECTED" -eq 1 && -f "$ROOT_DIR/CHANGELOG.md" ]] && git -C "$ROOT_DIR" add CHANGELOG.md - if git -C "$ROOT_DIR" diff --cached --quiet; then - log_skip "No staged changes to commit" - return 0 - fi - - log "Committing: $(dim "$msg")" - git -C "$ROOT_DIR" commit -m "$msg" - log_ok "Committed" - - log "Pushing to origin/$CURRENT_BRANCH..." - git -C "$ROOT_DIR" push origin "$CURRENT_BRANCH" - log_ok "Pushed" -} - -create_and_push_app_tag() { - [[ "$APP_SELECTED" -eq 1 ]] || return 0 - - if [[ "$CLI_SELECTED" -eq 0 ]]; then - log_step "3/5" "Creating app tag" - else - log_step "4/5" "Creating app tag" - fi - - log "Creating annotated tag $(bold "$NEXT_APP_TAG")..." - if [[ "$DRY_RUN" -eq 1 ]]; then - echo " $(yellow "[dry-run]") git tag -a $NEXT_APP_TAG -m \"release: $NEXT_APP_TAG\"" - echo " $(yellow "[dry-run]") git push origin $NEXT_APP_TAG" - return 0 - fi - - git -C "$ROOT_DIR" tag -a "$NEXT_APP_TAG" -m "release: $NEXT_APP_TAG" - log_ok "Tag created" - - log "Pushing tag to origin..." - git -C "$ROOT_DIR" push origin "$NEXT_APP_TAG" - log_ok "Tag pushed (GitHub Actions release workflow will trigger)" -} - -publish_cli() { - [[ "$CLI_SELECTED" -eq 1 ]] || return 0 - - log_step "5/5" "Publishing CLI to npm" - - log "Running npm publish --access public..." - if [[ "$DRY_RUN" -eq 1 ]]; then - echo " $(yellow "[dry-run]") npm publish --access public" - return 0 - fi - (cd "$ROOT_DIR/apps/cli" && npm publish --access public) - log_ok "Published to npm" -} - -# -- Argument parsing ---------------------------------------------------------- - -parse_args() { - while [[ $# -gt 0 ]]; do - case "$1" in - --target) - [[ $# -ge 2 ]] || die "--target requires a value." - TARGET="$2" - shift 2 - ;; - --bump) - [[ $# -ge 2 ]] || die "--bump requires a value." - DEFAULT_BUMP="$2" - shift 2 - ;; - --yes) - AUTO_YES=1 - shift - ;; - --dry-run) - DRY_RUN=1 - shift - ;; - --help|-h) - usage - exit 0 - ;; - *) - die "Unknown argument '$1'." - ;; - esac - done -} - -# -- Main ---------------------------------------------------------------------- - -main() { - parse_args "$@" - validate_bump "$DEFAULT_BUMP" - - echo - echo "$(bold "Kandev Release")" - if [[ "$DRY_RUN" -eq 1 ]]; then - echo "$(yellow " DRY RUN MODE - no changes will be made")" - fi - - ensure_prerequisites - - log_step "2/5" "Selecting target and version" - - if [[ -n "$TARGET" ]]; then - parse_target "$TARGET" - local selected="" - [[ "$CLI_SELECTED" -eq 1 ]] && selected+="cli " - [[ "$APP_SELECTED" -eq 1 ]] && selected+="app " - log "Target: $(bold "$selected") $(dim "(from --target flag)")" - else - # Interactive mode should prompt for target and bump choices. - DEFAULT_BUMP="" - select_target_interactive - fi - - ensure_npm_auth - detect_current_cli_version - detect_current_app_version - compute_next_versions - ensure_app_tag_available - print_plan - - if ! confirm "Proceed with release?"; then - echo - echo "Release cancelled." - exit 0 - fi - - apply_cli_release - generate_changelog - commit_and_push_if_needed - create_and_push_app_tag - publish_cli - - echo - echo "$(green "$(bold "Release complete!")")" - if [[ "$CLI_SELECTED" -eq 1 ]]; then - echo " $(green "cli:") published $NEXT_CLI_VERSION to npm" - fi - if [[ "$APP_SELECTED" -eq 1 ]]; then - echo " $(green "app:") pushed tag $NEXT_APP_TAG to origin" - fi - echo -} - -main "$@" diff --git a/scripts/release/update-homebrew-tap.sh b/scripts/release/update-homebrew-tap.sh new file mode 100755 index 0000000000..7b597c53fe --- /dev/null +++ b/scripts/release/update-homebrew-tap.sh @@ -0,0 +1,186 @@ +#!/usr/bin/env bash +# Update the kdlbs/homebrew-kandev tap with a new formula for the given release. +# +# Downloads .sha256 files from the GitHub release to populate the formula, +# generates Formula/kandev.rb, and pushes (or opens a PR). +# +# Usage: +# update-homebrew-tap.sh +# +# Arguments: +# version SemVer string (e.g. 0.17.0) +# tag Git tag (e.g. v0.17.0) +# +# Environment: +# HOMEBREW_TAP_REPO Override tap repo (default: kdlbs/homebrew-kandev). +# HOMEBREW_TAP_DEPLOY_KEY Private SSH key (PEM) for the tap repo. When set, +# clone via SSH and push directly to main. This is +# the default CI path. Deploy keys cannot open PRs. +# HOMEBREW_TAP_PUSH When no deploy key is present, set to "1" to push +# directly via `gh`/HTTPS instead of opening a PR. +set -euo pipefail + +VERSION="${1:?Usage: $0 }" +TAG="${2:?Usage: $0 }" +TAP_REPO="${HOMEBREW_TAP_REPO:-kdlbs/homebrew-kandev}" +PUSH_DIRECT="${HOMEBREW_TAP_PUSH:-0}" + +bold() { printf '\033[1m%s\033[0m' "$*"; } +green() { printf '\033[32m%s\033[0m' "$*"; } +red() { printf '\033[31m%s\033[0m' "$*"; } + +log() { echo " >> $*"; } +log_ok() { echo " $(green "ok") $*"; } + +die() { + echo "$(red "Error:") $*" >&2 + exit 1 +} + +# -- Determine clone strategy: deploy key (SSH) or gh (HTTPS) ----------------- + +CLONE_METHOD="gh" +SSH_KEY_FILE="" +SSH_WRAPPER="" + +cleanup_ssh() { + [[ -n "${SSH_KEY_FILE:-}" && -f "$SSH_KEY_FILE" ]] && rm -f "$SSH_KEY_FILE" + [[ -n "${SSH_WRAPPER:-}" && -f "$SSH_WRAPPER" ]] && rm -f "$SSH_WRAPPER" +} +trap cleanup_ssh EXIT + +if [[ -n "${HOMEBREW_TAP_DEPLOY_KEY:-}" ]]; then + CLONE_METHOD="ssh" + # Materialize the deploy key + a custom GIT_SSH_COMMAND so it doesn't pollute ~/.ssh + SSH_KEY_FILE="$(mktemp)" + chmod 600 "$SSH_KEY_FILE" + printf '%s\n' "$HOMEBREW_TAP_DEPLOY_KEY" > "$SSH_KEY_FILE" + # Ensure trailing newline (some secret stores strip it; openssh requires it) + if [[ "$(tail -c1 "$SSH_KEY_FILE")" != "" ]]; then + echo "" >> "$SSH_KEY_FILE" + fi + export GIT_SSH_COMMAND="ssh -i $SSH_KEY_FILE -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR" + log "Using SSH deploy key for tap auth" +fi + +# -- Fetch sha256 values from GitHub release ---------------------------------- + +fetch_sha256() { + local platform="$1" + local sha_file="kandev-${platform}.tar.gz.sha256" + local content + content="$(gh release download "$TAG" --pattern "$sha_file" --dir /tmp 2>/dev/null && cat "/tmp/$sha_file")" || { + die "Failed to download $sha_file from release $TAG." + } + # sha256 files contain: — extract just the hash + echo "$content" | awk '{print $1}' +} + +log "Fetching sha256 values from GitHub release $TAG..." +SHA_MACOS_ARM64="$(fetch_sha256 macos-arm64)" +SHA_MACOS_X64="$(fetch_sha256 macos-x64)" +SHA_LINUX_ARM64="$(fetch_sha256 linux-arm64)" +SHA_LINUX_X64="$(fetch_sha256 linux-x64)" +log_ok "sha256 values retrieved" + +# -- Clone tap repo ----------------------------------------------------------- + +WORK_DIR="$(mktemp -d)" +TAP_DIR="$WORK_DIR/tap" + +log "Cloning $TAP_REPO..." +if [[ "$CLONE_METHOD" == "ssh" ]]; then + git clone "git@github.com:${TAP_REPO}.git" "$TAP_DIR" +else + gh repo clone "$TAP_REPO" "$TAP_DIR" +fi +log_ok "Tap cloned to $TAP_DIR" + +# -- Generate formula --------------------------------------------------------- + +FORMULA_PATH="$TAP_DIR/Formula/kandev.rb" +mkdir -p "$(dirname "$FORMULA_PATH")" + +GITHUB_BASE="https://github.com/kdlbs/kandev/releases/download/${TAG}" + +cat > "$FORMULA_PATH" <