前端项目
开发环境
- nodejs 推荐使用
10.16.0(最新的lts版本)
- yarn
npm install -g yarn
- lerna
npm install -g lerna
- hzero-front-cli
npm install -g hzero-front-cli --registry=http://nexus.saas.hand-china.com/content/groups/hzero-npm-group/
ide
创建新项目
1 创建新项目
# srm-front 为新项目名称,--puppeteer-skip 为省略非必需的 puppeteer 依赖的下载
hzero-front-cli srm-front --puppeteer-skip
2 创建主模块
# 主模块 spfm-front, 项目 srm-front
cd srm-front
hzero-front-cli . --create-packages 'spfm-front'
3 创建其他模块
# 其他模块 spfm-front-sslm
hzero-front-cli . --create-packages 'spfm-front-sslm'
升级平台依赖
-
升级 runtime
- 备份自己的修改的
srm-front
的内容(重新clone一个仓库来做升级) - 在升级runtime时忽略 config/webpack.config.js 文件
cd srm-front
;hzero-front . -i config/webpack.config.js
cd srm-front
;hzero-front-cli .
- 将自己做的修改 patch 到 刚更新的文件中
- 备份自己的修改的
-
升级
hzero-front@0.10.5
平台依赖cd srm-front
;yarn add -W hzero-front@0.10.1 --registry=http://nexus.saas.hand-china.com/content/groups/hzero-npm-group/
-
升级多个依赖
cd srm-front
;yarn add -W hzero-front@0.10.1 hzero-front-hiam@0.10.1 --registry=http://nexus.saas.hand-china.com/content/groups/hzero-npm-group/
- 根据package.json,更新依赖:更改需要升级的模块版本号,之后执行
yarn -W --registry=http://nexus.saas.hand-china.com/content/groups/hzero-npm-group/
-
安装一个新的模块
hzero-front-hnlp@0.10.0
cd srm-front
;yarn add -W hzero-front-hnlp@0.10.0 --registry=http://nexus.saas.hand-china.com/content/groups/hzero-npm-group/
- 将新安装的模块加入到主工程中:
- 在
srm-front/src/utils/getModuleRouters.js
引入import * as hzeroFrontHNlpRouters from 'hzero-front-hnlp/lib/utils/router';
- 在
srm-front/src/utils/getModuleRouters.js
的 默认导出中 加入hzeroFrontHNlpRouters
启动项目
- 按需修改
srm-front/config/compileStartEnv.js
中的环境变量 - 安装依赖 yarn bootstrap
- 编译子模块 lerna run transpile
- 打包dll yarn build:dll
- 启动本地环境 yarn start
部署项目
- 编译项目:
- 安装依赖 yarn bootstrap
- 编译子模块 lerna run transpile
- 打包dll yarn build:dll
- 编译项目 yarn build
- 将build完成的
dist
所有文件 复制到/usr/share/nginx/html(服务器部署目录)
- 替换环境变量
run.sh
/usr/share/nginx/html
: 服务器部署目录
#!/bin/bash
set -e
find /usr/share/nginx/html -name '*.js' | xargs sed -i "s BUILD_BASE_PATH $BUILD_BASE_PATH g"
find /usr/share/nginx/html -name '*.js' | xargs sed -i "s BUILD_API_HOST $BUILD_API_HOST g"
find /usr/share/nginx/html -name '*.js' | xargs sed -i "s BUILD_CLIENT_ID $BUILD_CLIENT_ID g"
find /usr/share/nginx/html -name '*.js' | xargs sed -i "s BUILD_BPM_HOST $BUILD_BPM_HOST g"
find /usr/share/nginx/html -name '*.js' | xargs sed -i "s BUILD_WFP_EDITOR $BUILD_WFP_EDITOR g"
find /usr/share/nginx/html -name '*.js' | xargs sed -i "s BUILD_WEBSOCKET_HOST $BUILD_WEBSOCKET_HOST g"
find /usr/share/nginx/html -name '*.js' | xargs sed -i "s BUILD_PLATFORM_VERSION $BUILD_PLATFORM_VERSION g"
exec "$@"
环境变量说明
- BUILD_BASE_PATH:
如果`/app/`二级目录部署 需要修改 `srm-front/config/compileBuildEnv.js` 中的
BASE_PATH 为 `/app/(二级目录)`
PUBLIC_URL 为 `/app(二级目录不要后面的/)`
- BUILD_API_HOST: 网关地址
- BUILD_CLIENT_ID: oauth 认证客户端id
- BUILD_BPM_HOST: 工作流地址
- BUILD_WEBSOCKET_HOST: websocket 地址
- BUILD_PLATFORM_VERSION: OP/SAAS
gitlab-ci & docker
docker/enterpoint.sh
#!/bin/bash
set -e
find /usr/share/nginx/html -name '*.js' | xargs sed -i "s BUILD_BASE_PATH $BUILD_BASE_PATH g"
find /usr/share/nginx/html -name '*.js' | xargs sed -i "s BUILD_API_HOST $BUILD_API_HOST g"
find /usr/share/nginx/html -name '*.js' | xargs sed -i "s BUILD_CLIENT_ID $BUILD_CLIENT_ID g"
find /usr/share/nginx/html -name '*.js' | xargs sed -i "s BUILD_BPM_HOST $BUILD_BPM_HOST g"
find /usr/share/nginx/html -name '*.js' | xargs sed -i "s BUILD_WFP_EDITOR $BUILD_WFP_EDITOR g"
find /usr/share/nginx/html -name '*.js' | xargs sed -i "s BUILD_WEBSOCKET_HOST $BUILD_WEBSOCKET_HOST g"
find /usr/share/nginx/html -name '*.js' | xargs sed -i "s BUILD_PLATFORM_VERSION $BUILD_PLATFORM_VERSION g"
exec "$@"
docker/default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
root /usr/share/nginx/html;
location / {
# react-router BrowserHistory 需要配合这个
try_files $uri /index.html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# Media: images, icons, video, audio, HTC
location ~* \.(jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
# CSS and Javascript
location ~* \.(css|js)$ {
expires 1y;
access_log off;
add_header Cache-Control "public";
}
}
Dockerfile
FROM registry.cn-hangzhou.aliyuncs.com/choerodon-tools/frontbase:0.5.0
RUN echo "Asia/shanghai" > /etc/timezone;
RUN sed -i 's/\#gzip/gzip/g' /etc/nginx/nginx.conf;
ADD ./dist /usr/share/nginx/html
ADD ./docker/default.conf /etc/nginx/conf.d/
COPY ./docker/enterpoint.sh /usr/share/nginx/html
RUN chmod 777 /usr/share/nginx/html/enterpoint.sh
ENTRYPOINT ["/usr/share/nginx/html/enterpoint.sh"]
CMD ["nginx", "-g", "daemon off;"]
EXPOSE 80
.gitlab-ci.yml
# 使用 choerodon 镜像
image: registry.cn-hangzhou.aliyuncs.com/choerodon-tools/cifront:0.7.0
stages:
# 编译代码
- node_build
# 编译Docker镜像
- docker_build
node_build:
stage: node_build
script:
# 安装依赖
- node_module
# 编译代码
- node_build
# 复制编译好的文件到 磁盘 volumn, 一遍二次构建 获取编译好的文件
- cp -r dist /cache/${CI_PROJECT_NAME}-${CI_PROJECT_ID}-${CI_COMMIT_REF_NAME}-${CI_COMMIT_SHA}/dist
only:
# 只在 master 分支merge 的时候触发
- master
# 打包部署镜像
docker_build:
image: registry.cn-hangzhou.aliyuncs.com/choerodon-tools/cibase:0.7.0
stage: docker_build
script:
# 打包镜像
- docker_build
# 删除缓存
- rm -rf /cache/${CI_PROJECT_NAME}-${CI_PROJECT_ID}-${CI_COMMIT_REF_NAME}-${CI_COMMIT_SHA}
# 没有找到 估计是 k8s chart 替换环境变量?
- chart_build
only:
- master
.auto_devops: &auto_devops |
http_status_code=`curl -o .auto_devops.sh -s -m 10 --connect-timeout 10 -w %{http_code} "${CHOERODON_URL}/devops/ci?token=${Token}&type=front"`
if [ "$http_status_code" != "200" ]; then
cat .auto_devops.sh
exit 1
fi
source .auto_devops.sh
function node_module(){
# 在 volumn 创建缓存目录
mkdir -p /cache/${CI_PROJECT_NAME}-${CI_PROJECT_ID}-${CI_COMMIT_REF_NAME}-${CI_COMMIT_SHA}
# 安装 lerna
yarn global add lerna
# 安装依赖
lerna bootstrap --registry http://nexus.saas.hand-china.com/content/groups/hzero-npm-group
# 安装依赖
yarn --registry http://nexus.saas.hand-china.com/content/groups/hzero-npm-group
}
function node_build(){
# 可能是有些 脚本需要执行权限
chmod -R 777 node_modules
lerna run transpile
yarn dll
yarn build
}
function docker_build(){
cp -r /cache/${CI_PROJECT_NAME}-${CI_PROJECT_ID}-${CI_COMMIT_REF_NAME}-${CI_COMMIT_SHA}/* ${1:-"."}
docker login -u ${DOCKER_USER} -p ${DOCKER_PWD} ${DOCKER_REGISTRY}
docker build --pull -t ${DOCKER_REGISTRY}/${GROUP_NAME}/${PROJECT_NAME}:${CI_COMMIT_TAG} ${1:-"."}
docker push ${DOCKER_REGISTRY}/${GROUP_NAME}/${PROJECT_NAME}:${CI_COMMIT_TAG}
echo "${DOCKER_REGISTRY}/${GROUP_NAME}/${PROJECT_NAME}:${CI_COMMIT_TAG}"
}
before_script:
- *auto_devops
jenkins & 开发服务器
jenkins 点击构建后 触发对应 服务器上的 run.sh
脚本
jenkins-run-sh
- 服务器需要安装运行环境(以及编译环境)
- nginx, node
- yarn, lerna
- 脚本目录在
**/srm-fornt/**.sh
- nginx root 指向
**/srm-fornt/html
# 获取最新代码
git pull
# 阻止下载 测试依赖
export PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
# 安装依赖
yarn install
# 编译子模块
lerna run transpile
# 打包第三方包等
yarn build:dll
# 编译
yarn build
# 替换变量
find dist -name '*.js' | xargs sed -i "s BUILD_API_HOST http://***网关地址 g"
find dist -name '*.js' | xargs sed -i "s BUILD_CLIENT_ID ***oauth-client-id g"
find dist -name '*.js' | xargs sed -i "s BUILD_BPM_HOST http://***工作流地址 g"
find dist -name '*.js' | xargs sed -i "s BUILD_WEBSOCKET_HOST ws://***websocket地址 g"
find dist -name '*.js' | xargs sed -i "s BUILD_WFP_EDITOR http://***工作流编辑器地址 g"
find dist -name '*.js' | xargs sed -i "s BUILD_PLATFORM_VERSION SAAS g"
# 删除 之前的
rm -rf html
mv dist html
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"