# 推送 main 时构建并发布(镜像推送 + 后端测试) name: Build and Deploy on: push: branches: [main] env: REGISTRY: ghcr.io jobs: test-backend: runs-on: ubuntu-latest services: postgres: image: postgres:16-alpine env: POSTGRES_USER: wecom POSTGRES_PASSWORD: wecom_secret POSTGRES_DB: wecom_ai ports: - 5432:5432 options: >- --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 env: DATABASE_URL: postgresql+asyncpg://wecom:wecom_secret@localhost:5432/wecom_ai DATABASE_URL_SYNC: postgresql://wecom:wecom_secret@localhost:5432/wecom_ai JWT_SECRET: test-secret WECOM_TOKEN: test WECOM_ENCODING_AES_KEY: abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: python-version: "3.12" - name: Install dependencies run: | cd backend pip install -r requirements.txt - name: Run migrations run: | cd backend && PYTHONPATH=. alembic upgrade head - name: Pytest run: | cd backend && PYTHONPATH=. pytest tests/ -v --tb=short build-backend: runs-on: ubuntu-latest needs: test-backend steps: - uses: actions/checkout@v4 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Log in to Container Registry uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Build and push backend uses: docker/build-push-action@v6 with: context: ./backend file: ./backend/Dockerfile push: true tags: | ${{ env.REGISTRY }}/${{ github.repository_owner }}/wecom-ai-backend:latest ${{ env.REGISTRY }}/${{ github.repository_owner }}/wecom-ai-backend:${{ github.sha }} build-admin: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Log in to Container Registry uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Build and push admin uses: docker/build-push-action@v6 with: context: ./admin file: ./admin/Dockerfile push: true tags: | ${{ env.REGISTRY }}/${{ github.repository_owner }}/wecom-ai-admin:latest ${{ env.REGISTRY }}/${{ github.repository_owner }}/wecom-ai-admin:${{ github.sha }}