From bbfe0dbcc68515b59367738e8b9ac18f52fb7e97 Mon Sep 17 00:00:00 2001 From: Mick Ryan Date: Tue, 10 Dec 2019 14:55:17 -0800 Subject: [PATCH] chore(build): Add dry run option --- build/release.sh | 94 ++++++++++++++++++++++++++++-------------------- 1 file changed, 55 insertions(+), 39 deletions(-) diff --git a/build/release.sh b/build/release.sh index 0dde46c0a..4cfae8ae1 100755 --- a/build/release.sh +++ b/build/release.sh @@ -6,6 +6,19 @@ export NODE_PATH=$NODE_PATH:./node_modules OLD_VERSION="XXX" VERSION="XXX" +dry_run=false + +# Wraps mutating commands with echo if dry_run is enabled. +cmd() { + if $dry_run; then + echo "The build script would run $*" + return 0 + fi + + eval "$@" + + return 0 +} # Major, minor, or patch release major_release=false @@ -17,69 +30,65 @@ reset_tags() { echo "----------------------------------------------------------------------" echo "Wiping local tags" echo "----------------------------------------------------------------------" - git tag -l | xargs git tag -d || return 1 + cmd git tag -l | xargs git tag -d || return 1 # Add the upstream remote if it is not present if ! git remote get-url github-upstream; then - git remote add github-upstream git@github.com:box/box-content-preview.git || return 1 + cmd git remote add github-upstream git@github.com:box/box-content-preview.git || return 1 fi # Fetch latest code with tags echo "----------------------------------------------------------------------" echo "Fetching latest upstream code + tags" echo "----------------------------------------------------------------------" - git fetch --tags github-upstream || return 1; + cmd git fetch --tags github-upstream || return 1 } - reset_to_previous_version() { if OLD_VERSION === "XXX"; then echo "----------------------------------------------------------------------" echo "Error while cleaning workspace!" echo "----------------------------------------------------------------------" - return 1; + return 1 fi # Reset and fetch upstream with tags - reset_tags || return 1; + reset_tags || return 1 # Reset to previous release version and clear unstashed changes echo "----------------------------------------------------------------------" echo "Resetting to v" $OLD_VERSION echo "----------------------------------------------------------------------" - git reset --hard OLD_VERSION || return 1 - git clean -f || return 1 + cmd git reset --hard OLD_VERSION || return 1 + cmd git clean -f || return 1 } - reset_to_master() { # Update to latest code on GitHub master - git checkout master || return 1 + cmd git checkout master || return 1 # Reset and fetch upstream with tags - reset_tags || return 1; + cmd reset_tags || return 1 # Reset to latest code and clear unstashed changes echo "----------------------------------------------------------------------" echo "Resetting to upstream/master" echo "----------------------------------------------------------------------" - git reset --hard github-upstream/master || return 1 - git clean -f || return 1 + cmd git reset --hard github-upstream/master || return 1 + cmd git clean -f || return 1 } - build_lint_and_test() { # The build command includes linting - yarn build && yarn test || return 1 + cmd yarn build && cmd yarn test || return 1 } - increment_version() { # Old version OLD_VERSION=$(./build/current_version.sh) # The current branch should not match the previous release tag - if [[ $(git log --oneline ...v$OLD_VERSION) == "" ]] ; then + if [[ $(git log --oneline ...v$OLD_VERSION) == "" ]]; then echo "----------------------------------------------------" echo "Your release has no new commits!" echo "----------------------------------------------------" @@ -107,13 +116,12 @@ increment_version() { VERSION=$(./build/current_version.sh) } - update_changelog() { echo "----------------------------------------------------------------------" echo "Updating CHANGELOG.md" echo "----------------------------------------------------------------------" - if ./node_modules/.bin/conventional-changelog -i CHANGELOG.md --same-file; then + if cmd ./node_modules/.bin/conventional-changelog -i CHANGELOG.md --same-file; then echo "----------------------------------------------------------------------" echo "Updated CHANGELOG successfully" echo "----------------------------------------------------------------------" @@ -125,28 +133,26 @@ update_changelog() { fi } - update_readme() { echo "----------------------------------------------------------------------" echo "Updating README" echo "----------------------------------------------------------------------" # Replace 'v{VERSION}' string - sed -i -e "s@v$OLD_VERSION@v$VERSION@g" README.md + cmd sed -i -e "s@v$OLD_VERSION@v$VERSION@g" README.md # Replace 'preview/{VERSION}' string - sed -i -e "s@preview/$OLD_VERSION@preview/$VERSION@g" README.md + cmd sed -i -e "s@preview/$OLD_VERSION@preview/$VERSION@g" README.md - rm README.md-e + cmd rm README.md-e } - push_to_github() { # Add new files - git commit -am "chore(release): $VERSION" + cmd git commit -am "chore(release): $VERSION" # Force update tag after updating files - git tag -a v$VERSION -m $VERSION + cmd git tag -a v$VERSION -m $VERSION echo "----------------------------------------------------------------------" echo "Master version is now at" $VERSION @@ -154,7 +160,7 @@ push_to_github() { # Push release to GitHub if $patch_release; then - if git push github-upstream v$VERSION --no-verify; then + if cmd git push github-upstream v$VERSION --no-verify; then echo "----------------------------------------------------------------------" echo "Pushed version" $VERSION "to git successfully" echo "----------------------------------------------------------------------" @@ -165,7 +171,7 @@ push_to_github() { return 1 fi else - if git push github-upstream master --tags --no-verify; then + if cmd git push github-upstream master --tags --no-verify; then echo "----------------------------------------------------------------------" echo "Pushed version" $VERSION "to git successfully" echo "----------------------------------------------------------------------" @@ -181,6 +187,12 @@ push_to_github() { # Check out latest code from git, build assets, increment version, and push tags push_new_release() { # Get latest commited code and tags + if $dry_run; then + echo "----------------------------------------------------------------------" + echo "Running in Dry Run Mode!" + echo "----------------------------------------------------------------------" + fi + if $patch_release; then echo "----------------------------------------------------------------------" echo "Starting patch release - skipping reset to master" @@ -217,21 +229,26 @@ push_new_release() { echo "----------------------------------------------------------------------" echo "Pushing new GitHub release" echo "----------------------------------------------------------------------" - ./node_modules/.bin/conventional-github-releaser + cmd ./node_modules/.bin/conventional-github-releaser return 0 } - # Check if we are doing major, minor, or patch release -while getopts "mnp" opt; do +while getopts "mnpd" opt; do case "$opt" in - m ) - major_release=true ;; - n ) - minor_release=true ;; - p ) - patch_release=true ;; + m) + major_release=true + ;; + n) + minor_release=true + ;; + p) + patch_release=true + ;; + d) + dry_run=true + ;; esac done @@ -245,7 +262,6 @@ if ! push_new_release; then echo "Cleaning workspace by checking out master and removing tags" echo "----------------------------------------------------------------------" - if $patch_release; then # Only reset to previous version for patch releases reset_to_previous_version || return 1 @@ -259,6 +275,6 @@ if ! push_new_release; then echo "----------------------------------------------------------------------" echo "Workspace succesfully cleaned!" echo "----------------------------------------------------------------------" - fi; + fi exit 1 fi