微服务CI/CD实践系列:
微服务CI/CD实践(一)环境准备及虚拟机创建
微服务CI/CD实践(二)服务器先决准备
微服务CI/CD实践(三)Jenkins部署及环境配置
微服务CI/CD实践(四)Jenkins + Dokcer 部署微服务前端VUE项目
微服务CI/CD实践(五)Jenkins + Dokcer 部署微服务后端项目
文章目录
前端项目是基于NodeJS(Vue)框架开发,我们通过打包成Docker镜像的方式进行部署,原理是先将项目打包成静态页面,然后再将静态页面直接copy到Nginx镜像中运行。构建部署流程如下:
- 拉取代码
- jenkins服务器进行nodejs编译
- 使用dockerfile构建镜像并打包镜像
- 上传镜像包
- 执行sh
一、先决条件
1.1 服务器先决条件
Jenkins 和 server服务器先决条件参考微服务CI/CD实践(二)服务器先决准备 和 微服务CI/CD实践(四)Jenkins部署及环境配置
1.2 项目配置
Dockerfile
使用Jenkins本地编译项目在构建镜像
FROM nginx:latest
# 将生成的静态页面文件复制到nginx的/usr/share/nginx/html/目录
COPY dist/ /usr/share/nginx/html/
# 将mime文件复制到nginx的/etc/nginx/目录 后续配置ng会使用
COPY mime.types /etc/nginx/mime.types
# 容器启动时运行的命令
CMD ["nginx", "-g", "daemon off;"]
也可以直接使用docker 镜像编译-构建镜像,此模式jenkins服务器可以不需要node环境
# Install dependencies
FROM node:18.20.4 as builder
WORKDIR /app
# Install pnpm
RUN npm i -g pnpm
# copy file for next stage
COPY . /app
RUN pnpm install && pnpm run build
# copy dist from the first stage for Production
FROM nginx:latest AS runner
COPY --from=builder /app/dist/ /usr/share/nginx/html
COPY --from=builder /app/nginx.conf /etc/nginx/conf.d/default.conf
不过此模式在docker-hub停止国内服务后可能无法正常拉取镜像。
Nginx配置文件
根据项目要求编写ng配置
cd /data/container/nginx/etc
vi nginx.conf
# 编写配置并保存
vi mime.types
# 编写配置并保存
以下为nginx.conf配置示例
events {
worker_connections 1024;
}
http {
# 需要引入mime.types配置或者显示配置静态文件mimetype类型,否则运行后,浏览器会因为文件类型导致无法正常加载静态文件
include mime.types;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_max_body_size 100m;
server {
listen 80;
listen [::]:80;
server_name localhost;
# 设置 CORS 相关的响应头
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' '*' always;
add_header 'Access-Control-Max-Age' 1728000 always;
add_header 'Access-Control-Allow-Headers' '*' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
gzip on;
gzip_buffers 32 4k;
gzip_comp_level 6;
gzip_min_length 100;
gzip_types application/javascript text/css text/xml text/plain application/x-javascript image/jpeg image/gif image/png;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;
charset utf8;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
if (!-e $request_filename) {
rewrite ^/(.*) /index.html last;
break;
}
}
location ~ .*\.(jpg|png|js|css|woff2|ttf|woff|eot)$ {
root /usr/share/nginx/html;
}
#error_page 404 /404.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;
}
# 配置全局代理并统一处理CORS
location /gateway-api/ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.1.103:10000/;
# 添加 CORS 相关的响应头
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' '*' always;
add_header 'Access-Control-Max-Age' 1728000 always;
add_header 'Access-Control-Allow-Headers' '*' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
# 处理 OPTIONS 请求
if ($request_method = 'OPTIONS') {
return 204;
}
}
}
}
以下为mime.type示例
types {
text/html html htm shtml;
text/css css;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/xml xml;
application/json json;
application/pdf pdf;
application/rss+xml rss;
application/atom+xml atom;
text/mathml mml;
text/plain txt;
text/vnd.sun.j2me.app-descriptor jad;
text/vnd.wap.wml wml;
text/x-component htc;
image/png png;
image/svg+xml svg;
image/tiff tif tiff;
image/vnd.wap.wbmp wbmp;
image/x-icon ico;
image/x-jng jng;
image/x-ms-bmp bmp;
application/zip zip;
application/tar tar;
application/x-7z-compressed 7z;
application/x-java-archive jar;
application/x-rar-compressed rar;
application/x-web-app-manifest+json webapp;
application/xhtml+xml xhtml;
application/x-msdownload exe dll;
audio/midi mid midi kar;
audio/mpeg mp3;
video/mp4 mp4;
video/mpeg mpeg mpg;
video/webm webm;
video/x-msvideo avi;
video/x-ms-wmv wmv;
video/x-ms-asf asx asf;
video/x-flv flv;
application/x-shockwave-flash swf;
application/vnd.ms-excel xls