Files
railway_cloud/deploy.sh
2025-10-30 15:36:45 +08:00

101 lines
2.3 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# 服务部署脚本
# 用于停止旧服务并重新启动服务
# 使用方法: ./deploy.sh [0] (带0参数时不拉取代码)
echo "=== 服务部署脚本 ==="
echo "此脚本将停止当前服务并重新启动服务"
echo ""
# 检查是否跳过git pull
SKIP_GIT_PULL=0
if [ "$1" = "0" ]; then
SKIP_GIT_PULL=1
echo "跳过代码拉取,直接部署"
else
echo "默认执行代码拉取"
fi
# 如果不跳过git pull则拉取最新代码
if [ $SKIP_GIT_PULL -eq 0 ]; then
echo ""
echo "正在拉取最新代码..."
# 读取gitea用户名和密码
echo -n "请输入gitea用户名: "
read -r GIT_USERNAME
echo -n "请输入gitea密码: "
read -s GIT_PASSWORD
echo ""
# 设置git凭据
git config credential.helper store
echo "https://$GIT_USERNAME:$GIT_PASSWORD@gitea.com" > ~/.git-credentials
# 拉取代码
git pull origin main
if [ $? -eq 0 ]; then
echo "✓ 代码拉取成功"
# 清除git凭据
rm -f ~/.git-credentials
git config --unset credential.helper
else
echo "✗ 代码拉取失败"
# 清除git凭据
rm -f ~/.git-credentials
git config --unset credential.helper
echo "是否继续部署? (y/n)"
read -r CONTINUE_DEPLOY
if [ "$CONTINUE_DEPLOY" != "y" ] && [ "$CONTINUE_DEPLOY" != "Y" ]; then
echo "部署已取消"
exit 1
fi
fi
fi
# 读取sudo密码
echo ""
echo -n "请输入sudo密码: "
read -s SUDO_PASSWORD
echo ""
echo ""
# 检查密码是否为空
if [ -z "$SUDO_PASSWORD" ]; then
echo "错误: 密码不能为空"
exit 1
fi
echo "正在停止当前服务..."
# 使用expect或者直接传递密码给sudo
echo "$SUDO_PASSWORD" | sudo -S docker compose down --rmi all
if [ $? -eq 0 ]; then
echo "✓ 服务已成功停止"
else
echo "✗ 停止服务失败,请检查密码是否正确"
exit 1
fi
echo ""
echo "正在启动新服务..."
echo "$SUDO_PASSWORD" | sudo -S docker compose up -d
if [ $? -eq 0 ]; then
echo "✓ 服务启动成功"
echo ""
echo "服务状态:"
echo "$SUDO_PASSWORD" | sudo -S docker compose ps
else
echo "✗ 启动服务失败"
exit 1
fi
echo ""
echo "当前运行的Docker容器:"
echo "$SUDO_PASSWORD" | sudo -S docker ps
echo ""
echo "=== 部署完成 ==="