88 lines
2.3 KiB
YAML
88 lines
2.3 KiB
YAML
name: Build and Deploy
|
|
|
|
on:
|
|
push:
|
|
branches: [main]
|
|
pull_request:
|
|
branches: [main]
|
|
|
|
jobs:
|
|
build:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: Checkout
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
|
|
- name: Build Docker image
|
|
run: |
|
|
docker build -t awesome-index:${{ github.sha }} .
|
|
docker tag awesome-index:${{ github.sha }} awesome-index:latest
|
|
|
|
- name: Save Docker image
|
|
run: |
|
|
docker save awesome-index:latest | gzip > awesome-index.tar.gz
|
|
|
|
- name: Upload artifact
|
|
uses: actions/upload-artifact@v4
|
|
with:
|
|
name: docker-image
|
|
path: awesome-index.tar.gz
|
|
retention-days: 1
|
|
|
|
deploy:
|
|
needs: build
|
|
if: github.ref == 'refs/heads/main'
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: Download artifact
|
|
uses: actions/download-artifact@v4
|
|
with:
|
|
name: docker-image
|
|
|
|
- name: Deploy to server
|
|
uses: appleboy/ssh-action@master
|
|
with:
|
|
host: 114.134.187.170
|
|
username: root
|
|
password: ${{ secrets.SERVER_PASSWORD }}
|
|
script: |
|
|
# 创建部署目录
|
|
mkdir -p /app/awesome/data /app/awesome/logs
|
|
|
|
- name: Copy files to server
|
|
uses: appleboy/scp-action@master
|
|
with:
|
|
host: 114.134.187.170
|
|
username: root
|
|
password: ${{ secrets.SERVER_PASSWORD }}
|
|
source: "docker-compose.yml,start.sh"
|
|
target: "/app/awesome/"
|
|
|
|
- name: Load and start container
|
|
uses: appleboy/ssh-action@master
|
|
with:
|
|
host: 114.134.187.170
|
|
username: root
|
|
password: ${{ secrets.SERVER_PASSWORD }}
|
|
script: |
|
|
cd /app/awesome
|
|
|
|
# 加载镜像
|
|
gunzip -c awesome-index.tar.gz | docker load || true
|
|
rm -f awesome-index.tar.gz
|
|
|
|
# 停止旧容器
|
|
docker compose down --remove-orphans 2>/dev/null || true
|
|
|
|
# 启动新容器
|
|
docker compose up -d
|
|
|
|
# 等待启动
|
|
sleep 10
|
|
|
|
# 健康检查
|
|
curl -sf http://localhost:3000/healthz && echo " - 服务已启动" || echo " - 启动失败"
|