合并构建

概述

因前端占用资源较少,但一个服务需占用一个POD,为节约K8S之POD资源,有需求将前端工程合并构建到一个镜像中,故此命令应运而生。

1、确定需要合并的工程
2、通过pai命名创建一个前端工程,删除多余的文件及目录(含.gitignore),仅保留Deployment.yaml,Dockerfile,README.md,Service.yaml,default.conf并推送到git仓库。
3、修改Dockerfile

FROM nginx:alpine
MAINTAINER 作者名<mail@flyrise.cn>
RUN mkdir -p /var/log/nginx
RUN mkdir -p /usr/share/nginx/html/
COPY dist/ /usr/share/nginx/html/
COPY default.conf /etc/nginx/conf.d/default.conf
WORKDIR /usr/share/nginx/html/

4、修改default.conf (有多少个需合并的工程就需要添加几个 location)

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;
    error_log  /var/log/nginx/error.log  error;

    location /approve {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/=403 /approve/index.html;
    }

    location /auth {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/=403 /auth/index.html;
    }
    # 有多少个需合并的工程就需要添加几个 location

    # 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;
    }
}

5、切换到此工程的根目录,通过pai build gen命令产生合并构建的描述文件pai-build.yaml

  • 第一次执行需指定namespacedist
  • 添加需合并的工程,执行多次此命令即可。或直接修改pai-build.yaml
pai:
  dist: pai-lingyun-ui #合并后的工程名
  namespace: dev-cloud #K8S命名空间(若需开发部署则按此设置)
  builds:
  - name: pai-approve-ui # 需合并的工程名
    branch: v2.0.0.0 # 分支或仓库tag(若不指定将取仓库默认分支)
  - name: pai-auth-ui
    branch: v2.0.0.0  

6、执行pai build image命令构建合并后的工程镜像。此命令执行过程如下:

  • 根据builds列表下载需要合并工程的代码
  • 执行npm build
  • 将各工程构建好的dist文件夹复制到合并后的工程/dist/合并前的工程名(去掉pai-及-ui)
  • 根据Dockerfile构建镜像并推送到镜像仓库
  • 将合并后的工程代码推送到git仓库,以便下次单独更新某个(一个-n)或某几个(多个-n)合并前的工程时不需要每个工程都重新编译。
文档更新时间: 2023-04-07 13:31   作者:管理员