保险网站建设平台,温州微网站开发,wordpress增加分类目录,嘉兴seo优化项目目录
app/
├── container/
│ ├── init.sh
│ ├── nginx.conf.template
├── src/
├── .gitlab-ci.yml
└── deploy.sh
└── Dockerfile
└── Makefilecontainer目录是放nginx的配置文件#xff0c;给nginx镜像使用 .gitlab-ci.yml和Makefile是c…项目目录
app/
├── container/
│ ├── init.sh
│ ├── nginx.conf.template
├── src/
├── .gitlab-ci.yml
└── deploy.sh
└── Dockerfile
└── Makefilecontainer目录是放nginx的配置文件给nginx镜像使用 .gitlab-ci.yml和Makefile是cicd自动发版适用于测试环境和生产环境 deploy.sh是使用shell手动发版适用于开发环境
下面是以上配置文件
init.sh 主要作用是根据env环境变量替换nginx的反向代理地址
#!/bin/bash
BACKENDURL$BACKENDURL
export BACKENDURL$BACKENDURL
envsubst $BACKENDURL /etc/nginx/nginx.conf.template /etc/nginx/nginx.conf nginx -g daemon off;nginx.conf.template
user root;
events {worker_connections 4096; ## Default: 1024
}http {proxy_connect_timeout 300000; #连接握手时间proxy_send_timeout 300000; # 设置发送超时时间proxy_read_timeout 300000; # 设置读取超时时间。client_max_body_size 100M;include /etc/nginx/mime.types;sendfile on;server {listen 80;listen [::]:80;add_header Access-Control-Allow-Origin *;add_header Access-Control-Allow-Headers X-Requested-With,access-token,Access-Token,Refresh-Token,Accept,Content-Type,User-Agent;add_header Access-Control-Allow-Methods GET,POST,OPTIONS;location / {root /usr/share/nginx/html;index index.html;try_files $uri $uri/ /index.html;}location ^~/api/ {proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-NginX-Proxy true;proxy_pass $BACKENDURL; #后端实际服务器地址add_header Access-Control-Allow-Origin *;add_header Access-Control-Allow-Headers X-Requested-With,access-token,Access-Token,Refresh-Token,Accept,Content-Type,User-Agent;add_header Access-Control-Allow-Methods GET,POST,OPTIONS;}}
}Dockerfile 镜像内编译打包因为服务器上可能有多个node项目版本会有兼容问题直接容器内编译
# 编译打包
FROM node:18-alpine as builder
WORKDIR /app
COPY package.json .
ENV NODE_OPTIONS--openssl-legacy-provider
RUN npm install
COPY . .
RUN npm run build# 运行应用
FROM nginx:latest
COPY --frombuilder /app/dist /usr/share/nginx/html
COPY container/nginx.conf.template /etc/nginx/nginx.conf.template
COPY container/init.sh /init.sh
RUN chmod 777 init.sh
EXPOSE 80CMD [/bin/bash, /init.sh]deploy.sh 人工发版时运行脚本 部署脚本里面打包命令、项目名字、端口、后端地址记得更换
#!/bin/bash
set -xfunction show_help {echo Usage: $0 [OPTIONS]echo Options:echo --modemode Set the mode (local, dev, test, prod)
}
# 保存输入的参数
args($)
# 使用 shift 命令去除已处理的位置参数
shift
# 处理命令行参数
for ((i 0; i ${#args[]}; i)); docase ${args[$i]} in--mode* | -mode*)mode${args[$i]#*};;--mode | -mode)mode${args[$((i 1))]};;--help)show_helpexit 0;;esac
done
# 如果 mode 不在合法的模式值中则输出错误信息
if ! $valid; thenecho mode值只能是:空值 local, dev, test, prod.exit 1
fi
# 根据 mode 设置不同的 BACKENDURL 地址
if [ $mode dev ]; thenBACKENDURLhttp://xxxx
elif [ $mode test ]; thenBACKENDURLhttp://xxxx
elif [ $mode prod ]; thenBACKENDURL-
elseBACKENDURLhttp://xxxx
finameproject
port8080
versionlatest
current_user$(whoami)
echo 当前用户${current_user}
# 拉代码和打包镜像
git pull
# 设置 node_modules 为当前用户
sudo chown -R $current_user:$current_user ./
sudo docker build -f Dockerfile -t $name:$version .
# 停止并删除容器
sudo docker stop $name
sudo docker rm $name -f
# 启动容器
sudo docker run --restartalways --name $name -p $port:80 -e BACKENDURL$BACKENDURL -d $name:$version人工发版
运行命令
# 记得先给部署脚本执行权限
chmod x ./deploy.sh
./deploy.sh --mode dev运行截图 项目成功运行 然后浏览器访问http://xxx.xxx.com:21000
gitlab-cicd的下一篇文章写叭