1
0

release.sh 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env bash
  2. # Tag the current commit with the version in package.json and publish a
  3. # matching GitHub Release whose body is the corresponding CHANGELOG.md entry.
  4. #
  5. # Run AFTER you have:
  6. # - bumped package.json
  7. # - added a `## [X.Y.Z] - YYYY-MM-DD` block at the top of CHANGELOG.md
  8. # - committed, pushed to origin, and run `npm publish`
  9. #
  10. # Idempotent: safe to re-run after a partial failure. Skips steps that are
  11. # already done (tag created, tag pushed, release published).
  12. #
  13. # Usage: ./scripts/release.sh
  14. set -euo pipefail
  15. cd "$(dirname "$0")/.."
  16. VERSION=$(node -p "require('./package.json').version")
  17. TAG="v${VERSION}"
  18. REPO=$(git remote get-url origin | sed -E 's|.*github\.com[:/]||; s|\.git$||')
  19. if [ -z "${REPO}" ]; then
  20. echo "error: could not derive owner/repo from origin remote URL" >&2
  21. exit 1
  22. fi
  23. if ! grep -q "^## \[${VERSION}\]" CHANGELOG.md; then
  24. echo "error: no '## [${VERSION}]' entry found in CHANGELOG.md" >&2
  25. exit 1
  26. fi
  27. NOTES=$(awk -v v="${VERSION}" '
  28. /^## \[/ {
  29. if (p) exit
  30. if ($0 ~ "^## \\[" v "\\]") p = 1
  31. }
  32. p
  33. ' CHANGELOG.md)
  34. if [ -z "${NOTES}" ]; then
  35. echo "error: failed to extract changelog notes for ${VERSION}" >&2
  36. exit 1
  37. fi
  38. if git rev-parse "${TAG}" >/dev/null 2>&1; then
  39. echo "✓ tag ${TAG} already exists locally"
  40. else
  41. echo "→ tagging ${TAG}"
  42. git tag "${TAG}"
  43. fi
  44. if git ls-remote --exit-code --tags origin "${TAG}" >/dev/null 2>&1; then
  45. echo "✓ tag ${TAG} already on origin"
  46. else
  47. echo "→ pushing ${TAG} to origin"
  48. git push origin "${TAG}"
  49. fi
  50. if gh release view "${TAG}" --repo "${REPO}" >/dev/null 2>&1; then
  51. echo "✓ release ${TAG} already published"
  52. else
  53. echo "→ creating GitHub Release ${TAG} on ${REPO}"
  54. gh release create "${TAG}" \
  55. --repo "${REPO}" \
  56. --title "${TAG}" \
  57. --notes "${NOTES}"
  58. fi
  59. echo "done: https://github.com/${REPO}/releases/tag/${TAG}"