当前位置: 首页 > news >正文

前端项目使用docker编译发版和gitlab-cicd发版方式

项目目录

app/
├── container/
│   ├── init.sh
│   ├── nginx.conf.template
├── src/
├── .gitlab-ci.yml
└── deploy.sh
└── Dockerfile
└── Makefile

container目录是放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 --from=builder /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 "  --mode=<mode>   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" ]; thenBACKENDURL="http://xxxx"
elif [ "$mode" == "test" ]; thenBACKENDURL="http://xxxx"
elif [ "$mode" == "prod" ]; thenBACKENDURL="-"
elseBACKENDURL="http://xxxx"
finame="project"
port=8080
version="latest"
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 --restart=always --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的下一篇文章写叭

http://www.lryc.cn/news/351375.html

相关文章:

  • 18kw 机架式液冷负载的使用方法有哪些?
  • Linux liloconfig命令教程:创建和配置LILO引导加载器(附实例详解和注意事项)
  • 大厂程序员离职,开发一个盲盒小程序2万,一周开发完!
  • 自定义 Spring AOP 切面实战(鉴权、记录日志)
  • JAVA面试题大全(九)
  • React 组件三大核心之 ref
  • json 读写 python
  • Docker快速部署Seata的TC服务以及微服务引入Seata教程
  • 我的第一个JAVA程序IDEA版
  • 轻量SEO分析报告程序网站已开心去授权
  • 本机与华为云ping不通的问题
  • FastJSON2 > FastJSON 好在何处
  • 7个常见的SQL慢查询问题及其解决方法
  • 《Rust奇幻之旅:从Java和C++开启》第1章Hello world 1/5
  • 将富文本编辑器中的H标签处理成树形结构,支持无限层级
  • 探索移动云:我的ES与Kibana之旅
  • java 线程执行原理,java线程在jvm中执行流程
  • [Redis]基本全局命令
  • 【Linux】- HBase集群部署 [19]
  • js如何遍历FormData的值
  • 【C语言】明析部分C语言内存函数
  • 一阶数字高通滤波器
  • Linux多线程系列2: 模拟封装简易语言级线程库,线程互斥和锁,线程同步和条件变量,线程其他知识点
  • VUE3-form表单保存附件与基本信息
  • 无线网络安全技术基础
  • sheng的学习笔记-docker部署Greenplum
  • 【投稿资讯】区块链会议CCF A -- SP 2025 截止6.6、11.14 附录用率
  • C++哪些函数不能被声明为虚函数
  • vue中数据已经改变了,但是table里面内容没更新渲染!
  • 头歌实践教学平台:Junit实训入门篇