docs: 更新 CI/CD 接入指南,改用日志捕获 + run_number 方式
Some checks failed
Build and Deploy Log Center / build-and-deploy (push) Has been cancelled
Some checks failed
Build and Deploy Log Center / build-and-deploy (push) Has been cancelled
- 核心要点:tee 捕获日志、github.run_number、模板语法、单一综合上报 - 完整 Gitea Actions 集成示例(含日志收集和失败判断逻辑) - 字段说明中标注 run_url 的正确拼接方式 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
cd22a5696b
commit
89f19e9776
@ -408,32 +408,39 @@ void main() {
|
|||||||
|
|
||||||
> `source: "cicd"`
|
> `source: "cicd"`
|
||||||
|
|
||||||
在 Gitea Actions 流水线中,为每个关键步骤添加失败上报,构建/测试/Lint 失败时自动上报到 Log Center。
|
在 Gitea Actions 流水线中添加失败上报,构建/测试/部署失败时自动捕获实际错误日志并上报到 Log Center。
|
||||||
|
|
||||||
|
### 核心要点
|
||||||
|
|
||||||
|
1. **用 `tee` 捕获日志** — 构建和部署步骤的输出必须通过 `2>&1 | tee /tmp/xxx.log` 捕获,否则上报的 stack_trace 为空
|
||||||
|
2. **用 `github.run_number`** — URL 中必须使用 `${{ github.run_number }}`(仓库维度序号),**不要用 `github.run_id`**(全局ID,会导致跳转到错误页面)
|
||||||
|
3. **用 `${{ }}` 模板语法** — 比 `$GITHUB_*` 环境变量更可靠
|
||||||
|
4. **单一综合上报步骤** — 一个 `if: failure()` 步骤自动判断哪个阶段失败,收集对应日志
|
||||||
|
|
||||||
### 上报格式
|
### 上报格式
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"project_id": "rtc_backend",
|
"project_id": "rtc_backend",
|
||||||
"environment": "cicd",
|
"environment": "main",
|
||||||
"level": "ERROR",
|
"level": "ERROR",
|
||||||
"source": "cicd",
|
"source": "cicd",
|
||||||
"commit_hash": "abc1234",
|
"commit_hash": "abc1234def5678",
|
||||||
|
"repo_url": "https://gitea.airlabs.art/zyc/rtc_backend.git",
|
||||||
"error": {
|
"error": {
|
||||||
"type": "DockerBuildError",
|
"type": "CICDFailure",
|
||||||
"message": "Docker build failed",
|
"message": "[build] Build and Deploy failed on branch main",
|
||||||
"file_path": null,
|
"stack_trace": ["...实际构建日志最后 50 行..."]
|
||||||
"line_number": null,
|
|
||||||
"stack_trace": ["Build step failed. Check CI logs for details."]
|
|
||||||
},
|
},
|
||||||
"context": {
|
"context": {
|
||||||
"workflow_name": "Build and Deploy",
|
"job_name": "build-and-deploy",
|
||||||
"job_name": "build",
|
"step_name": "build",
|
||||||
"step_name": "Build Docker Image",
|
"workflow": "Build and Deploy",
|
||||||
"run_id": "123",
|
"run_id": "24",
|
||||||
"branch": "main",
|
"branch": "main",
|
||||||
"repository": "team/rtc_backend",
|
"actor": "zyc",
|
||||||
"run_url": "https://gitea.airlabs.art/team/rtc_backend/actions/runs/123"
|
"commit": "abc1234def5678",
|
||||||
|
"run_url": "https://gitea.airlabs.art/zyc/rtc_backend/actions/runs/24"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -443,20 +450,19 @@ void main() {
|
|||||||
| 字段 | 说明 |
|
| 字段 | 说明 |
|
||||||
|------|------|
|
|------|------|
|
||||||
| `source` | **必须**设为 `"cicd"` |
|
| `source` | **必须**设为 `"cicd"` |
|
||||||
| `environment` | 设为 `"cicd"` |
|
| `environment` | 用分支名 `${{ github.ref_name }}`,如 `main`、`dev` |
|
||||||
| `error.type` | 推荐值:`DockerBuildError`, `NpmBuildError`, `TestFailure`, `LintError`, `CIBuildError` |
|
| `repo_url` | 仓库地址,便于 Repair Agent 关联 |
|
||||||
| `error.file_path` | 可为 `null` |
|
| `error.type` | 推荐 `CICDFailure`(通用)或 `DockerBuildError` / `TestFailure` / `DeployError` |
|
||||||
| `error.line_number` | 可为 `null` |
|
| `error.stack_trace` | **实际错误日志**(通过 `tee` 捕获),不要写死占位文字 |
|
||||||
| `context.workflow_name` | 工作流名称 |
|
| `context.run_id` | **必须用 `${{ github.run_number }}`**(不是 `github.run_id`) |
|
||||||
| `context.job_name` | Job 名称 |
|
| `context.run_url` | 拼接方式:`https://gitea.airlabs.art/${{ github.repository }}/actions/runs/${{ github.run_number }}` |
|
||||||
| `context.step_name` | 失败的步骤名称 |
|
| `context.step_name` | 失败的步骤名称 |
|
||||||
| `context.run_id` | 运行 ID |
|
| `context.actor` | 触发者 |
|
||||||
| `context.run_url` | CI 运行详情链接 |
|
| `context.commit` | 完整 commit hash |
|
||||||
| `context.branch` | 分支名 |
|
|
||||||
|
|
||||||
### Gitea Actions 集成方式
|
### Gitea Actions 集成方式(推荐)
|
||||||
|
|
||||||
为每个关键步骤添加 `id`,然后在末尾添加条件上报步骤:
|
以下是完整示例,关键点:构建步骤用 `tee` 捕获日志,末尾一个综合上报步骤自动判断失败阶段。
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
name: Build and Deploy
|
name: Build and Deploy
|
||||||
@ -465,87 +471,94 @@ on:
|
|||||||
push:
|
push:
|
||||||
branches: [main]
|
branches: [main]
|
||||||
|
|
||||||
env:
|
|
||||||
LOG_CENTER_URL: https://qiyuan-log-center-api.airlabs.art
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build-and-deploy:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
# ===== 构建步骤:用 tee 捕获日志 =====
|
||||||
|
|
||||||
# 关键步骤:添加 id
|
|
||||||
- name: Build Docker Image
|
- name: Build Docker Image
|
||||||
id: build
|
id: build
|
||||||
run: docker build -t myapp:latest .
|
run: |
|
||||||
|
set -o pipefail
|
||||||
- name: Run Tests
|
docker buildx build \
|
||||||
id: test
|
--push \
|
||||||
run: docker run myapp:latest python -m pytest
|
--provenance=false \
|
||||||
|
--tag your-registry/your-app:latest \
|
||||||
|
. 2>&1 | tee /tmp/build.log
|
||||||
|
|
||||||
- name: Deploy
|
- name: Deploy
|
||||||
id: deploy
|
id: deploy
|
||||||
run: kubectl apply -f k8s/
|
|
||||||
|
|
||||||
# ===== 失败上报步骤(放在所有关键步骤之后) =====
|
|
||||||
|
|
||||||
- name: Report Build Failure
|
|
||||||
if: failure() && steps.build.outcome == 'failure'
|
|
||||||
run: |
|
run: |
|
||||||
curl -s -X POST "${LOG_CENTER_URL}/api/v1/logs/report" \
|
set -o pipefail
|
||||||
-H "Content-Type: application/json" \
|
{
|
||||||
-d '{
|
kubectl apply -f k8s/deployment.yaml
|
||||||
"project_id": "'"${GITHUB_REPOSITORY##*/}"'",
|
kubectl rollout restart deployment/your-app
|
||||||
"environment": "cicd",
|
} 2>&1 | tee /tmp/deploy.log
|
||||||
"level": "ERROR",
|
|
||||||
"source": "cicd",
|
|
||||||
"commit_hash": "'"$GITHUB_SHA"'",
|
|
||||||
"error": {
|
|
||||||
"type": "DockerBuildError",
|
|
||||||
"message": "Docker build failed",
|
|
||||||
"file_path": null,
|
|
||||||
"line_number": null,
|
|
||||||
"stack_trace": ["Docker build step failed. Check CI logs."]
|
|
||||||
},
|
|
||||||
"context": {
|
|
||||||
"workflow_name": "'"$GITHUB_WORKFLOW"'",
|
|
||||||
"job_name": "'"$GITHUB_JOB"'",
|
|
||||||
"step_name": "Build Docker Image",
|
|
||||||
"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 Test Failure
|
# ===== 失败上报(单一综合步骤) =====
|
||||||
if: failure() && steps.test.outcome == 'failure'
|
|
||||||
|
- name: Report failure to Log Center
|
||||||
|
if: failure()
|
||||||
run: |
|
run: |
|
||||||
curl -s -X POST "${LOG_CENTER_URL}/api/v1/logs/report" \
|
# 判断哪个步骤失败,收集对应日志
|
||||||
|
BUILD_LOG=""
|
||||||
|
DEPLOY_LOG=""
|
||||||
|
FAILED_STEP="unknown"
|
||||||
|
|
||||||
|
if [[ "${{ steps.build.outcome }}" == "failure" ]]; then
|
||||||
|
FAILED_STEP="build"
|
||||||
|
if [ -f /tmp/build.log ]; then
|
||||||
|
BUILD_LOG=$(tail -50 /tmp/build.log | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g')
|
||||||
|
fi
|
||||||
|
elif [[ "${{ steps.deploy.outcome }}" == "failure" ]]; then
|
||||||
|
FAILED_STEP="deploy"
|
||||||
|
if [ -f /tmp/deploy.log ]; then
|
||||||
|
DEPLOY_LOG=$(tail -50 /tmp/deploy.log | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g')
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
ERROR_LOG="${BUILD_LOG}${DEPLOY_LOG}"
|
||||||
|
if [ -z "$ERROR_LOG" ]; then
|
||||||
|
ERROR_LOG="No captured output. Check Gitea Actions UI for details."
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 判断 source
|
||||||
|
if [[ "$FAILED_STEP" == "deploy" ]]; then
|
||||||
|
SOURCE="deployment"
|
||||||
|
ERROR_TYPE="DeployError"
|
||||||
|
else
|
||||||
|
SOURCE="cicd"
|
||||||
|
ERROR_TYPE="DockerBuildError"
|
||||||
|
fi
|
||||||
|
|
||||||
|
curl -s -X POST "https://qiyuan-log-center-api.airlabs.art/api/v1/logs/report" \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d '{
|
-d "{
|
||||||
"project_id": "'"${GITHUB_REPOSITORY##*/}"'",
|
\"project_id\": \"your_project_id\",
|
||||||
"environment": "cicd",
|
\"environment\": \"${{ github.ref_name }}\",
|
||||||
"level": "ERROR",
|
\"level\": \"ERROR\",
|
||||||
"source": "cicd",
|
\"source\": \"${SOURCE}\",
|
||||||
"commit_hash": "'"$GITHUB_SHA"'",
|
\"commit_hash\": \"${{ github.sha }}\",
|
||||||
"error": {
|
\"repo_url\": \"https://gitea.airlabs.art/zyc/your_project.git\",
|
||||||
"type": "TestFailure",
|
\"error\": {
|
||||||
"message": "Tests failed in CI pipeline",
|
\"type\": \"${ERROR_TYPE}\",
|
||||||
"file_path": null,
|
\"message\": \"[${FAILED_STEP}] Build and Deploy failed on branch ${{ github.ref_name }}\",
|
||||||
"line_number": null,
|
\"stack_trace\": [\"${ERROR_LOG}\"]
|
||||||
"stack_trace": ["Test step failed. Check CI logs."]
|
|
||||||
},
|
},
|
||||||
"context": {
|
\"context\": {
|
||||||
"workflow_name": "'"$GITHUB_WORKFLOW"'",
|
\"job_name\": \"build-and-deploy\",
|
||||||
"job_name": "'"$GITHUB_JOB"'",
|
\"step_name\": \"${FAILED_STEP}\",
|
||||||
"step_name": "Run Tests",
|
\"workflow\": \"${{ github.workflow }}\",
|
||||||
"run_id": "'"$GITHUB_RUN_ID"'",
|
\"run_id\": \"${{ github.run_number }}\",
|
||||||
"branch": "'"$GITHUB_REF_NAME"'",
|
\"branch\": \"${{ github.ref_name }}\",
|
||||||
"repository": "'"$GITHUB_REPOSITORY"'",
|
\"actor\": \"${{ github.actor }}\",
|
||||||
"run_url": "'"$GITHUB_SERVER_URL"'/'"$GITHUB_REPOSITORY"'/actions/runs/'"$GITHUB_RUN_ID"'"
|
\"commit\": \"${{ github.sha }}\",
|
||||||
|
\"run_url\": \"https://gitea.airlabs.art/${{ github.repository }}/actions/runs/${{ github.run_number }}\"
|
||||||
}
|
}
|
||||||
}' --connect-timeout 5 --max-time 10 || true
|
}" || true
|
||||||
```
|
```
|
||||||
|
|
||||||
### 使用 report-cicd-error.sh 脚本
|
### 使用 report-cicd-error.sh 脚本
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user