14e4022c创建于 2025年12月6日历史提交
name: Docker Check

permissions:
  contents: read
  issues: write
  pull-requests: write

on:
  issue_comment:
    types:
      - created

jobs:
  docker-check:
    name: Docker Check
    runs-on: ubuntu-latest
    if: github.event.issue.pull_request && contains(github.event.comment.body, '/cmd docker --check')
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.issue.pull_request.head.sha }}
          repository: ${{ github.event.repository.full_name }}
      - name: Post Initial Status
        if: github.event_name == 'issue_comment'
        uses: peter-evans/create-or-update-comment@v3
        with:
          issue-number: ${{ github.event.issue.number }}
          body: |
            ## 🐳 Docker Check Status
            > CI triggered by command `/cmd docker --check`.

            **Status:**  **Building**... Preparing environment and running Docker health checks.
      - name: Setup Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: Set up Docker Compose
        uses: docker/setup-compose-action@v1
      - name: Run docker compose
        working-directory: ./template/nestJs
        run: docker compose -f docker-compose-ci.yml up -d

      - name: Wait Service
        working-directory: ./template/nestJs
        run: |
          timeout 60 bash -c '
            until [[ "$(docker compose -f docker-compose-ci.yml ps --services --filter "status=running" | wc -l)" -ge 1 ]]; do
              sleep 2
            done
          '

      - name: Send HealthCheck Request
        run: curl -k --retry 20 --retry-delay 5 --retry-all-errors -f http://localhost:3000/healthCheck

      - name: Update Status on Failure
        if: failure() && github.event_name == 'issue_comment'
        uses: peter-evans/create-or-update-comment@v3
        with:
          issue-number: ${{ github.event.issue.number }}
          body: |
            ## ❌ Docker Check Status FAILED
            > CI triggered by command `/cmd docker --check`.

            **Status:** **FAILURE!** The service failed to start or did not pass the health check.

            [查看完整的 CI 运行日志](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
      - name: Check service logs
        if: failure()
        working-directory: ./template/nestJs
        run: docker compose -f docker-compose-ci.yml logs back
      - name: Update Status on Success
        if: success() && github.event_name == 'issue_comment'
        uses: peter-evans/create-or-update-comment@v3
        with:
          issue-number: ${{ github.event.issue.number }}
          body: |
            ## ✅ Docker Check Status
            > CI triggered by command `/cmd docker --check`.

            **Status:** **SUCCESS!** Service is built, running, and responded to health check on `http://localhost:3000/healthCheck`.
      - name: Shutdown Service
        working-directory: ./template/nestJs
        run: docker compose -f docker-compose-ci.yml down