103 lines
2.4 KiB
YAML
103 lines
2.4 KiB
YAML
name: 部署到生产环境
|
|
|
|
on:
|
|
push:
|
|
branches: [ main ]
|
|
pull_request:
|
|
branches: [ main ]
|
|
|
|
jobs:
|
|
deploy:
|
|
runs-on: ubuntu-latest
|
|
environment: SSH-KEY
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
|
|
- name: 安装依赖工具
|
|
run: sudo apt-get install -y sshpass expect
|
|
|
|
- name: 创建部署脚本
|
|
run: |
|
|
cat > deploy.sh << 'EOF'
|
|
#!/usr/bin/expect -f
|
|
set timeout 60
|
|
|
|
# 连接服务器
|
|
spawn ssh -o StrictHostKeyChecking=no -p 42422 $env(SERVER_USER)@$env(SERVER_ADDRESS)
|
|
|
|
# 处理普通密码提示
|
|
expect {
|
|
"Password:" {
|
|
send "$env(SERVER_PASSWORD)\r"
|
|
}
|
|
}
|
|
|
|
# 等待登录成功
|
|
expect "$ "
|
|
|
|
# 关闭服务
|
|
send "sudo systemctl stop neobot.service\r"
|
|
expect {
|
|
"Password:" {
|
|
send "$env(SERVER_PASSWORD)\r"
|
|
exp_continue
|
|
}
|
|
"$ " {}
|
|
}
|
|
|
|
# 拉取最新代码
|
|
send "cd /home/luoxiaolei/neobot/NeoBot && git pull origin main\r"
|
|
expect {
|
|
"error:" {
|
|
send_user "代码拉取失败!\n"
|
|
exit 1
|
|
}
|
|
"$ " {}
|
|
}
|
|
|
|
# 重启服务
|
|
send "sudo systemctl start neobot.service\r"
|
|
expect {
|
|
"Password:" {
|
|
send "$env(SERVER_PASSWORD)\r"
|
|
exp_continue
|
|
}
|
|
"$ " {}
|
|
}
|
|
|
|
# 检查服务状态
|
|
send "sudo systemctl status neobot.service\r"
|
|
expect {
|
|
"active (running)" {
|
|
send_user "服务重启成功!\n"
|
|
}
|
|
"failed" {
|
|
send_user "服务启动失败!\n"
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
# 退出
|
|
send "exit\r"
|
|
|
|
expect eof
|
|
EOF
|
|
chmod +x deploy.sh
|
|
|
|
- name: 执行部署
|
|
run: |
|
|
export SERVER_USER="${{ secrets.SERVER_USER }}"
|
|
export SERVER_ADDRESS="${{ secrets.SERVER_ADDRESS }}"
|
|
export SERVER_PASSWORD="${{ secrets.SERVER_PASSWORD }}"
|
|
./deploy.sh
|
|
continue-on-error: true
|
|
|
|
- name: 清理脚本
|
|
run: rm deploy.sh
|
|
|
|
- name: 检查部署状态
|
|
if: failure()
|
|
run: |
|
|
echo "部署失败!请检查服务器日志。"
|
|
exit 1
|