release.sh 3.3 KB

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