Create main.yml

This commit is contained in:
镀铬酸钾
2026-01-23 00:34:30 +08:00
committed by GitHub
parent 12d1eb3438
commit 093b29c056

101
.github/workflows/main.yml vendored Normal file
View File

@@ -0,0 +1,101 @@
name: 部署到生产环境
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
1:
runs-on: ubuntu-latest
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