name: Build and Deploy Log Center on: push: branches: - main - master env: LOG_CENTER_URL: https://qiyuan-log-center-api.airlabs.art jobs: build-and-deploy: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v3 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 with: config-inline: | [registry."docker.io"] mirrors = ["https://docker.m.daocloud.io", "https://docker.1panel.live", "https://hub.rat.dev"] - name: Login to Huawei Cloud SWR uses: docker/login-action@v2 with: registry: ${{ secrets.SWR_SERVER }} username: ${{ secrets.SWR_USERNAME }} password: ${{ secrets.SWR_PASSWORD }} # Build API Image - name: Build and Push API id: build-api uses: docker/build-push-action@v4 with: context: . file: ./Dockerfile push: true provenance: false tags: ${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/log-center-api:latest # Build Web Image - name: Build and Push Web id: build-web uses: docker/build-push-action@v4 with: context: ./web file: ./web/Dockerfile push: true provenance: false tags: ${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/log-center-web:latest build-args: | VITE_API_BASE_URL=https://qiyuan-log-center-api.airlabs.art # Build K8s Monitor Image - name: Build and Push K8s Monitor id: build-monitor uses: docker/build-push-action@v4 with: context: ./k8s-monitor file: ./k8s-monitor/Dockerfile push: true provenance: false tags: ${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/k8s-pod-monitor:latest - name: Setup Kubectl run: | curl -LO "https://files.m.daocloud.io/dl.k8s.io/release/v1.28.2/bin/linux/amd64/kubectl" chmod +x kubectl mv kubectl /usr/local/bin/ - name: Deploy to K3s uses: Azure/k8s-set-context@v3 with: method: kubeconfig kubeconfig: ${{ secrets.KUBE_CONFIG }} - name: Update K8s Manifests id: deploy run: | echo "Environment: Production" # Replace image placeholders sed -i "s|\${CI_REGISTRY_IMAGE}/log-center-api:latest|${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/log-center-api:latest|g" k8s/api-deployment-prod.yaml sed -i "s|\${CI_REGISTRY_IMAGE}/log-center-web:latest|${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/log-center-web:latest|g" k8s/web-deployment-prod.yaml sed -i "s|\${CI_REGISTRY_IMAGE}/k8s-pod-monitor:latest|${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/k8s-pod-monitor:latest|g" k8s/monitor-cronjob.yaml # Apply configurations kubectl apply -f k8s/api-deployment-prod.yaml kubectl apply -f k8s/web-deployment-prod.yaml kubectl apply -f k8s/ingress.yaml kubectl apply -f k8s/monitor-cronjob.yaml # Restart deployments kubectl rollout restart deployment/log-center-api kubectl rollout restart deployment/log-center-web # ==================== CI/CD 错误上报 ==================== - name: Report API Build Failure if: failure() && steps.build-api.outcome == 'failure' run: | curl -s -X POST "${LOG_CENTER_URL}/api/v1/logs/report" \ -H "Content-Type: application/json" \ -d '{ "project_id": "log_center_api", "environment": "cicd", "level": "ERROR", "source": "cicd", "commit_hash": "'"$GITHUB_SHA"'", "error": { "type": "DockerBuildError", "message": "Log Center API Docker build failed", "file_path": null, "line_number": null, "stack_trace": ["API Docker build step failed. Check CI logs for details."] }, "context": { "workflow_name": "'"$GITHUB_WORKFLOW"'", "job_name": "'"$GITHUB_JOB"'", "step_name": "Build and Push API", "run_id": "'"$GITHUB_RUN_ID"'", "branch": "'"$GITHUB_REF_NAME"'", "repository": "'"$GITHUB_REPOSITORY"'", "run_url": "'"$GITHUB_SERVER_URL"'/'"$GITHUB_REPOSITORY"'/actions/runs/'"$GITHUB_RUN_ID"'" } }' --connect-timeout 5 --max-time 10 || true - name: Report Web Build Failure if: failure() && steps.build-web.outcome == 'failure' run: | curl -s -X POST "${LOG_CENTER_URL}/api/v1/logs/report" \ -H "Content-Type: application/json" \ -d '{ "project_id": "log_center_web", "environment": "cicd", "level": "ERROR", "source": "cicd", "commit_hash": "'"$GITHUB_SHA"'", "error": { "type": "DockerBuildError", "message": "Log Center Web Docker build failed", "file_path": null, "line_number": null, "stack_trace": ["Web Docker build step failed. Check CI logs for details."] }, "context": { "workflow_name": "'"$GITHUB_WORKFLOW"'", "job_name": "'"$GITHUB_JOB"'", "step_name": "Build and Push Web", "run_id": "'"$GITHUB_RUN_ID"'", "branch": "'"$GITHUB_REF_NAME"'", "repository": "'"$GITHUB_REPOSITORY"'", "run_url": "'"$GITHUB_SERVER_URL"'/'"$GITHUB_REPOSITORY"'/actions/runs/'"$GITHUB_RUN_ID"'" } }' --connect-timeout 5 --max-time 10 || true - name: Report Monitor Build Failure if: failure() && steps.build-monitor.outcome == 'failure' run: | curl -s -X POST "${LOG_CENTER_URL}/api/v1/logs/report" \ -H "Content-Type: application/json" \ -d '{ "project_id": "log_center_api", "environment": "cicd", "level": "ERROR", "source": "cicd", "commit_hash": "'"$GITHUB_SHA"'", "error": { "type": "DockerBuildError", "message": "K8s Monitor Docker build failed", "file_path": null, "line_number": null, "stack_trace": ["K8s Monitor Docker build step failed. Check CI logs for details."] }, "context": { "workflow_name": "'"$GITHUB_WORKFLOW"'", "job_name": "'"$GITHUB_JOB"'", "step_name": "Build and Push K8s Monitor", "run_id": "'"$GITHUB_RUN_ID"'", "branch": "'"$GITHUB_REF_NAME"'", "repository": "'"$GITHUB_REPOSITORY"'", "run_url": "'"$GITHUB_SERVER_URL"'/'"$GITHUB_REPOSITORY"'/actions/runs/'"$GITHUB_RUN_ID"'" } }' --connect-timeout 5 --max-time 10 || true - name: Report Deploy Failure if: failure() && steps.deploy.outcome == 'failure' run: | curl -s -X POST "${LOG_CENTER_URL}/api/v1/logs/report" \ -H "Content-Type: application/json" \ -d '{ "project_id": "log_center_api", "environment": "cicd", "level": "ERROR", "source": "deployment", "commit_hash": "'"$GITHUB_SHA"'", "error": { "type": "DeployError", "message": "Log Center K8s deployment failed", "file_path": null, "line_number": null, "stack_trace": ["K8s deployment step failed. Check CI logs for details."] }, "context": { "workflow_name": "'"$GITHUB_WORKFLOW"'", "job_name": "'"$GITHUB_JOB"'", "step_name": "Update K8s Manifests", "run_id": "'"$GITHUB_RUN_ID"'", "branch": "'"$GITHUB_REF_NAME"'", "repository": "'"$GITHUB_REPOSITORY"'", "namespace": "default", "deployment_name": "log-center", "run_url": "'"$GITHUB_SERVER_URL"'/'"$GITHUB_REPOSITORY"'/actions/runs/'"$GITHUB_RUN_ID"'" } }' --connect-timeout 5 --max-time 10 || true