release.sh 2.7 KB

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