release.sh 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/OneShot.Plugin/OneShot.Plugin.csproj | head -1)"
  17. [ -n "$VERSION" ] || { echo "错误:未能从 csproj 读取 Version | Error: cannot read Version from csproj" >&2; exit 1; }
  18. # 校验 BepInPlugin 特性里的版本与 csproj 一致,避免两处漂移
  19. # Verify the BepInPlugin version matches the csproj so the two cannot drift apart
  20. PLUGIN_VERSION="$(grep -oE '\[BepInPlugin\([^]]*' src/OneShot.Plugin/Plugin.cs | grep -oE '"[0-9]+\.[0-9]+\.[0-9]+"' | tr -d '"' | tail -1)"
  21. if [ -n "$PLUGIN_VERSION" ] && [ "$PLUGIN_VERSION" != "$VERSION" ]; then
  22. echo "错误:BepInPlugin 版本 $PLUGIN_VERSION 与 csproj 版本 $VERSION 不一致 | Error: BepInPlugin version $PLUGIN_VERSION does not match csproj version $VERSION" >&2
  23. exit 1
  24. fi
  25. TAG="v$VERSION"
  26. # 工作区必须干净,避免发布内容与提交不一致
  27. # Require a clean work tree so the release matches the commit
  28. if [ -n "$(git status --porcelain)" ]; then
  29. echo "错误:工作区有未提交改动,请先提交 | Error: uncommitted changes; commit them first" >&2
  30. git status --short
  31. exit 1
  32. fi
  33. BRANCH="$(git rev-parse --abbrev-ref HEAD)"
  34. if [ "$BRANCH" = "HEAD" ]; then
  35. echo "错误:处于游离 HEAD,无法发布 | Error: detached HEAD, cannot release" >&2
  36. exit 1
  37. fi
  38. # tag 不能已存在 / The tag must not already exist
  39. if git rev-parse -q --verify "refs/tags/$TAG" >/dev/null; then
  40. echo "错误:tag $TAG 已存在 | Error: tag $TAG already exists" >&2
  41. exit 1
  42. fi
  43. echo "版本 / Version:$VERSION 标签 / Tag:$TAG 分支 / Branch:$BRANCH"
  44. # 构建并打包(--dry-run 也会执行,以校验构建与产物)
  45. # Build and package (also runs under --dry-run to validate the build and artifact)
  46. bash scripts/package.sh
  47. ZIP="artifacts/OneShot-v${VERSION}.zip"
  48. [ -f "$ZIP" ] || { echo "错误:未找到产物 $ZIP | Error: artifact not found: $ZIP" >&2; exit 1; }
  49. if [ "$DRY_RUN" = "1" ]; then
  50. echo "[dry-run] 将执行 / would run:"
  51. echo " git tag -a $TAG -m \"$TAG\""
  52. echo " git push origin $BRANCH"
  53. echo " git push origin $TAG"
  54. echo " gh release create $TAG $ZIP --generate-notes"
  55. exit 0
  56. fi
  57. command -v gh >/dev/null 2>&1 || { echo "错误:未安装 GitHub CLI(gh)| Error: GitHub CLI (gh) not installed" >&2; exit 1; }
  58. git remote get-url origin >/dev/null 2>&1 || { echo "错误:未配置 git remote origin | Error: no git remote 'origin' configured" >&2; exit 1; }
  59. # 需要可用的 gh 鉴权(或提供 GH_TOKEN);在打 tag 之前校验,避免留下没有 Release 的悬空 tag
  60. # Require working gh auth (or GH_TOKEN); checked before tagging to avoid a dangling tag without a Release
  61. if [ -z "${GH_TOKEN:-}" ]; then
  62. 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; }
  63. fi
  64. git tag -a "$TAG" -m "OneShot $TAG"
  65. git push origin "$BRANCH"
  66. git push origin "$TAG"
  67. gh release create "$TAG" "$ZIP" --title "OneShot $TAG" --generate-notes
  68. echo "已发布 $TAG(产物:$ZIP)| Released $TAG (artifact: $ZIP)"