Deploy Analytics #51
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Deploy Backend | |
| on: | |
| # Production deployment is explicit and independent from Frontend, Hub, and Collectors. | |
| workflow_dispatch: | |
| env: | |
| IMAGE: ghcr.io/shenxianovo/heartbeat-backend | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - uses: actions/setup-dotnet@v4 | |
| with: | |
| dotnet-version: '10.0.x' | |
| - name: Test | |
| run: dotnet test server/Heartbeat.Server.Tests/Heartbeat.Server.Tests.csproj --configuration Release | |
| - uses: docker/setup-buildx-action@v3 | |
| - name: Log in to GHCR | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Build and push | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| file: server/Dockerfile | |
| push: true | |
| tags: | | |
| ${{ env.IMAGE }}:latest | |
| ${{ env.IMAGE }}:sha-${{ github.sha }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| deploy: | |
| needs: build | |
| runs-on: ubuntu-latest | |
| concurrency: deploy-backend-production | |
| steps: | |
| - uses: actions/checkout@v4 | |
| # compose.yml 的事实源是仓库;服务器副本每次部署前同步,不再手动维护 | |
| - name: Sync compose.yml to server | |
| uses: appleboy/scp-action@v1 | |
| with: | |
| host: ${{ secrets.SERVER_HOST }} | |
| username: ${{ secrets.SERVER_USER }} | |
| key: ${{ secrets.SERVER_SSH_KEY }} | |
| source: compose.yml | |
| target: /srv/heartbeat | |
| - name: Pull and restart backend container | |
| uses: appleboy/ssh-action@v1 | |
| with: | |
| host: ${{ secrets.SERVER_HOST }} | |
| username: ${{ secrets.SERVER_USER }} | |
| key: ${{ secrets.SERVER_SSH_KEY }} | |
| script: | | |
| set -eu | |
| cd /srv/heartbeat | |
| docker compose pull backend | |
| docker compose up -d backend | |
| for attempt in $(seq 1 30); do | |
| if curl --fail --silent --show-error --max-time 5 \ | |
| https://heartbeat.shenxianovo.com/health >/dev/null; then | |
| docker compose ps backend | |
| exit 0 | |
| fi | |
| if ! docker compose ps --status running backend | grep -q heartbeat-backend; then | |
| break | |
| fi | |
| sleep 2 | |
| done | |
| docker compose ps --all backend | |
| docker compose logs --no-color --tail=200 backend | |
| exit 1 |