File tree Expand file tree Collapse file tree 2 files changed +66
-0
lines changed
Expand file tree Collapse file tree 2 files changed +66
-0
lines changed Original file line number Diff line number Diff line change 66 - dev
77 paths-ignore :
88 - ' readme.md'
9+ pull_request :
10+ branches :
11+ - dev
12+ paths-ignore :
13+ - ' readme.md'
914
1015jobs :
1116 build_and_deploy :
Original file line number Diff line number Diff line change 1+ name : Deploy DRF Application
2+
3+ on :
4+ push :
5+ branches :
6+ - main
7+ paths-ignore :
8+ - ' readme.md'
9+ pull_request :
10+ branches :
11+ - main
12+ paths-ignore :
13+ - ' readme.md'
14+
15+ jobs :
16+ build_and_deploy :
17+ runs-on : ubuntu-latest
18+ steps :
19+ # 检出代码
20+ - name : Checkout code
21+ uses : actions/checkout@v4
22+
23+ # 安装 docker-compose
24+ - name : Install Docker Compose
25+ run : |
26+ sudo apt-get update
27+ sudo apt-get install -y docker-compose
28+
29+ # 构建 Docker 镜像
30+ - name : Build Docker image
31+ run : docker-compose -f docker-compose-prod.yml build
32+
33+ # 检查镜像是否能成功启动一个容器
34+ - name : Run Docker container
35+ run : docker-compose -f docker-compose-prod.yml up -d
36+
37+ # 检查容器状态
38+ - name : Check container status
39+ id : container_status
40+ run : |
41+ CONTAINER_NAME=$(docker-compose -f docker-compose-prod.yml ps -q | head -n 1)
42+ if [ -z "$CONTAINER_NAME" ]; then
43+ echo "No containers found, failing the step."
44+ exit 1
45+ fi
46+ STATUS=$(docker inspect -f '{{.State.Running}}' "$CONTAINER_NAME")
47+ if [ "$STATUS" != "true" ]; then
48+ echo "Container is not running, failing the step."
49+ exit 1
50+ fi
51+ echo "Container is running successfully."
52+
53+ # 推送镜像到 kworlds's Docker Hub
54+ - name : Push Docker image to Kworlds's Docker Hub
55+ if : success()
56+ run : docker push dockerhub.kworlds.cn/pure-drf-api:latest
57+
58+ # 更新开发环境镜像并重启容器
59+ - name : Deploy to remote server
60+ if : success()
61+ run : sshpass -p ${{ secrets.HOST_USER_PASSWORD_PROD }} ssh -o StrictHostKeyChecking=no ${{ vars.HOST_USER_NAME_PROD }}@${{ vars.HOST_PROD}} "cd ${{ vars.COMPOSE_FILE_PATH }} && docker-compose pull && docker-compose down && docker-compose up -d"
You can’t perform that action at this time.
0 commit comments