Skip to content

Commit

Permalink
feat: add deploy bash script
Browse files Browse the repository at this point in the history
  • Loading branch information
SolidZORO committed Jun 6, 2020
1 parent 0f119e4 commit d3914d1
Show file tree
Hide file tree
Showing 35 changed files with 227 additions and 184 deletions.
9 changes: 2 additions & 7 deletions packages/leaa-api/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@
#Accests
/public/attachments
/public/buildinfo.json

/public/assets/divisions/*.json

/src/assets/dicts/*
!/src/assets/dicts/.gitkeep
/src/assets/divisions/*
!/src/assets/divisions/.gitkeep
/public/version.txt
/public/logs

# Compiled
/.cache
Expand Down
6 changes: 5 additions & 1 deletion packages/leaa-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ leaa-api base [Nest.js](https://github.com/nestjs/nest) framework.
cp .env.example .env

# .env for production
cp .env.example .env.production # for production
cp .env.example .env.production

# run mysql with docker-compose
docker-compose down && docker-compose up -d
Expand All @@ -23,6 +23,10 @@ yarn insatll && yarn seed --nuke
yarn dev
```

## **DEPLOY**

more step see `deploy-local-test.sh`

## **TIPS**

starting from `v1.0.4`, the api also supports `i18n`.
Expand Down
62 changes: 0 additions & 62 deletions packages/leaa-api/deploy-build.sh

This file was deleted.

23 changes: 0 additions & 23 deletions packages/leaa-api/deploy-heroku.sh

This file was deleted.

14 changes: 0 additions & 14 deletions packages/leaa-api/deploy-now.sh

This file was deleted.

143 changes: 141 additions & 2 deletions packages/leaa-api/deploy.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,145 @@
#! /bin/bash

unset PLATFORM IGNORE_BUILD

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

sh deploy-build.sh 'HEROKU' && sh deploy-heroku.sh
#sh deploy-build.sh 'VERCEL.SH' && sh deploy-vercel.sh
__DEPLOY__="./_deploy"


usage() {
# shellcheck disable=SC2028
echo "\n🔰 Usage: $0 -p test|ecs|vercel|heroku [-i] (e.g. sh -p test)\n"
exit 2
}

set_var() {
local arg_name=$1
shift

if [ -z "${!arg_name}" ]; then
if echo "$*" | grep -Eq '^test|ecs|vercel|heroku$'; then
eval "$arg_name=\"$*\""
else
usage
fi

else
echo "Error: $arg_name already set"
usage
fi
}

platform_vercel() {
cp -fr ./tools/deploy-config/vercel/* ${__DEPLOY__}
cd ${__DEPLOY__} || exit

vercel --prod -c
}

platform_ecs() {
cd ${__DEPLOY__} || exit

ls
}

platform_test() {
cd ${__DEPLOY__} || exit

yarn start
}

platform_heroku() {
LOCAL_TIME=$(date "+%Y-%m-%d %H:%M:%S")

DEPLOY_HEROKU_APP_NAME="test-leaa-api"
DEPLOY_COMMIT="update AUTO-DEPLOY ${DEPLOY_HEROKU_APP_NAME} @ ${LOCAL_TIME}"

cd ${__DEPLOY__} || exit
git init
git remote add heroku https://git.heroku.com/${DEPLOY_HEROKU_APP_NAME}.git
git add -A
git commit -m "${DEPLOY_COMMIT}"
git push -f heroku master
}


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

echo "\x1B[96m
___ ___ ____ ${PLATFORM}
/ _ | / _ \/ _/
/ __ |/ ___// /
/_/ |_/_/ /___/
\x1B[0m"

[ -z "$PLATFORM" ] && usage

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


if [[ $KEY = "" ]]; then
# ---------
# @ROOT-DIR
# ---------
if [[ $IGNORE_BUILD != "yes" ]]; then
yarn build
fi

#/
if [ ! -d ${__DEPLOY__} ]; then
mkdir -p ${__DEPLOY__}
fi
cp -fr ./_dist/* ${__DEPLOY__}
cp -f ./tools/deploy-config/ecs/index.js ${__DEPLOY__}
cp -f ./.env.example ${__DEPLOY__}
cp -f ./.gitignore ${__DEPLOY__}

#/assets (copy and then delete some gen files)
if [ ! -d ${__DEPLOY__}/src/assets ]; then
mkdir -p ${__DEPLOY__}/src/assets
fi
cp -fr ./src/assets/* ${__DEPLOY__}/src/assets

#delete some gen files
rm -f ./src/assets/dicts/*.dict.txt
rm -f ./src/assets/divisions/*.division.json

#public
if [ ! -d ${__DEPLOY__}/public ]; then
mkdir -p ${__DEPLOY__}/public
fi
cp -f ./public/robots.txt ${__DEPLOY__}/public
cp -f ./public/favicon.ico ${__DEPLOY__}/public
cp -f ./public/get-weixin-code.html ${__DEPLOY__}/public
cp -f ./public/version.txt ${__DEPLOY__}/public


# -----------
# @DEPLOY-DIR
# -----------
if [ -n "$PLATFORM" ]; then
case $PLATFORM in
test) platform_test ;;
ecs) platform_test ;;
vercel) platform_vercel ;;
heroku) platform_heroku ;;
*) usage ;; esac
fi

else
# shellcheck disable=SC2028
echo "\nCancel Deploy\n"
fi
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ const path = require('path');
const mkdirp = require('mkdirp');
const moment = require('moment');

const DIST_PUBLIC_DIR = path.resolve(__dirname, './public');
const BUILDINFO_PATH = `${DIST_PUBLIC_DIR}/buildinfo.json`;
const { getVersion } = require('./tools/cli/get-build-info');

const DIST_PUBLIC_DIR = path.resolve('./public');
const BUILDINFO_PATH = `${DIST_PUBLIC_DIR}/version.txt`;

const buildInfo = {
BUILDTIME: moment().format('YYYYMMDD-HHmmss'),
VERSION: `v${process.env.npm_package_version}`,
VERSION: getVersion,
};

if (!fs.existsSync(DIST_PUBLIC_DIR)) {
Expand All @@ -17,4 +19,6 @@ if (!fs.existsSync(DIST_PUBLIC_DIR)) {
mkdirp(DIST_PUBLIC_DIR, (err) => err && console.log(JSON.stringify(err)));
}

console.log('\n\n🌈 DONE-GEN-VERSION-FILE', '\n\n');

fs.writeFileSync(BUILDINFO_PATH, JSON.stringify(buildInfo));
3 changes: 0 additions & 3 deletions packages/leaa-api/index.js

This file was deleted.

3 changes: 2 additions & 1 deletion packages/leaa-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"---------------------------------------------------------------------- PROD ----": "----",
"prod": "NODE_ENV=production ts-node-dev --respawn --transpileOnly -r tsconfig-paths/register ./src/main.ts",
"---------------------------------------------------------------------- SERVER ----": "----",
"build": "yarn clear && NODE_ENV=production ttsc -b tsconfig.build.json && node gen-buildinfo.js",
"postbuild": "node gen-version.js",
"build": "yarn clear && NODE_ENV=production ttsc -b tsconfig.build.json",
"start": "NODE_ENV=production node ./index.js",
"---------------------------------------------------------------------- ALIAS ----": "----",
"commit": "git-cz",
Expand Down
2 changes: 2 additions & 0 deletions packages/leaa-api/public/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/attachments
buildinfo.json
1 change: 1 addition & 0 deletions packages/leaa-api/public/version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"BUILDTIME":"20200606-195756","VERSION":"v3.2.0 (0f11)"}
1 change: 1 addition & 0 deletions packages/leaa-api/src/assets/_README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
if deploy, MUST manual copy this dir to `_deploy/src/assets`
2 changes: 2 additions & 0 deletions packages/leaa-api/src/assets/divisions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/*.json
!/source
Empty file.
6 changes: 3 additions & 3 deletions packages/leaa-api/src/configs/captcha.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ConfigObject } from 'svg-captcha';
import path from 'path';
import { envConfig } from '@leaa/api/src/modules/v1/config/config.module';
import { ConfigObject } from 'svg-captcha';

const SVG_CAPTCHA_OPTION: ConfigObject = {
noise: 3,
Expand All @@ -13,7 +12,8 @@ const SVG_CAPTCHA_OPTION: ConfigObject = {
// background: '#cc9966',
};

const SVG_CAPTCHA_FONT_PATH: string = path.resolve(__dirname, `../../${envConfig.PUBLIC_DIR}/assets/fonts/halva.otf`);
const FONT_DIR = path.resolve(__dirname, '../assets/fonts');
const SVG_CAPTCHA_FONT_PATH = `${FONT_DIR}/halva.otf`;

export const captchaConfig = {
SVG_CAPTCHA_OPTION,
Expand Down
3 changes: 1 addition & 2 deletions packages/leaa-api/src/configs/division.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import path from 'path';
import { envConfig } from '@leaa/api/src/modules/v1/config/config.module';

const DIVISION_DIR = path.resolve(__dirname, `../../${envConfig.PUBLIC_DIR}/assets/divisions`);
const DIVISION_DIR = path.resolve(__dirname, '../assets/divisions');
const DIVISION_OF_CHINA_FILE_PATH = `${DIVISION_DIR}/division_of_china.json`;

export const divisionConfig = {
Expand Down
3 changes: 2 additions & 1 deletion packages/leaa-api/src/interfaces/config.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export interface IDotEnv {
}

export interface IBuild {
// MODE: string;
MODE: string;
LANG: string;
VERSION: string;
BUILDTIME: string;
}
9 changes: 3 additions & 6 deletions packages/leaa-api/src/modules/v1/config/config.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ import { Module, Global } from '@nestjs/common';

import { ConfigService } from '@leaa/api/src/modules/v1/config/config.service';

const __DEV__ = process.env.NODE_ENV !== 'production';
const __PROD__ = process.env.NODE_ENV === 'production';
const envFilePath = path.resolve(__PROD__ ? '.env.production' : '.env');

const envFilePath = __DEV__
? path.resolve(__dirname, '../../../../.env')
: path.resolve(__dirname, '../../../../../../.env.production');

console.log('envFilePath:', envFilePath);
console.log('\n\n🌈 ENV-FILE-PATH:', envFilePath, '\n\n');

export const envConfig = new ConfigService(envFilePath);

Expand Down

0 comments on commit d3914d1

Please sign in to comment.