Skip to content

Commit

Permalink
feat: auto push to DeployRepo and Server and RUN!
Browse files Browse the repository at this point in the history
  • Loading branch information
SolidZORO committed Jun 13, 2020
1 parent 7e3097b commit a7a91ac
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 17 deletions.
82 changes: 68 additions & 14 deletions packages/leaa-api/deploy.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
#! /bin/bash

unset PLATFORM YARN_BUILD
unset PLATFORM YARN_BUILD PM2_SETUP

cd "$(dirname "$0")" || exit

__DEPLOY__="./_deploy"



usage() {
# -p = platform
# -i = ignore yarn build
# -S = Setup (pm2)

# shellcheck disable=SC2028
echo "\n\n\n\n🔰 Usage: $0 -p local_start|docker_start|docker_install|vercel [-i] (e.g. sh -p test)\n\n\n\n"
echo "\n\n
🔰 Usage: $0 -p (node_start | docker_start | docker_install | push_to_repo | sync_run | vercel) [-i] [-S]
\n
-p platform
-i ignore yarn build
-S Setup (init PM2 deploy)
\n
e.g. sh deploy.sh -p sync_run -S
\n\n"
exit 2
}

Expand All @@ -18,11 +31,11 @@ set_var() {
shift

# shellcheck disable=SC2028
echo "VAR { $arg_name: $* }"
echo "BASH-VAR { $arg_name: $* }"

if [ -z "${!arg_name}" ]; then
if [ "$arg_name" = "PLATFORM" ]; then
if echo "$*" | grep -Eq '^local_start|docker_start|docker_install|vercel'; then
if echo "$*" | grep -Eq '^node_start|docker_start|docker_install|push_to_repo|sync_run|vercel'; then
eval "$arg_name=\"$*\""
else
usage
Expand All @@ -33,7 +46,7 @@ set_var() {
eval "$arg_name=\"$*\""
fi

if [ "$arg_name" = "YARN_BUILD" ]; then
if [ "$arg_name" = "PM2_SETUP" ]; then
eval "$arg_name=\"$*\""
fi

Expand All @@ -43,11 +56,10 @@ set_var() {
fi
}


platform_docker_install() {
if [ -f "${__DEPLOY__}/.env" ]; then
# shellcheck disable=SC2028
echo '\n Already .env, Do not Copy :)\n'
echo '\n👌 Already .env, Do not Copy :)\n'
else
cp -f ./.env ${__DEPLOY__}
fi
Expand All @@ -69,7 +81,36 @@ platform_docker_install() {

docker-compose -f docker-compose-deploy-yarn-install.yml down && docker-compose -f docker-compose-deploy-yarn-install.yml up

echo '\n\n\n\n🎉 All Dependencies Installation Completed!\n\n\n\n\n'
echo '\n\n🎉 All Dependencies Installation Completed!\n\n\n'
}

platform_push_to_repo() {
platform_docker_install;

pwd
# shellcheck disable=SC2002
GIT_MESSAGE=$(cat ./public/version.txt | sed 's/["{}]//g' | sed 's/[,]/ /g')

git status
git add -A
git commit -m "$GIT_MESSAGE"
git push -u origin api

echo '\n\n\n📮 Push To Deploy Repo Completed!\n\n\n'
}

platform_sync_run() {
platform_push_to_repo;

if [ "$PM2_SETUP" = "true" ]; then
pm2 deploy api setup

echo '\n\n🚚 PM2 Setup Completed!\n\n\n'
fi

pm2 deploy api

echo "\n\n\n\n✅ PM2 Deploy Completed! <$GIT_MESSAGE>\n\n\n\n"
}

platform_vercel() {
Expand All @@ -79,7 +120,7 @@ platform_vercel() {
vercel --prod -c
}

platform_local_start() {
platform_node_start() {
cd ${__DEPLOY__} || exit

yarn start
Expand All @@ -94,17 +135,17 @@ platform_docker_start() {

# ------------------------------------------------------------------------

while getopts 'p:i?h' arg
while getopts 'p:i?S?h' arg
do
# shellcheck disable=SC2220
case $arg in
p) set_var PLATFORM "$OPTARG" ;;
i) set_var YARN_BUILD ignore ;;
S) set_var PM2_SETUP true ;;
h|?) usage ;;
*) usage ;; esac
done


echo "\x1B[96m
___ ___ ____ ${PLATFORM}
Expand All @@ -118,7 +159,7 @@ echo "\x1B[96m

[ -z "$PLATFORM" ] && usage

CONFIRM_MESSAGE=$(printf "\n\n🤖 \033[1m Start Deploy <%s> ?\033[0m (Enter/n)" "${PLATFORM}")
CONFIRM_MESSAGE=$(printf "\n\n🔰 \033[1m Start Deploy 👉 <%s> ?\033[0m (Enter/n)" "${PLATFORM}")
read -p "${CONFIRM_MESSAGE}" -n 1 -r KEY


Expand All @@ -137,7 +178,18 @@ if [ "$KEY" = "" ]; then
cp -fr ./_dist/* ${__DEPLOY__}
cp -f ./tools/deploy-config/server/index.js ${__DEPLOY__}
cp -f ./tools/deploy-config/server/package.json ${__DEPLOY__}
cp -f ./.gitignore ${__DEPLOY__}


if [ -f "./ecosystem.config.js" ]; then
cp -f ./.gitignore ${__DEPLOY__}
fi

if [ -f "${__DEPLOY__}/.gitignore" ]; then
# shellcheck disable=SC2028
echo '\n\n👌 Already .gitignore, Do not Copy :)\n'
else
cp -f ./.gitignore ${__DEPLOY__}
fi

#/assets (copy and then delete some gen files)
if [ ! -d ${__DEPLOY__}/src/assets ]; then
Expand All @@ -164,9 +216,11 @@ if [ "$KEY" = "" ]; then
# -----------
if [ -n "$PLATFORM" ]; then
case $PLATFORM in
local_start) platform_local_start ;;
node_start) platform_node_start ;;
docker_start) platform_docker_start ;;
docker_install) platform_docker_install ;;
push_to_repo) platform_push_to_repo ;;
sync_run) platform_sync_run ;;
vercel) platform_vercel ;;
*) usage ;; esac
fi
Expand Down
2 changes: 1 addition & 1 deletion packages/leaa-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"postbuild": "node ./tools/cli/gen-version.js && node ./tools/cli/gen-production-package.js",
"build": "yarn clear && NODE_ENV=production ttsc -b tsconfig.build.json",
"---------------------------------------------------------------------- DOCKER ----": "----",
"docker-install-notes": "⚠️ All the install actions of yarn are completed in the local docker, and then created git repo for deployment, all dist files (includes node_modules dir) push to this repo. It will be very large for the init time, but it will be very small afterwards, and when needed Can be rolled back reliably and conveniently. 所有关于 yarn 的安装动作都在本地的 docker 中完成,然后新建一个专门用于部署的 git-repo,然后把所有 dist 文件(包含完整的 node_modules)提交到这个 repo,第一次提交体积会很大,但是之后就小了,并且在需要的时候可以很方便、可靠的回滚。",
"docker-install-notes": "⚠️ All the install actions of yarn are completed in the local docker, and then created git repo for deployment, all dist files (includes node_modules dir) push to this repo. It will be very large for the init time, but it will be very small afterwards, and when needed Can be rolled back reliably and conveniently. (More See DEPLOYMENT.md) 所有关于 yarn 的安装动作都在本地的 docker 中完成,然后新建一个专门用于部署的 git-repo,然后把所有 dist 文件(包含完整的 node_modules)提交到这个 repo,第一次提交体积会很大,但是之后就小了,并且在需要的时候可以很方便、可靠的回滚。(更多细节请阅读 DEPLOYMENT.md)",
"docker-install": "NODE_ENV=production yarn install --production && yarn add pm2",
"docker-start": "NODE_ENV=production pm2-docker start",
"---------------------------------------------------------------------- SERVER ----": "----",
Expand Down
2 changes: 1 addition & 1 deletion packages/leaa-api/tools/cli/gen-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const DIST_PUBLIC_DIR = path.resolve(__dirname, '../../public');
const DIST_FILE_PATH = `${DIST_PUBLIC_DIR}/version.txt`;

const buildInfo = {
BUILDTIME: moment().format('YYYYMMDD-HHmmss'),
VERSION: getVersion,
BUILDTIME: moment().format('YYYYMMDD-HHmmss'),
};

if (!fs.existsSync(DIST_PUBLIC_DIR)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/leaa-dashboard/tools/webpack/_plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ const htmlWebpackPluginOption = {
).toString('base64'),
__BUILD_DATA__: Buffer.from(
JSON.stringify({
BUILDTIME: moment().format('YYYYMMDD-HHmmss'),
VERSION: `v${process.env.npm_package_version}`,
MODE: WPCONST.MODE,
BUILDTIME: moment().format('YYYYMMDD-HHmmss'),
}),
).toString('base64'),
__ANALYTICS_CODE__:
Expand Down

0 comments on commit a7a91ac

Please sign in to comment.