Pre Merge pull request !606 from AprilWind/dev-docker

This commit is contained in:
AprilWind 2024-12-11 06:01:34 +00:00 committed by Gitee
commit 48ed7a98bb
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

103
ruoyi-admin/deploy.sh Normal file
View File

@ -0,0 +1,103 @@
#!/bin/bash
# 接收参数
for arg in "$@"; do
eval "$arg"
done
# 默认变量检查,如果未传入对应参数则使用默认值
#指定目标主机的目录路径,它代表着部署应用的目录位置
host=${host:-"server"}
# 项目名称
name=${name:-"ruoyi-admin"}
# 环境配置
profile=${profile:-"prod"}
# 镜像标签,默认 "ruoyi/ruoyi-server:5.2.3"
dockerfile_imageTag=${dockerfile_imageTag:-"ruoyi/ruoyi-server:5.2.3"}
# Dockerfile 路径,默认 "ruoyi-admin"
dockerfile_sourceFilePath=${dockerfile_sourceFilePath:-"ruoyi-admin"}
# 制品包路径,默认 "/home/admin/app"
package_path=${package_path:-"/home/admin/app"}
# 1. 删除目标目录中的旧文件
echo "删除旧的文件..."
rm -rf /www/wwwroot/${host}/*
# 2. 解压制品包到对应目录
echo "解压制品包到 ${host}..."
tar zxvf ${package_path}/${name}_package.tgz -C /www/wwwroot/${host}/
# 3. 停止容器(优雅停止)
echo "尝试优雅停止容器..."
if docker ps | grep -q "${name}_${profile}"; then
docker stop --time=30 ${name}_${profile}
else
echo "容器 ${name}_${profile} 不存在或已停止,无需处理。"
fi
# 4. 删除容器
echo "删除容器..."
if docker ps -a | grep -q "${name}_${profile}"; then
docker rm ${name}_${profile}
else
echo "容器 ${name}_${profile} 不存在,无需删除。"
fi
# 5. 删除镜像
echo "删除镜像 ${dockerfile_imageTag}..."
if docker images | grep -q "${dockerfile_imageTag}"; then
docker rmi ${dockerfile_imageTag}
else
echo "镜像 ${dockerfile_imageTag} 不存在,无需删除。"
fi
# 6. 构建镜像
echo "构建镜像 ${dockerfile_imageTag}..."
docker build -t ${dockerfile_imageTag} -f /www/wwwroot/${host}/${dockerfile_sourceFilePath}/Dockerfile /www/wwwroot/${host}/${dockerfile_sourceFilePath}
# 7. 运行容器
echo "运行容器 ${name}_${profile}..."
docker run --name ${name}_${profile} --network=host -e TZ=Asia/Shanghai -d ${dockerfile_imageTag}
# 8. 清理制品包
echo "清理制品包..."
rm -rf ${package_path}/${name}_package.tgz
echo "脚本执行完成!"
#------------------------- 流水线部署脚本命令(请将此段代码复制到实际部署脚本中)-------------------------
## 定义必要的变量
#host=${host} # 指定目标主机的目录路径,它代表着部署应用的目录位置
#name=${name} # 应用名
#profile=${profile} # 环境配置prod/test
#dockerfile_imageTag=${dockerfile_imageTag} # 镜像标签
#dockerfile_sourceFilePath=${dockerfile_sourceFilePath} # Dockerfile 所在路径
#package_path=${package_path} # 制品包路径
#
## 创建部署脚本的路径
#deploy_script=${deploy_script}
#
## 检查部署脚本是否存在
#if [ ! -f "$deploy_script" ]; then
# echo "部署脚本 $deploy_script 不存在,无法执行!"
# exit 1
#fi
#
## 为部署脚本设置执行权限
#chmod +x "$deploy_script"
#
## 调用部署脚本并传递参数
#bash "$deploy_script" \
# host="$host" \
# name="$name" \
# profile="$profile" \
# dockerfile_imageTag="$dockerfile_imageTag" \
# dockerfile_sourceFilePath="$dockerfile_sourceFilePath" \
# package_path="$package_path"
#
## 检查脚本执行结果
#if [ $? -eq 0 ]; then
# echo "部署成功!"
#else
# echo "部署失败,请检查日志!"
#fi