Last active
March 18, 2016 16:44
-
-
Save mmollick/4809e91fc4a2d008a60b to your computer and use it in GitHub Desktop.
A bash script that builds and packages our application. It automatically commits and tags it enforcing semver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| printHelp () { | |
| printf "This utility will create the proper release commit to be used for deployment\n" | |
| printf "Options:\n" | |
| printf " -v|--version Explicitly define the version number\n" | |
| printf " -M|--major Bump the major version number\n" | |
| printf " -m|--minor Bump the minor version number\n" | |
| printf " -p|--patch Bump the patch version number\n" | |
| printf " -r|--rebundle Forces Gulp & Webpack compilation\n" | |
| printf "\n" | |
| printf "Usage:\n" | |
| printf " \$ ./release <options>\n" | |
| printf "\n" | |
| printf "Copyright 2016 NeverBounce\n" | |
| printf "Written by: Michael Mollick\n" | |
| printf "\n" | |
| exit 0 | |
| } | |
| if [ -z $1 ] | |
| then | |
| printHelp | |
| fi | |
| # Get latest version number | |
| read -r latest_version < './VERSION' | |
| # Parse version number | |
| IFS="." && Array=($latest_version) | |
| latest_major=${Array[0]} | |
| latest_minor=${Array[1]} | |
| latest_patch=${Array[2]} | |
| # Parse command arguments | |
| MAJOR=false; | |
| MINOR=false; | |
| PATCH=false; | |
| FORCE_REBUNDLE=false; | |
| VERSION=""; | |
| for i in "$@" | |
| do | |
| case $i in | |
| -M|--major) | |
| MAJOR=true | |
| shift # past argument=value | |
| ;; | |
| -m|--minor) | |
| MINOR=true | |
| shift # past argument=value | |
| ;; | |
| -p|--patch) | |
| PATCH=true | |
| shift # past argument=value | |
| ;; | |
| -r|--rebundle) | |
| FORCE_REBUNDLE=true | |
| shift # past argument=value | |
| ;; | |
| -v=*|--version=*) | |
| VERSION="${i#*=}" | |
| shift # past argument with no value | |
| ;; | |
| *) | |
| # unknown option | |
| printf "\e[30;48;5;1m An unrecognized option was supplied \e[0m\n\n" | |
| printHelp | |
| exit 0 | |
| ;; | |
| esac | |
| done | |
| # Build & Verify version number | |
| if [ -z $VERSION ] | |
| # Version number was not explicitly defined, build based on args | |
| then | |
| printf "Explicit version not found, attempting to build version...\n" | |
| if [ $MAJOR == true ] | |
| then | |
| new_major=$(( $latest_major + 1 )) | |
| new_minor=0 | |
| new_patch=0 | |
| elif [ $MINOR == true ] | |
| then | |
| new_major=$latest_major | |
| new_minor=$(( $latest_minor + 1 )) | |
| new_patch=0 | |
| else | |
| new_major=$latest_major | |
| new_minor=$latest_minor | |
| new_patch=$(( $latest_patch + 1 )) | |
| fi | |
| VERSION="${new_major}.${new_minor}.${new_patch}" | |
| else | |
| if [[ ! $VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] | |
| then | |
| printf "\e[30;48;5;1m The version should start with a number and follow semantic versioning (major.minor.patch) \e[0m\n" | |
| exit 0 | |
| else | |
| IFS="." && Array=($1) | |
| new_major=${Array[0]} | |
| new_minor=${Array[1]} | |
| new_patch=${Array[2]} | |
| if [ $new_major -lt $latest_major ] | |
| then | |
| printf "\e[30;48;5;1m The new major version should be greater or equal to the previous version ($latest_major) \e[0m\n" | |
| exit 0 | |
| elif [ $new_minor -lt $latest_minor ] | |
| then | |
| printf "\e[30;48;5;1m The new minor version should be greater or equal to the previous version ($latest_minor) \e[0m\n" | |
| exit 0 | |
| elif [ $new_patch -le $latest_patch ] | |
| then | |
| printf "\e[30;48;5;1m The new patch version should be greater than the previous version ($latest_patch) \e[0m\n" | |
| exit 0 | |
| fi | |
| fi | |
| fi | |
| printf "\e[30;48;5;82m Packaging v${VERSION} \e[0m\n" | |
| # Lets check changed files to determine | |
| # if we need to recompile our assets (sass & js) | |
| echo `git diff --name-only "v${latest_version}" HEAD` > './DIFF' | |
| NEEDS_REBUNDLED=false | |
| while read p; do | |
| if [[ $p =~ ^(resources\/assets) ]] | |
| then | |
| NEEDS_REBUNDLED=true | |
| fi | |
| done < './DIFF' | |
| rm -f './DIFF' | |
| git checkout releases | |
| git merge -X theirs development -m "Merging development for version ${VERSION}" | |
| if [ $NEEDS_REBUNDLED == true ] || [ $FORCE_REBUNDLE == true ] | |
| then | |
| printf "\e[30;48;5;82m Building version ${VERSION}... \e[0m\n" | |
| printf "\e[30;48;5;82m Cleaning up old build... \e[0m\n" | |
| rm -Rf ./public/build/ | |
| printf "\e[30;48;5;82m Building webpack bundle for ${VERSION}... \e[0m\n" | |
| npm run build | |
| printf "\e[30;48;5;82m Building gulp bundle for ${VERSION}... \e[0m\n" | |
| NODE_ENV=production gulp --production | |
| printf "\e[30;48;5;82m Build completed! \e[0m\n" | |
| else | |
| printf "\e[30;48;5;82m Nothing to build for ${VERSION}... \e[0m\n" | |
| fi | |
| printf "\e[30;48;5;82m Creating commit... \e[0m\n" | |
| git add * | |
| git commit -m "Build version ${VERSION}" | |
| git tag "v${VERSION}" | |
| git push origin releases --tags | |
| git checkout development | |
| printf "\e[30;48;5;82m Saving version number to file... \e[0m\n" | |
| echo "${VERSION}" > './VERSION' | |
| git add VERSION | |
| git commit -m "Updating VERSION file to ${VERSION}" | |
| git push origin development | |
| printf "\e[30;48;5;82m Packaging completed! \e[0m\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment