66 lines
1.7 KiB
YAML
66 lines
1.7 KiB
YAML
name: Docker Build & Deploy
|
|
on:
|
|
workflow_dispatch:
|
|
push:
|
|
branches: [ main ]
|
|
|
|
env:
|
|
COMPOSE_PROJECT_NAME: emailbill
|
|
IMAGE_NAME: emailbill-app
|
|
|
|
jobs:
|
|
build:
|
|
name: Build Docker Image
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
# ✅ 使用 Gitea 兼容的代码检出方式
|
|
- name: Checkout code
|
|
uses: actions/checkout@v3
|
|
with:
|
|
gitea-server: http://192.168.31.14:14200
|
|
token: ${{ secrets.GITEA_TOKEN }}
|
|
ref: ${{ gitea.ref }} # 必须传递 Gitea 的 ref 参数
|
|
|
|
- name: Cleanup old containers
|
|
run: |
|
|
docker compose -p $COMPOSE_PROJECT_NAME down || true
|
|
docker rmi $IMAGE_NAME || true
|
|
|
|
- name: Build new image
|
|
run: |
|
|
RETRIES=3
|
|
DELAY=10
|
|
count=0
|
|
until docker build -t $IMAGE_NAME .; do
|
|
count=$((count+1))
|
|
if [ $count -ge $RETRIES ]; then
|
|
echo "Build failed after $RETRIES attempts"
|
|
exit 1
|
|
fi
|
|
echo "Build failed. Retrying in $DELAY seconds... ($count/$RETRIES)"
|
|
sleep $DELAY
|
|
done
|
|
|
|
deploy:
|
|
name: Deploy to Production
|
|
runs-on: ubuntu-latest
|
|
needs: build
|
|
environment: production
|
|
steps:
|
|
- name: Checkout code
|
|
uses: https://gitea.com/actions/checkout@v3
|
|
|
|
- name: Start containers
|
|
run: |
|
|
docker compose -p $COMPOSE_PROJECT_NAME down
|
|
docker compose -p $COMPOSE_PROJECT_NAME up -d --build
|
|
|
|
cleanup:
|
|
name: Cleanup Dangling Images
|
|
runs-on: ubuntu-latest
|
|
needs: deploy
|
|
steps:
|
|
- name: Remove dangling images
|
|
run: |
|
|
docker rm $(docker images -f "dangling=true" -q) || true
|
|
echo "Cleanup completed." |