diff --git a/.gitea/workflows/deploy.yaml b/.gitea/workflows/deploy.yaml index f16744a..0e03797 100644 --- a/.gitea/workflows/deploy.yaml +++ b/.gitea/workflows/deploy.yaml @@ -28,12 +28,15 @@ jobs: password: ${{ secrets.SWR_PASSWORD }} - name: Build and Push Backend - uses: docker/build-push-action@v4 - with: - context: . - push: true - provenance: false - tags: ${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/rtc-backend:latest + id: build + run: | + docker buildx build \ + --push \ + --provenance=false \ + --tag ${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/rtc-backend:latest \ + . 2>&1 | tee /tmp/build.log + echo "build_exit_code=${PIPESTATUS[0]}" >> $GITHUB_OUTPUT + exit ${PIPESTATUS[0]} - name: Setup Kubectl run: | @@ -48,6 +51,7 @@ jobs: kubeconfig: ${{ secrets.KUBE_CONFIG }} - name: Update K8s Manifests + id: deploy run: | # 1. 判断分支,决定使用哪个配置文件 if [[ "${{ github.ref_name }}" == "main" || "${{ github.ref_name }}" == "master" ]]; then @@ -65,15 +69,40 @@ jobs: # 2. 替换镜像地址 sed -i "s|\${CI_REGISTRY_IMAGE}/backend:latest|${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/rtc-backend:latest|g" $DEPLOY_FILE - # 3. 应用配置 - kubectl apply -f $DEPLOY_FILE - kubectl apply -f $INGRESS_FILE - - kubectl rollout restart deployment/$DEPLOY_NAME + # 3. 应用配置并捕获输出 + { + kubectl apply -f $DEPLOY_FILE + kubectl apply -f $INGRESS_FILE + kubectl rollout restart deployment/$DEPLOY_NAME + } 2>&1 | tee /tmp/deploy.log - name: Report failure to Log Center if: failure() run: | + # 收集构建和部署日志(取最后 50 行) + BUILD_LOG="" + DEPLOY_LOG="" + FAILED_STEP="unknown" + + if [ -f /tmp/build.log ]; then + BUILD_LOG=$(tail -50 /tmp/build.log | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g') + FAILED_STEP="build" + fi + if [ -f /tmp/deploy.log ]; then + DEPLOY_LOG=$(tail -50 /tmp/deploy.log | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g') + if [ -n "$DEPLOY_LOG" ]; then + FAILED_STEP="deploy" + fi + fi + + # 如果构建日志为空(action 级别失败),标记步骤 + if [ -z "$BUILD_LOG" ] && [ -z "$DEPLOY_LOG" ]; then + BUILD_LOG="No captured output. Check Gitea Actions UI for details." + FAILED_STEP="pre-build" + fi + + ERROR_LOG="${BUILD_LOG}${DEPLOY_LOG}" + curl -s -X POST "https://qiyuan-log-center-api.airlabs.art/api/v1/logs/report" \ -H "Content-Type: application/json" \ -d "{ @@ -85,19 +114,15 @@ jobs: \"repo_url\": \"https://gitea.airlabs.art/zyc/rtc_backend.git\", \"error\": { \"type\": \"CICDFailure\", - \"message\": \"Build and Deploy failed on branch ${{ github.ref_name }}\", - \"stack_trace\": [ - \"Repository: ${{ github.repository }}\", - \"Branch: ${{ github.ref_name }}\", - \"Commit: ${{ github.sha }}\", - \"Run ID: ${{ github.run_id }}\", - \"Actor: ${{ github.actor }}\" - ] + \"message\": \"[${FAILED_STEP}] Build and Deploy failed on branch ${{ github.ref_name }}\", + \"stack_trace\": [\"${ERROR_LOG}\"] }, \"context\": { \"workflow\": \"${{ github.workflow }}\", \"run_id\": \"${{ github.run_id }}\", \"branch\": \"${{ github.ref_name }}\", - \"actor\": \"${{ github.actor }}\" + \"actor\": \"${{ github.actor }}\", + \"failed_step\": \"${FAILED_STEP}\", + \"commit\": \"${{ github.sha }}\" } }" || true