release.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env bash
  2. # Exit script as soon as a command fails.
  3. set -o errexit
  4. # Default the prerelease version suffix to rc
  5. : ${PRERELEASE_SUFFIX:=rc}
  6. log() {
  7. # Print to stderr to prevent this from being 'returned'
  8. echo "$@" > /dev/stderr
  9. }
  10. current_version() {
  11. echo "v$(node --print --eval "require('./package.json').version")"
  12. }
  13. current_release_branch() {
  14. v="$(current_version)" # 3.3.0-rc.0
  15. vf="${v%-"$PRERELEASE_SUFFIX".*}" # 3.3.0
  16. r="${vf%.*}" # 3.3
  17. echo "release-$r"
  18. }
  19. assert_current_branch() {
  20. current_branch="$(git symbolic-ref --short HEAD)"
  21. expected_branch="$1"
  22. if [[ "$current_branch" != "$expected_branch" ]]; then
  23. log "Current branch '$current_branch' is not '$expected_branch'"
  24. exit 1
  25. fi
  26. }
  27. push_release_branch_and_tag() {
  28. git push upstream "$(current_release_branch)" "$(current_version)"
  29. }
  30. publish() {
  31. dist_tag="$1"
  32. log "Publishing @openzeppelin/contracts on npm"
  33. cd contracts
  34. npm publish --tag "$dist_tag" --otp "$(prompt_otp)"
  35. cd ..
  36. if [[ "$dist_tag" == "latest" ]]; then
  37. npm dist-tag rm --otp "$(prompt_otp)" @openzeppelin/contracts next
  38. fi
  39. }
  40. push_and_publish() {
  41. dist_tag="$1"
  42. log "Pushing release branch and tags to upstream"
  43. push_release_branch_and_tag
  44. publish "$dist_tag"
  45. }
  46. prompt_otp() {
  47. log -n "Enter npm 2FA token: "
  48. read -r otp
  49. echo "$otp"
  50. }
  51. environment_check() {
  52. if ! git remote get-url upstream &> /dev/null; then
  53. log "No 'upstream' remote found"
  54. exit 1
  55. fi
  56. if npm whoami &> /dev/null; then
  57. log "Will publish as '$(npm whoami)'"
  58. else
  59. log "Not logged in into npm, run 'npm login' first"
  60. exit 1
  61. fi
  62. }
  63. environment_check
  64. if [[ "$*" == "push" ]]; then
  65. push_and_publish next
  66. elif [[ "$*" == "start minor" ]]; then
  67. log "Creating new minor pre-release"
  68. assert_current_branch master
  69. # Create temporary release branch
  70. git checkout -b release-temp
  71. # This bumps minor and adds prerelease suffix, commits the changes, and tags the commit
  72. npm version preminor --preid="$PRERELEASE_SUFFIX"
  73. # Rename the release branch
  74. git branch --move "$(current_release_branch)"
  75. push_and_publish next
  76. elif [[ "$*" == "start major" ]]; then
  77. log "Creating new major pre-release"
  78. assert_current_branch master
  79. # Create temporary release branch
  80. git checkout -b release-temp
  81. # This bumps major and adds prerelease suffix, commits the changes, and tags the commit
  82. npm version premajor --preid="$PRERELEASE_SUFFIX"
  83. # Rename the release branch
  84. git branch --move "$(current_release_branch)"
  85. push_and_publish next
  86. elif [[ "$*" == "rc" ]]; then
  87. log "Bumping pre-release"
  88. assert_current_branch "$(current_release_branch)"
  89. # Bumps prerelease number, commits and tags
  90. npm version prerelease
  91. push_and_publish next
  92. elif [[ "$*" == "final" ]]; then
  93. # Update changelog release date, remove prerelease suffix, tag, push to git, publish in npm, remove next dist-tag
  94. log "Creating final release"
  95. assert_current_branch "$(current_release_branch)"
  96. # This will remove the prerelease suffix from the version
  97. npm version patch
  98. push_release_branch_and_tag
  99. push_and_publish latest
  100. log "Remember to merge the release branch into master and push upstream"
  101. else
  102. log "Unknown command: '$*'"
  103. exit 1
  104. fi