refactor(env): migrate env config by .env file

This commit is contained in:
haitao(lj) 2022-11-10 16:37:03 +08:00
parent 8e10de39a0
commit 424f97f539
9 changed files with 9 additions and 119 deletions

View File

@ -1 +0,0 @@
PUBLIC_URL=

View File

@ -1 +1,2 @@
REACT_APP_API_URL=http://10.0.10.98:2060 PUBLIC_URL
REACT_APP_API_URL = http://127.0.0.1

View File

@ -1,3 +1,2 @@
REACT_APP_API_URL=/ PUBLIC_URL = /
REACT_APP_PUBLIC_PATH=/ REACT_APP_API_URL = /
REACT_APP_VERSION=

View File

5
ui/.gitignore vendored
View File

@ -13,10 +13,7 @@
# misc # misc
.DS_Store .DS_Store
.env.local .env*.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log* npm-debug.log*
yarn-debug.log* yarn-debug.log*

View File

@ -1,93 +0,0 @@
include:
- project: 'segmentfault/devops/templates'
file:
- .deploy-cdn.yml
- .deploy-helm.yml
variables:
FF_USE_FASTZIP: 'true'
PROJECT_NAME: 'answer_static'
stages:
- install
- publish
- deploy
# 静态资源构建
install:
image: dockerhub.qingcloud.com/sf_base/node-build:14
stage: install
allow_failure: false
cache:
- key:
files:
- pnpm-lock.yml
paths:
- node_modules/
policy: pull-push
script:
- pnpm install
- if [ "$CI_COMMIT_BRANCH" = "dev" ]; then
sed -i "s/<projectName>/$PROJECT_NAME/g" .env.development;
sed -i "s/<version>/$CI_COMMIT_SHORT_SHA/g" .env.development;
pnpm run build:dev;
elif [ "$CI_COMMIT_BRANCH" = "main" ]; then
sed -i "s/<projectName>/$PROJECT_NAME/g" .env.test;
sed -i "s/<version>/$CI_COMMIT_SHORT_SHA/g" .env.test;
pnpm run build:test;
elif [ "$CI_COMMIT_BRANCH" = "release" ]; then
sed -i "s/<projectName>/$PROJECT_NAME/g" .env.production;
sed -i "s/<version>/$CI_COMMIT_SHORT_SHA/g" .env.production;
pnpm run build:prod;
fi
artifacts:
paths:
- build/
publish:cdn:dev:
extends: .deploy-cdn
stage: publish
only:
- dev
variables:
AssetsPath: ./build
Project: $PROJECT_NAME
Version: $CI_COMMIT_SHORT_SHA
Destination: dev
publish:cdn:test:
extends: .deploy-cdn
stage: publish
only:
- main
variables:
AssetsPath: ./build
Project: $PROJECT_NAME
Version: $CI_COMMIT_SHORT_SHA
Destination: test
publish:cdn:prod:
extends: .deploy-cdn
stage: publish
only:
- release
variables:
AssetsPath: ./build
Project: $PROJECT_NAME
Version: $CI_COMMIT_SHORT_SHA
Destination: prod
deploy:dev:
extends: .deploy-helm
stage: deploy
only:
- dev
needs:
- publish:cdn:dev
variables:
KubernetesCluster: dev
KubernetesNamespace: 'sf-test'
DockerTag: $CI_COMMIT_SHORT_SHA
ChartName: answer-web
InstallPolicy: replace

View File

@ -4,7 +4,6 @@ const i18nLocaleTool = require('./scripts/i18n-locale-tool');
module.exports = { module.exports = {
webpack: function (config, env) { webpack: function (config, env) {
if (env === 'production') { if (env === 'production') {
config.output.publicPath = process.env.REACT_APP_PUBLIC_PATH;
i18nLocaleTool.resolvePresetLocales(); i18nLocaleTool.resolvePresetLocales();
} }
@ -33,12 +32,12 @@ module.exports = {
const config = configFunction(proxy, allowedHost); const config = configFunction(proxy, allowedHost);
config.proxy = { config.proxy = {
'/answer': { '/answer': {
target: 'http://10.0.10.98:2060', target: process.env.REACT_APP_API_URL,
changeOrigin: true, changeOrigin: true,
secure: false, secure: false,
}, },
'/installation': { '/installation': {
target: 'http://10.0.10.98:2060', target: process.env.REACT_APP_API_URL,
changeOrigin: true, changeOrigin: true,
secure: false, secure: false,
}, },

View File

@ -5,11 +5,7 @@
"homepage": "/", "homepage": "/",
"scripts": { "scripts": {
"start": "react-app-rewired start", "start": "react-app-rewired start",
"build:dev": "env-cmd -f .env.development react-app-rewired build", "build": "react-app-rewired build",
"build:test": "env-cmd -f .env.test react-app-rewired build",
"build:prod": "env-cmd -f .env.production react-app-rewired build",
"build": "env-cmd -f .env react-app-rewired build",
"test": "react-app-rewired test",
"lint": "eslint . --cache --fix --ext .ts,.tsx", "lint": "eslint . --cache --fix --ext .ts,.tsx",
"prepare": "cd .. && husky install", "prepare": "cd .. && husky install",
"cz": "cz", "cz": "cz",

View File

@ -10,16 +10,8 @@ import { getCurrentLang } from '@/utils/localize';
import Storage from './storage'; import Storage from './storage';
import { floppyNavigation } from './floppyNavigation'; import { floppyNavigation } from './floppyNavigation';
const API = {
development: '',
production: '',
test: '',
};
const baseApiUrl = process.env.REACT_APP_API_URL || API[process.env.NODE_ENV];
const baseConfig = { const baseConfig = {
baseUrl: baseApiUrl, baseUrl: process.env.REACT_APP_API_URL || '',
timeout: 10000, timeout: 10000,
withCredentials: true, withCredentials: true,
}; };