Skip to content

Instantly share code, notes, and snippets.

@ricardoboss
Created January 16, 2021 04:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricardoboss/de05920c5296e851490743d03d1158c8 to your computer and use it in GitHub Desktop.
Save ricardoboss/de05920c5296e851490743d03d1158c8 to your computer and use it in GitHub Desktop.
Deploy script for NPM (Vue) websites and git-auto-deploy
#!/usr/bin/env bash
# change to location of script
SCRIPT_PATH="${0%/*}"
if [[ "$0" != "$SCRIPT_PATH" && "$SCRIPT_PATH" != "" ]]; then
cd "$SCRIPT_PATH" || exit
fi
timestamp=$(date +"%Y-%m-%d_%H-%M-%S")
echo "Executing deploy script at $timestamp"
# check current user
cur_user=$(whoami)
if [[ "$cur_user" != 'git-auto-deploy' ]]; then
echo "Invalid user! Got: $cur_user" >&2
exit 1
fi
# try to get npm executable
npm_exec=$(which npm)
if [[ $? -ne 0 ]]; then
echo "Cannot find npm executable!" >&2
exit 2
fi
# try to get node executable
node_exec=$(which node)
if [[ $? -ne 0 ]]; then
echo "Cannot find node executable!" >&2
exit 2
fi
# try to get the npm version
npm_ver=$(eval "$npm_exec -v")
if [[ $? -ne 0 ]]; then
echo "Cannot get version of npm using $npm_exec" >&2
exit 3
fi
# try to get the node version
node_ver=$(eval "$node_exec -v")
if [[ $? -ne 0 ]]; then
echo "Cannot get version of node using $node_exec" >&2
exit 3
fi
# check for minimal npm version
if [[ "$(dpkg --compare-versions "$npm_ver" ge "6.14.8")" -ne 0 ]]; then
echo "npm needs to be at least version 6.14.8! Is $npm_ver at $npm_exec" >&2
exit 4
fi
# check for minimal node version
if [[ "$(dpkg --compare-versions "$(echo "$node_ver" | cut -c2-)" ge "12.19.0")" -ne 0 ]]; then
echo "node needs to be at least version 12.19.0! Is $node_ver at $node_exec" >&2
exit 4
fi
echo "Using npm $npm_ver at $npm_exec"
echo "Using node $node_ver at $node_exec"
# switch to root directory
pushd .. >/dev/null
# use 'npm ci' instead of 'npm install' because it is optimized for automated deployment (non-interactive)
echo "Installing dependencies using 'npm ci'"
$npm_exec ci
# remove previous build
dist_dir="$(pwd)/dist"
echo "Removing previous build (if any) from: $dist_dir"
rm -rf "$dist_dir"
# get NODE_ENV with fallback
ENV=${NODE_ENV:-staging}
# pass NODE_ENV variable to node
echo "Running 'npm run build' with NODE_ENV '$ENV'"
NODE_ENV=ENV "$npm_exec" run build
if [[ $? -ne 0 ]]; then
echo "Build exited with non-zero exit code. Cancelling deployment." >&2
exit 5
fi
# check if we are staging or deploying to production
if [[ "$ENV" == 'production' ]]; then
root_dir=/var/www/example.com
else
root_dir=/var/www/test.example.com
fi
public_dir=$root_dir/public_html
target_dir=$root_dir/$timestamp
# create target directory and copy files
echo "Deploying to target directory: $target_dir"
# create directory so group can write to it
mkdir -p "$target_dir"
chmod 775 "$target_dir"
# copy all files inside dist/ to target directory
cp -r "$dist_dir"/* "$target_dir"/
# check if the target directory was created and copy was successful
if [[ $? -ne 0 || ! -d "$target_dir" ]]; then
echo "Failed to copy deployment files to destination. Cancelling deployment." >&2
exit 6
fi
# quick sanity check
if [[ ! -f "$target_dir"/index.html ]]; then
echo "Cannot find 'index.html' in target directory. Something must be broken. Cancelling deployment." >&2
exit 7
fi
echo "Symlinking 'public_html' to latest release"
# resolve and save path to previous release
previous_target_dir=$(readlink -f "$public_dir")
# remove link to previous release
rm -f "$public_dir"
# link new target to public
ln -s "$target_dir" "$public_dir"
# check public dir is a link and target exists
if [[ $? -ne 0 || ! -L $public_dir || ! -e $public_dir ]]; then
echo "Linking public folder failed. Cancelling deployment." >&2
# remove link if it exists
rm -f "$public_dir"
# link to previous target
ln -s "$previous_target_dir" "$public_dir"
exit 8
fi
echo "Updating .htaccess in $root_dir"
cp -ub ./.htaccess "$root_dir"/
echo "Done!"
@ricardoboss
Copy link
Author

One might want to update the .htaccess copy command to this: cp -ub --no-preserve=all ./.htaccess "$root_dir"/

The main reason being that you may not want to overwrite the permissions of the .htaccess to keep it readable for your webserver.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment