File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ name : Build and Deploy to Docker
2+
3+ on :
4+ # 1. 当推送到 main 分支时自动触发
5+ push :
6+ branches : ["main"]
7+ # 忽略文档修改,不触发部署
8+ paths-ignore :
9+ - " README.md"
10+ - " docs/**"
11+ - " .gitignore"
12+ - " LICENSE"
13+
14+ # 2. 【新增】允许在 GitHub 网页上手动点击按钮触发部署
15+ # 等您明天配好服务器,不用改代码,直接点一下 "Run workflow" 即可
16+ workflow_dispatch :
17+
18+ jobs :
19+ build-and-push :
20+ runs-on : ubuntu-latest
21+ steps :
22+ - name : Checkout code
23+ uses : actions/checkout@v3
24+
25+ - name : Log in to Docker Hub
26+ uses : docker/login-action@v2
27+ with :
28+ username : ${{ secrets.DOCKER_USERNAME }}
29+ # 这里填您的 Docker Hub 密码 或 Access Token
30+ password : ${{ secrets.DOCKER_PASSWORD }}
31+
32+ - name : Build and push Docker image
33+ uses : docker/build-push-action@v4
34+ with :
35+ context : .
36+ push : true
37+ # 注意:这里的 jerry113 是您的用户名,不要改
38+ tags : jerry113/echowave:latest
39+
40+ deploy-to-vps :
41+ needs : build-and-push
42+ runs-on : ubuntu-latest
43+ steps :
44+ - name : Deploy via SSH
45+ uses : appleboy/ssh-action@master
46+ with :
47+ host : ${{ secrets.HOST }}
48+ username : ${{ secrets.USERNAME }}
49+ key : ${{ secrets.SSH_KEY }}
50+ port : 22
51+ script : |
52+ # 登录 Docker
53+ echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
54+
55+ # 拉取最新镜像
56+ docker pull jerry113/echowave:latest
57+
58+ # 停止旧容器 (如果存在)
59+ docker stop echowave || true
60+ docker rm echowave || true
61+
62+ # 确保数据文件存在
63+ touch /root/bot_data.json
64+
65+ # 启动新容器
66+ # 注意:明天租服务器时,记得把 .env 文件上传到 /root/ 目录下
67+ docker run -d \
68+ --name echowave \
69+ --restart always \
70+ -v /root/bot_data.json:/app/bot_data.json \
71+ --env-file /root/.env \
72+ jerry113/echowave:latest
73+
74+ # 清理垃圾
75+ docker image prune -f
You can’t perform that action at this time.
0 commit comments