Created
March 25, 2024 02:34
-
-
Save rutcreate/f14aac11e91afb8463a386c612c8d393 to your computer and use it in GitHub Desktop.
Deploy script
This file contains 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 | |
branch_name='' | |
force_export=false | |
usage() { | |
echo "Usage: $0 <prod|uat> [OPTIONS]" | |
echo "Options:" | |
echo " -h, --help Display this help message" | |
echo " -f, --force Force export (even no new commit)" | |
} | |
handle_options() { | |
branch_name=$1 | |
shift | |
while [ $# -gt 0 ]; do | |
case $1 in | |
-h | --help) | |
usage | |
exit 0 | |
;; | |
-f | --force) | |
force_export=true | |
shift | |
;; | |
*) | |
echo "Invalid option: $1" >&2 | |
usage | |
exit 1 | |
;; | |
esac | |
shift | |
done | |
} | |
handle_options "$@" | |
# validate branch_name must be either prod or uat | |
if [ "$branch_name" != 'prod' ] && [ "$branch_name" != 'uat' ]; then | |
usage | |
exit 1 | |
fi | |
confirm() { | |
# ANSI escape codes for bold and yellow text | |
bold=$(tput bold) | |
yellow=$(tput setaf 3) | |
reset=$(tput sgr0) | |
read -r -p "Are you sure you want to deploy ${bold}${yellow}$branch_name${reset}? [y/N] " response | |
case "$response" in | |
[yY][eE][sS]|[yY]) | |
true | |
;; | |
*) | |
false | |
;; | |
esac | |
} | |
run() { | |
echo "Deploy $branch_name in progress..." | |
if ! confirm; then | |
echo "Deployment cancelled" | |
exit 0 | |
fi | |
git checkout $branch_name | |
if [ $? -ne 0 ]; then | |
echo "Cannot checkout $branch_name" | |
exit 1 | |
fi | |
# Collect rev commit | |
rev_commit=$(git rev-parse HEAD) | |
# pull latest code | |
git pull | |
# Check if there is any changes and no force export | |
if [ $(git rev-parse HEAD) == $rev_commit ] && [ $force_export == false ]; then | |
echo "No changes" | |
exit 0 | |
fi | |
yarn && | |
yarn build | |
} | |
run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment