release.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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-solidity on npm"
  33. npm publish --tag "$dist_tag" --otp "$(prompt_otp)"
  34. log "Publishing @openzeppelin/contracts on npm"
  35. env ALREADY_COMPILED= \
  36. npm publish contracts --tag "$dist_tag" --otp "$(prompt_otp)"
  37. if [[ "$dist_tag" == "latest" ]]; then
  38. otp="$(prompt_otp)"
  39. npm dist-tag rm --otp "$otp" openzeppelin-solidity next
  40. npm dist-tag rm --otp "$otp" @openzeppelin/contracts next
  41. fi
  42. }
  43. push_and_publish() {
  44. dist_tag="$1"
  45. log "Pushing release branch and tags to upstream"
  46. push_release_branch_and_tag
  47. publish "$dist_tag"
  48. }
  49. prompt_otp() {
  50. log -n "Enter npm 2FA token: "
  51. read -r otp
  52. echo "$otp"
  53. }
  54. environment_check() {
  55. if ! git remote get-url upstream &> /dev/null; then
  56. log "No 'upstream' remote found"
  57. exit 1
  58. fi
  59. if npm whoami &> /dev/null; then
  60. log "Will publish as '$(npm whoami)'"
  61. else
  62. log "Not logged in into npm, run 'npm login' first"
  63. exit 1
  64. fi
  65. }
  66. environment_check
  67. if [[ "$*" == "push" ]]; then
  68. push_and_publish next
  69. elif [[ "$*" == "start minor" ]]; then
  70. log "Creating new minor pre-release"
  71. assert_current_branch master
  72. # Create temporary release branch
  73. git checkout -b release-temp
  74. # This bumps minor and adds prerelease suffix, commits the changes, and tags the commit
  75. npm version preminor --preid="$PRERELEASE_SUFFIX"
  76. # Rename the release branch
  77. git branch --move "$(current_release_branch)"
  78. push_and_publish next
  79. elif [[ "$*" == "start major" ]]; then
  80. log "Creating new major pre-release"
  81. assert_current_branch master
  82. # Create temporary release branch
  83. git checkout -b release-temp
  84. # This bumps major and adds prerelease suffix, commits the changes, and tags the commit
  85. npm version premajor --preid="$PRERELEASE_SUFFIX"
  86. # Rename the release branch
  87. git branch --move "$(current_release_branch)"
  88. push_and_publish next
  89. elif [[ "$*" == "rc" ]]; then
  90. log "Bumping pre-release"
  91. assert_current_branch "$(current_release_branch)"
  92. # Bumps prerelease number, commits and tags
  93. npm version prerelease
  94. push_and_publish next
  95. elif [[ "$*" == "final" ]]; then
  96. # Update changelog release date, remove prerelease suffix, tag, push to git, publish in npm, remove next dist-tag
  97. log "Creating final release"
  98. assert_current_branch "$(current_release_branch)"
  99. # This will remove the prerelease suffix from the version
  100. npm version patch
  101. push_release_branch_and_tag
  102. push_and_publish latest
  103. log "Remember to merge the release branch into master and push upstream"
  104. else
  105. log "Unknown command: '$*'"
  106. exit 1
  107. fi