From c749442ac8914428f3c5566ee753224a69487961 Mon Sep 17 00:00:00 2001 From: liuxiaohua Date: Mon, 3 Mar 2025 14:41:50 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20[2025-03-03]=20=E6=B7=BB=E5=8A=A0Je?= =?UTF-8?q?nkins=E7=BB=93=E5=90=88Jacoco=E5=AE=9E=E7=8E=B0=E8=A6=86?= =?UTF-8?q?=E7=9B=96=E7=8E=87=E6=8A=A5=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 任务/周报.md | 6 + .../20250303-Jenkins整合Jacoco实现覆盖率报告.md | 192 ++++++++++++++++++ 材料/00-基础镜像地址.md | 55 +++++ 草稿/20250226-Gitflow.md | 22 ++ 草稿/20250226-夜莺整合GrafanaOnCall.md | 140 +++++++++++++ 草稿/Jenkinsfile | 48 +++++ 6 files changed, 463 insertions(+) create mode 100644 教程/20250303-Jenkins整合Jacoco实现覆盖率报告.md create mode 100644 材料/00-基础镜像地址.md create mode 100644 草稿/20250226-Gitflow.md create mode 100644 草稿/20250226-夜莺整合GrafanaOnCall.md create mode 100644 草稿/Jenkinsfile diff --git a/任务/周报.md b/任务/周报.md index 8933b34..cf64c98 100644 --- a/任务/周报.md +++ b/任务/周报.md @@ -1,3 +1,9 @@ +## 20250224-20250228 +- [ ] 试用期规划 +- [ ] JeecgBoot搭建及初步使用 +- [ ] KubeSphere API: https://kubesphere.io/api/kubesphere/ +- [ ] KubeSphere DevOps + ## 20250120-20250123 - [X] 梳理南北流量网关,重构具有歧义调用方法 diff --git a/教程/20250303-Jenkins整合Jacoco实现覆盖率报告.md b/教程/20250303-Jenkins整合Jacoco实现覆盖率报告.md new file mode 100644 index 0000000..6898ee1 --- /dev/null +++ b/教程/20250303-Jenkins整合Jacoco实现覆盖率报告.md @@ -0,0 +1,192 @@ + + + + + + + + + + + + + + +# Jenkins整合Jacoco实现覆盖率报告 + +## 前置说明 +- [Jenkins 安装](https://plugins.jenkins.io/maven-plugin) +- Jenkins 插件安装 + - [Git plugin][Git plugin] + - [Git Forensics Plugin][Git Forensics Plugin] + - [Git Parameter Plug-In][Git Parameter Plug-In] + - [Coverage Plugin][Coverage Plugin] + - [Maven Integration plugin][Maven Integration plugin] +- [Maven](https://plugins.jenkins.io/maven-plugin) +- [Jacoco](https://www.jacoco.org/) + +## Maven添加Jacoco插件 +### 方法一、更新parent版本 +- 项目中父依赖为:`qifu-saas-parent` +- 更新 `qifu-saas-parent` 版本 `[1.0.14,)`。开发测试 `[1.0.14-SNAPSHOT,)` + +### 方法二、直接添加插件 +- pom.xml 中添加插件 +- ```xml + + + org.apache.maven.plugins + maven-surefire-plugin + 3.5.2 + + ${argLine} + 4 + true + + + + org.jacoco + jacoco-maven-plugin + 0.8.12 + + + prepare-agent + + prepare-agent + + + + report + test + + report + + + + + ``` + +## 集成覆盖率报告 +### Jenkins安装插件 + - [Git plugin][Git plugin] + - [Git Forensics Plugin][Git Forensics Plugin] + - [Git Parameter Plug-In][Git Parameter Plug-In] + - [Coverage Plugin][Coverage Plugin] + - [Maven Integration plugin][Maven Integration plugin] + +### Jenkins流水线配置 +- 可以参考:https://github.com/jenkinsci/coverage-plugin?tab=readme-ov-file#usage +#### 自由风格项目(Freestyle Project) +- 添加构建后操作(Post Step) +- 增量对比: +- ![](https://picture.texous.cn/blog/20250303104613312.png) +- 报告采集展示 +- ![](https://picture.texous.cn/blog/20250303104707049.png) + +#### 流水线(Pipeline) +- 编写Jenkinsfile +- ```groovy + pipeline { + agent any + tools { + maven "MavenInner" + } + parameters { + gitParameter(branch: '', branchFilter: '.*', defaultValue: 'master', name: 'buildBranch', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'GitParameterDefinition') + } + environment { + GIT_URL='ssh://git@git.keyfil.com:9922/qifu-gateway/keycloak-justauth.git' + } + stages { + stage('Checkout') { + steps { + echo "正在从 GitLab 拉取分支的代码..." + git branch: "main", credentialsId: "1", url: "${GIT_URL}" + } + } + stage('Maven Build') { + steps { + echo "开始执行 Maven 构建..." + sh 'mvn clean test jacoco:report -Daliyun=*' + } + } + stage('Generate Coverage Report') { + steps { + echo "使用 Git Forensics 统计代码变更..." + discoverGitReferenceBuild referenceJob: 'Jacoco测试', targetBranch: '$referenceJob' + recordCoverage qualityGates: [[criticality: 'NOTE', metric: 'MODULE'], [baseline: 'MODIFIED_FILES', criticality: 'NOTE', metric: 'FILE'], [baseline: 'MODIFIED_LINES', criticality: 'NOTE', metric: 'LINE']], tools: [[parser: 'JACOCO']] + } + } + } + post { + success { + echo "构建成功!" + } + failure { + echo "构建失败,请检查日志。" + } + } + } + ``` + +### 构建测试 + +## 编写单元测试 +### 添加pom依赖 +```xml + + org.springframework.boot + spring-boot-starter-test + test + +``` +### 编写单元测试 +```java +package io.github.yanfeiwuji.justauth.social; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class WeworkIdentityProviderFactoryTest { + + private WeworkIdentityProviderFactory weworkIdentityProviderFactoryUnderTest; + + @BeforeEach + void setUp() { + weworkIdentityProviderFactoryUnderTest = new WeworkIdentityProviderFactory(); + } + + @Test + void testGetName() throws Exception { + assertEquals("企业微信", weworkIdentityProviderFactoryUnderTest.getName()); + } +} + +``` + +## 高级使用 +### Squaretest插件 +- 安装 Squaretest 插件并破解 + - [直接下载](http://confluence.qifu.com/download/attachments/38510668/Squaretest.zip?api=v2) + - [破解教程参考](https://blog.csdn.net/binbushi/article/details/135942290) +- 右键 > Generate > Generate test + +## 参考 + + +[Git plugin]: https://plugins.jenkins.io/git +[Git Forensics Plugin]: https://plugins.jenkins.io/git-forensics +[Git Parameter Plug-In]: https://plugins.jenkins.io/git-parameter +[Coverage Plugin]: https://plugins.jenkins.io/coverage +[Maven Integration plugin]: https://plugins.jenkins.io/maven-plugin \ No newline at end of file diff --git a/材料/00-基础镜像地址.md b/材料/00-基础镜像地址.md new file mode 100644 index 0000000..5f9ce09 --- /dev/null +++ b/材料/00-基础镜像地址.md @@ -0,0 +1,55 @@ +# 基础镜像地址记录 + +## Jenkins插件镜像 + +- 配置路径:`Dashboard > Manage Jenkins > Plugins > Advanced settings > Update Site > URL` +- 镜像地址:`https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json` +- 点击 `submit` 使其生效 + +## Maven镜像仓库 +### 阿里云Maven镜像仓库 +- 配置路径 `settings.xml` +- ```xml + + aliyun-central + aliyun-central + https://maven.aliyun.com/repository/central + central + + + aliyun-public + aliyun-public + https://maven.aliyun.com/repository/public + public + + ``` + +## NPM镜像仓库 +- 使用方式 +- ```shell + npm config set registry https://registry.npmmirror.com + #- 或者 + npm install xxx --registry https://registry.npmmirror.com + ``` + +## Docker镜像仓库 +- 为了加速镜像拉取,你可以使用以下命令设置 registry mirror: +- ```shell + sudo tee /etc/docker/daemon.json <|Webhook 告警推送| B[Grafana OnCall] + B -->|通知值班人员| C[钉钉/企业微信/短信] + B -->|告警状态同步| A +``` + +--- + +### **二、核心步骤** + +#### **步骤 1:配置 Grafana OnCall 告警接收通道** + +1. **创建 OnCall Webhook 集成**: + - 进入 Grafana OnCall → **Integrations** → **New Integration** → 选择 **Webhook**。 + - 记录生成的 **Webhook URL**(如 `https://oncall-api.example.com/integrations/webhook/abc123/`)。 + +2. **配置 Payload 模板**(适配 Nightingale 告警格式): + ```json + { + "title": "{{ .CommonLabels.alertname }}", + "message": "{{ .CommonAnnotations.summary }}", + "status": "{{ .Status }}", + "severity": "{{ .CommonLabels.severity }}", + "fingerprint": "{{ .Fingerprint }}", + "source": "nightingale" + } + ``` + +--- + +#### **步骤 2:配置 Nightingale 告警转发到 Grafana OnCall** + +1. **在 Nightingale 中创建通知模板**: + - 进入 Nightingale → **告警管理** → **通知模板** → **新建模板**。 + - 类型选择 **Webhook**,URL 填写 Grafana OnCall 的 Webhook URL。 + +2. **设置告警规则关联 Webhook**: + - 编辑告警规则 → **通知配置** → 选择刚创建的 Webhook 模板。 + - 自定义告警参数映射(确保 `alertname`、`summary` 等字段正确传递)。 + +--- + +#### **步骤 3:配置 OnCall 排班与通知策略** + +1. **创建值班表**: + - 进入 Grafana OnCall → **Schedules** → **New Schedule**。 + - 设置轮班规则(如 24x7 轮班、工作日/节假日)、时区、交接提醒。 + +2. **配置通知渠道**: + - **Channels** → 添加钉钉机器人、企业微信应用、短信网关等。 + - 测试通知是否可达。 + +3. **设置告警路由规则**: + - **Routes** → 根据标签(如 `severity=critical`)将告警路由到不同值班组。 + - 示例:`severity=critical → 电话通知;severity=warning → 企业微信群通知`。 + +--- + +#### **步骤 4:告警状态回传同步(可选)** + +若需在 Nightingale 中同步 OnCall 处理状态,需开发回调接口: + +```python +# 示例:Grafana OnCall → Nightingale 状态同步 +@app.route("/oncall-callback", methods=["POST"]) +def handle_oncall_callback(): + data = request.json + alert_id = data.get("fingerprint") + status = "resolved" if data.get("status") == "ok" else "firing" + # 调用 Nightingale API 更新告警状态 + requests.patch( + f"{NIGHTINGALE_URL}/api/v1/alerts/{alert_id}", + json={"status": status}, + headers={"Authorization": "Bearer {API_KEY}"} + ) + return jsonify({"status": "success"}) +``` + +--- + +### **三、关键配置说明** + +| 组件 | 配置项 | 作用 | +|--------------------|------------|-------------------------------| +| **Nightingale** | Webhook 模板 | 将告警格式转换为 OnCall 兼容的 JSON 结构 | +| **Grafana OnCall** | Payload 模板 | 解析 Nightingale 告警字段(标题、描述、状态) | +| **Grafana OnCall** | 告警路由规则 | 根据标签(如 `severity`)定向通知值班人员 | +| **Nightingale** | 回调接口(可选) | 同步 OnCall 处理状态,保持告警状态一致性 | + +--- + +### **四、验证与调试** + +1. **触发测试告警**: + ```bash + # 使用 Nightingale 的测试告警功能 + curl -X POST http://nightingale:17000/api/v1/alerts \ + -H "Content-Type: application/json" \ + -d '{"labels":{"alertname":"TestAlert","severity":"critical"},"annotations":{"summary":"Integration test"}}' + ``` + +2. **检查 OnCall 告警流水**: + - 进入 Grafana OnCall → **Alert Groups**,确认测试告警已接收并分配。 + +3. **验证通知渠道**: + - 确保值班人员收到钉钉/短信通知,且内容包含告警详情。 + +--- + +### **五、性能优化建议** + +1. **告警聚合**: + 在 Nightingale 中设置合理的告警分组规则,避免 OnCall 被高频告警淹没。 + +2. **分级通知**: + - 使用 OnCall 的 **Escalation Policies**,配置多级通知(如 5 分钟未响应则通知上级)。 + +3. **去重与静默**: + - 利用 OnCall 的 **Alert Manager** 功能,对重复告警自动合并或静默。 + +--- + +### **六、故障排查** + +| 现象 | 可能原因 | 解决方案 | +|--------------|------------------|-----------------------------------| +| OnCall 未收到告警 | Webhook URL 配置错误 | 检查 Nightingale 的 Webhook 地址和网络连通性 | +| 告警字段缺失 | Payload 模板不匹配 | 调整 OnCall 的 Payload 模板匹配字段 | +| 通知延迟 | 渠道限速或网络问题 | 检查钉钉/企业微信的 API 调用频率限制 | + +--- + +通过以上方案,可实现 **Nightingale 告警生成 → Grafana OnCall 值班管理** 的全链路自动化,显著提升运维响应效率。 \ No newline at end of file diff --git a/草稿/Jenkinsfile b/草稿/Jenkinsfile new file mode 100644 index 0000000..0bb4b87 --- /dev/null +++ b/草稿/Jenkinsfile @@ -0,0 +1,48 @@ +pipeline { + agent any +// options { +// ansiColor('xterm') +// } +// properties([parameters([gitParameter(branch: '', branchFilter: '.*', defaultValue: '*/master', name: 'buildBranch', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'GitParameterDefinition')])]) + tools { + maven: 'MavenInner' + } + parameters { +// string(name: 'BRANCH_NAME', defaultValue: 'main', description: '请输入要构建的分支名称') + gitParameter(branch: '', branchFilter: '.*', defaultValue: '*/master', name: 'buildBranch', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'GitParameterDefinition') + } + environment { + GIT_CREDENTIALS = credentials('your-gitlab-credentials-id') + GIT_URL='ssh://git@git.keyfil.com:9922/qifu-gateway/keycloak-justauth.git' + } + stages { + stage('Checkout') { + steps { + echo "正在从 GitLab 拉取 ${params.BRANCH_NAME} 分支的代码..." +// git credentialsId: "${GIT_CREDENTIALS}", url: 'https://gitlab.com/your-repo.git', branch: "${params.BRANCH_NAME}" + git branch: "${params.buildBranch}", credentialsId: '1', url: "${GIT_URL}" + } + } + stage('Maven Build') { + steps { + echo "开始执行 Maven 构建..." + sh 'mvn clean test jacoco:report -Daliyun=*' + } + } + stage('Generate Coverage Report') { + steps { + echo "使用 Git Forensics 统计代码变更..." + discoverGitReferenceBuild referenceJob: 'Jacoco测试', targetBranch: '$referenceJob' + recordCoverage qualityGates: [[criticality: 'NOTE', metric: 'MODULE'], [baseline: 'MODIFIED_FILES', criticality: 'NOTE', metric: 'FILE'], [baseline: 'MODIFIED_LINES', criticality: 'NOTE', metric: 'LINE']], tools: [[parser: 'JACOCO']] + } + } + } + post { + success { + echo "构建成功!" + } + failure { + echo "构建失败,请检查日志。" + } + } +} \ No newline at end of file