提交 05af1f2b authored 作者: yoyoyo's avatar yoyoyo

1

上级 2f4c8445
...@@ -318,7 +318,7 @@ start_project() { ...@@ -318,7 +318,7 @@ start_project() {
read -r restart read -r restart
if [ "$restart" = "y" ]; then if [ "$restart" = "y" ]; then
if docker-compose ps | grep -q "Up"; then if docker-compose ps | grep -q "Up"; then
docker-compose restart mall-admin mall-api mall-data nginx mall-tools go-nginx myiframe docker-compose restart mall-admin mall-api mall-data nginx mall-tools go-nginx myiframe mall_images_proxy
echo "重启中 预计 60秒左右启动成功 ..." echo "重启中 预计 60秒左右启动成功 ..."
sleep 60 sleep 60
else else
......
#!/bin/bash #!/bin/bash
install_dir="/wwwroot" install_dir="/jys"
...@@ -15,9 +15,9 @@ production_install_dir="$production_run_dir/jiaoyisuo" ...@@ -15,9 +15,9 @@ production_install_dir="$production_run_dir/jiaoyisuo"
projects=( projects=(
# 项目名 项目仓库 项目保存目录 # 项目名 项目仓库 项目保存目录
"server git.wkwork.xyz/jys/jiaoyisuo-all-php.git $install_dir/jiaoyisuo-all-php" "server git.wkwork.xyz/jys/jiaoyisuo-all-php.git $install_dir/server"
"client_admin git.wkwork.xyz/tiktok/production-client.git $install_dir/production-client" "client_admin git.wkwork.xyz/jys/tron-vue-main.git $install_dir/client_admin"
"client_user git.wkwork.xyz/tiktok/production-client.git $install_dir/production-client" "client_user git.wkwork.xyz/wk/jiaoyisuo-vue.git $install_dir/client_user"
) )
...@@ -32,6 +32,7 @@ cd $install_dir ...@@ -32,6 +32,7 @@ cd $install_dir
BRANCH_FILE_SUFFIX=".branch" BRANCH_FILE_SUFFIX=".branch"
CREDENTIALS_FILE="$HOME/.git_credentials" CREDENTIALS_FILE="$HOME/.git_credentials"
# 输出函数 # 输出函数
echo_content() { echo_content() {
ECHO_TYPE="echo -e" ECHO_TYPE="echo -e"
...@@ -60,7 +61,6 @@ echo_content() { ...@@ -60,7 +61,6 @@ echo_content() {
esac esac
} }
# 函数:检查并安装 Git # 函数:检查并安装 Git
check_git() { check_git() {
echo_content "green" "检查并安装 Git..." echo_content "green" "检查并安装 Git..."
...@@ -78,6 +78,17 @@ check_git() { ...@@ -78,6 +78,17 @@ check_git() {
fi fi
} }
check_unzip(){
if ! command -v unzip &> /dev/null; then
if command -v apt &> /dev/null; then
sudo apt update
sudo apt install -y unzip
elif command -v yum &> /dev/null; then
sudo yum install -y unzip
fi
fi
}
# 函数:检查并安装 Docker # 函数:检查并安装 Docker
install_docker() { install_docker() {
...@@ -110,9 +121,10 @@ install_docker() { ...@@ -110,9 +121,10 @@ install_docker() {
fi fi
fi fi
}
}
# 函数:解析项目信息 # 函数:解析项目信息
get_project_info() { get_project_info() {
local project_info="$1" local project_info="$1"
...@@ -131,7 +143,6 @@ get_project_names(){ ...@@ -131,7 +143,6 @@ get_project_names(){
echo "${names[@]}" echo "${names[@]}"
} }
set_branches(){ set_branches(){
repo_dir=$1 repo_dir=$1
repo_name=$2 repo_name=$2
...@@ -149,51 +160,71 @@ set_branches(){ ...@@ -149,51 +160,71 @@ set_branches(){
#打印当前分支名称 #打印当前分支名称
echo_content "yellow" "当前分支为: '$current_branch'"
branch_count=$(echo "$branches" | wc -l) branch_count=$(echo "$branches" | wc -l)
if [ "$branch_count" -gt 1 ]; then if [ "$branch_count" -gt 1 ]; then
echo_content "yellow" "$repo_name 项目:分支选择 (如不需要更换直接回车):" echo_content "yellow" "$repo_name 项目:分支选择 (如继续使用 '$current_branch' 直接回车):"
PS3="请输入分支对应的数字:"
# 显示分支列表并允许选择 # 显示分支列表
select branch in $branches; do i=1
if [ -z "$branch" ]; then for branch in $branches; do
# 如果按回车不选择,继续使用当前分支 echo "$i) $branch"
echo_content "yellow" "继续使用当前分支: $current_branch" i=$((i + 1))
branch="$current_branch" done
break
# 提示用户选择
read -p "请输入分支对应的数字 (回车保持当前分支: $current_branch): " input
# 如果用户直接按回车,保持当前分支
if [ -z "$input" ]; then
echo_content "yellow" "继续使用当前分支: $current_branch"
branch="$current_branch"
else
# 验证输入是否为有效的数字
if [[ "$input" =~ ^[0-9]+$ ]] && (( input >= 1 && input <= branch_count )); then
selected_branch=$(echo "$branches" | sed -n "${input}p" | xargs)
echo_content "green" "你选择了分支: $selected_branch"
# 切换到选定分支
if git show-ref --verify --quiet "refs/heads/$selected_branch"; then
git checkout "$selected_branch" # 切换到本地分支
else else
echo_content "green" "你选择了分支: $branch" git checkout -b "$selected_branch" "origin/$selected_branch" # 从远程分支创建并切换
# 切换到选定分支
if git show-ref --verify --quiet "refs/heads/$branch"; then
git checkout "$branch" # 切换到本地分支
else
git checkout -b "$branch" "origin/$branch" # 从远程分支创建并切换
fi
break
fi fi
done else
echo_content "red" "无效的输入,请重新运行脚本。"
exit 1
fi
fi
fi fi
}
}
updata_project(){ updata_project(){
repo_dir=$1 repo_dir=$1
repo_name=$2
set_branches $repo_dir $repo_name
cd "$repo_dir" || exit 1 cd "$repo_dir" || exit 1
# 更新代码,如果有冲突时使用本地版本
git fetch origin
git merge -X ours origin/master # 使用 'ours' 策略以本地为主
if [ $? -ne 0 ]; then
echo_content "red" "更新代码失败,请手动检查冲突。"
exit 1
fi
echo_content "green" "代码更新完成。" # 检查工作区是否有未提交的更改
if [[ -n $(git status --porcelain) ]]; then
git add .
git commit -m "Auto-commit: 保存当前变动"
fi
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
# git pull origin "$CURRENT_BRANCH"
git pull --rebase -X theirs origin "$CURRENT_BRANCH"
if [ $? -ne 0 ]; then
echo_content "red" "更新代码失败,请手动检查冲突。"
else
echo_content "green" "代码更新完成。"
fi
} }
# 函数:拉取项目代码 # 函数:拉取项目代码
pull_project() { pull_project() {
...@@ -239,7 +270,6 @@ pull_project() { ...@@ -239,7 +270,6 @@ pull_project() {
} }
# 函数:卸载项目 # 函数:卸载项目
uninstall_project() { uninstall_project() {
if [ -d "$install_dir" ]; then if [ -d "$install_dir" ]; then
...@@ -249,8 +279,9 @@ uninstall_project() { ...@@ -249,8 +279,9 @@ uninstall_project() {
echo_content "green" "正在卸载项目..." echo_content "green" "正在卸载项目..."
cd $production_install_dir cd $production_install_dir
docker-compose down docker-compose down
docker network rm production-run_video_network
docker network rm production-run_mall_network
docker network rm production_mall_network
rm -rf "$install_dir" # 删除项目目录 rm -rf "$install_dir" # 删除项目目录
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo_content "green" "项目已成功卸载。" echo_content "green" "项目已成功卸载。"
...@@ -265,18 +296,28 @@ uninstall_project() { ...@@ -265,18 +296,28 @@ uninstall_project() {
fi fi
} }
# 函数:启动或重启项目 # 函数:启动或重启项目
start_project() { start_project() {
cd "$production_install_dir" || exit 1 cd "$production_install_dir" || exit 1
echo_content "green" "是否要启动/重启项目?(y/n):" echo_content "green" "是否要启动/重启 项目?(y/n):"
read -r restart read -r restart
if [ "$restart" = "y" ]; then if [ "$restart" = "y" ]; then
if docker-compose ps | grep -q "Up"; then if docker-compose ps | grep -q "Up"; then
docker-compose restart docker-compose restart mall-admin mall-api mall-data nginx mall-tools go-nginx myiframe
echo "重启中 预计 60秒左右启动成功 ..."
sleep 60
else else
docker-compose up -d docker-compose up -d
echo "第一次启动 预计 150 秒左右启动成功 ..."
sleep 100
docker-compose restart mall-admin mall-api mall-data
echo "重启中 预计 60秒左右启动成功 ..."
sleep 60
fi fi
fi fi
...@@ -285,6 +326,7 @@ start_project() { ...@@ -285,6 +326,7 @@ start_project() {
# 函数:选择要更新的项目 # 函数:选择要更新的项目
select_project() { select_project() {
local project_names=($(get_project_names)) local project_names=($(get_project_names))
...@@ -293,6 +335,7 @@ select_project() { ...@@ -293,6 +335,7 @@ select_project() {
if [ ${#project_names[@]} -eq 1 ]; then if [ ${#project_names[@]} -eq 1 ]; then
selected_project="${project_names[0]}" selected_project="${project_names[0]}"
project_dir=$(get_project_info "${projects[0]}" 2) project_dir=$(get_project_info "${projects[0]}" 2)
project_name=$(get_project_info "${projects[0]}" 0)
else else
echo_content "yellow" "\n\n" echo_content "yellow" "\n\n"
echo_content "yellow" "请选择要更新的的项目\n" echo_content "yellow" "请选择要更新的的项目\n"
...@@ -305,6 +348,7 @@ select_project() { ...@@ -305,6 +348,7 @@ select_project() {
for project in "${projects[@]}"; do for project in "${projects[@]}"; do
if [ "$(get_project_info "$project" 0)" = "$selected_project" ]; then if [ "$(get_project_info "$project" 0)" = "$selected_project" ]; then
project_dir=$(get_project_info "$project" 2) project_dir=$(get_project_info "$project" 2)
project_name=$(get_project_info "$project" 0)
break 2 break 2
fi fi
done done
...@@ -315,7 +359,7 @@ select_project() { ...@@ -315,7 +359,7 @@ select_project() {
fi fi
done done
fi fi
updata_project "$project_dir" updata_project "$project_dir" "$project_name"
} }
...@@ -328,43 +372,34 @@ select_operation() { ...@@ -328,43 +372,34 @@ select_operation() {
case $REPLY in case $REPLY in
1) 1)
echo_content "green" "你选择了操作: 更新项目" echo_content "green" "你选择了操作: 更新项目"
select_project
start_project
break break
;; ;;
2) 2)
echo_content "green" "你选择了操作: 启动/重启" echo_content "green" "你选择了操作: 商城(启动/重启)"
break start_project
;;
3)
echo_content "green" "你选择了操作: 卸载项目"
break break
;; ;;
3)
echo_content "green" "你选择了操作: 卸载"
uninstall_project
exit
;;
4) 4)
echo_content "green" "你选择了操作: 退出" echo_content "green" "你选择了操作: 退出"
exit exit
;; ;;
*) *)
echo_content "red" "无效的选择,请输入 1 2 。" echo_content "red" "无效的选择"
continue continue
;; ;;
esac esac
done done
if [ "$operation" == "更新项目" ]; then
select_project
start_project
elif [ "$operation" == "启动/重启" ] ; then
start_project
elif [ "$operation" == "卸载" ] ; then
uninstall_project # 卸载
else
echo_content "red" "无效的操作。"
fi
} }
# 函数: 初始化脚本 检查环境 # 函数: 初始化脚本 检查环境
init_environment_check(){ init_environment_check(){
...@@ -378,12 +413,11 @@ init_environment_check(){ ...@@ -378,12 +413,11 @@ init_environment_check(){
echo_content "green" "更新项目: production_run" echo_content "green" "更新项目: production_run"
echo_content "green" "项目仓库: $production_run_repo" echo_content "green" "项目仓库: $production_run_repo"
echo_content "green" "项目目录: $production_run_dir" echo_content "green" "项目目录: $production_run_dir"
updata_project "$production_run_dir" updata_project "$production_run_dir" "production_run"
fi fi
get_project_names get_project_names
...@@ -404,10 +438,7 @@ init_environment_check(){ ...@@ -404,10 +438,7 @@ init_environment_check(){
# 调用 pull_project 函数进行项目拉取 # 调用 pull_project 函数进行项目拉取
pull_project "$project_repo" "$project_dir" "$project_name" pull_project "$project_repo" "$project_dir" "$project_name"
fi fi
# rm -rf "$production_run_dir/tomallmcat/"
# ln -s "$project_dir/$project_name" "$production_run_dir/$project_name"
done done
} }
...@@ -420,9 +451,14 @@ init_environment_check(){ ...@@ -420,9 +451,14 @@ init_environment_check(){
main(){ main(){
if [ "$(id -u)" -ne 0 ]; then
echo_content "red" "请以 root 权限运行此脚本。"
exit 1
fi
echo_content "green" "安装目录:$install_dir" echo_content "green" "安装目录:$install_dir"
echo_content "green" "初始化脚本 检查环境..." echo_content "green" "初始化脚本 检查环境..."
check_git check_git
check_unzip
install_docker install_docker
init_environment_check init_environment_check
select_operation select_operation
...@@ -432,3 +468,5 @@ main ...@@ -432,3 +468,5 @@ main
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论