• 前端项目


    开发环境

    1. nodejs 推荐使用 10.16.0(最新的lts版本)
    2. yarn npm install -g yarn
    3. lerna npm install -g lerna
    4. hzero-front-cli npm install -g hzero-front-cli --registry=http://nexus.saas.hand-china.com/content/groups/hzero-npm-group/

    ide

    1. vscode
    2. webstorm

    创建新项目

    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'
    

    升级平台依赖

    启动项目

    1. 按需修改 srm-front/config/compileStartEnv.js 中的环境变量
    2. 安装依赖 yarn bootstrap
    3. 编译子模块 lerna run transpile
    4. 打包dll yarn build:dll
    5. 启动本地环境 yarn start

    部署项目

    1. 编译项目:
      1. 安装依赖 yarn bootstrap
      2. 编译子模块 lerna run transpile
      3. 打包dll yarn build:dll
      4. 编译项目 yarn build
    2. 将build完成的dist所有文件 复制到 /usr/share/nginx/html(服务器部署目录)
    3. 替换环境变量
      1. 执行脚本

    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 "$@"
    

    环境变量说明

      如果`/app/`二级目录部署 需要修改 `srm-front/config/compileBuildEnv.js` 中的 
        BASE_PATH 为 `/app/(二级目录)`
        PUBLIC_URL 为 `/app(二级目录不要后面的/)`
    

    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

    # 获取最新代码
    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 ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"