1
0

release.sh 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env bash
  2. # ── 本地一键发布 · One-command local release ──
  3. # 构建 → 打包 → 打 tag → 推送 → 创建 GitHub Release
  4. # build → package → tag → push → create GitHub Release
  5. # 用法 / Usage:scripts/release.sh [--dry-run]
  6. # 前置 / Prerequisites:已安装 gh 并完成鉴权(gh auth login,或提供 GH_TOKEN),且仓库已配置 remote origin
  7. # GitHub CLI (gh) installed and authenticated (gh auth login, or provide GH_TOKEN), with a configured remote origin
  8. set -euo pipefail
  9. ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
  10. cd "$ROOT"
  11. DRY_RUN=0
  12. if [ "${1:-}" = "--dry-run" ]; then
  13. DRY_RUN=1
  14. fi
  15. # 从 csproj 读取版本号 / Read the version from the csproj
  16. VERSION="$(sed -n 's:.*<Version>\(.*\)</Version>.*:\1:p' src/PerformanceScope.Plugin/PerformanceScope.Plugin.csproj | head -1)"
  17. [ -n "$VERSION" ] || { echo "错误:未能从 csproj 读取 Version | Error: cannot read Version from csproj" >&2; exit 1; }
  18. TAG="v$VERSION"
  19. # 工作区必须干净,避免发布内容与提交不一致
  20. # Require a clean work tree so the release matches the commit
  21. if [ -n "$(git status --porcelain)" ]; then
  22. echo "错误:工作区有未提交改动,请先提交 | Error: uncommitted changes; commit them first" >&2
  23. git status --short
  24. exit 1
  25. fi
  26. BRANCH="$(git rev-parse --abbrev-ref HEAD)"
  27. if [ "$BRANCH" = "HEAD" ]; then
  28. echo "错误:处于游离 HEAD,无法发布 | Error: detached HEAD, cannot release" >&2
  29. exit 1
  30. fi
  31. # tag 不能已存在 / The tag must not already exist
  32. if git rev-parse -q --verify "refs/tags/$TAG" >/dev/null; then
  33. echo "错误:tag $TAG 已存在 | Error: tag $TAG already exists" >&2
  34. exit 1
  35. fi
  36. echo "版本 / Version:$VERSION 标签 / Tag:$TAG 分支 / Branch:$BRANCH"
  37. # 构建并打包(--dry-run 也会执行,以校验构建与产物)
  38. # Build and package (also runs under --dry-run to validate the build and artifact)
  39. bash scripts/package.sh
  40. ZIP="artifacts/PerformanceScope-v${VERSION}.zip"
  41. [ -f "$ZIP" ] || { echo "错误:未找到产物 $ZIP | Error: artifact not found: $ZIP" >&2; exit 1; }
  42. if [ "$DRY_RUN" = "1" ]; then
  43. echo "[dry-run] 将执行 / would run:"
  44. echo " git tag -a $TAG -m \"$TAG\""
  45. echo " git push origin $BRANCH"
  46. echo " git push origin $TAG"
  47. echo " gh release create $TAG $ZIP --generate-notes"
  48. exit 0
  49. fi
  50. command -v gh >/dev/null 2>&1 || { echo "错误:未安装 GitHub CLI(gh)| Error: GitHub CLI (gh) not installed" >&2; exit 1; }
  51. git remote get-url origin >/dev/null 2>&1 || { echo "错误:未配置 git remote origin | Error: no git remote 'origin' configured" >&2; exit 1; }
  52. # 需要可用的 gh 鉴权(或提供 GH_TOKEN);在打 tag 之前校验,避免留下没有 Release 的悬空 tag
  53. # Require working gh auth (or GH_TOKEN); checked before tagging to avoid a dangling tag without a Release
  54. if [ -z "${GH_TOKEN:-}" ]; then
  55. gh auth status >/dev/null 2>&1 || { echo "错误:gh 未登录,请先 gh auth login 或设置 GH_TOKEN | Error: gh is not authenticated; run gh auth login or set GH_TOKEN" >&2; exit 1; }
  56. fi
  57. git tag -a "$TAG" -m "PerformanceScope $TAG"
  58. git push origin "$BRANCH"
  59. git push origin "$TAG"
  60. gh release create "$TAG" "$ZIP" --title "PerformanceScope $TAG" --generate-notes
  61. echo "已发布 $TAG(产物:$ZIP)| Released $TAG (artifact: $ZIP)"