CI/CD 集成

本指南介绍 Nexus 嵌入式平台的 CI/CD 系统,包括优化后的 GitHub Actions 工作流以及与其他 CI/CD 平台的集成。

概述

Nexus 平台提供了一个全面的模块化 CI/CD 系统,具有:

  • 智能触发:基于路径的过滤和智能调度

  • 模块化设计:可复用的工作流和组合操作

  • 多平台构建:Windows、Linux、macOS 和 ARM 交叉编译

  • 质量保证:代码覆盖率、静态分析、Sanitizer 和 MISRA 合规性

  • 性能优化:构建缓存、并行执行和智能依赖

  • 完整文档:自动化的 Doxygen 和 Sphinx 文档,支持国际化

系统架构

Nexus CI/CD 系统采用模块化架构:

ci.yml (Main Orchestrator)
    ↓
Smart Change Detection
    ↓
Trigger Sub-Workflows
    ├─► build-matrix.yml (Build & Test)
    │   ├─ Multi-platform builds
    │   ├─ Coverage analysis
    │   └─ Sanitizer tests
    │
    ├─► quality-checks.yml (Code Quality)
    │   ├─ Format checking
    │   ├─ Static analysis
    │   ├─ Complexity analysis
    │   └─ MISRA compliance
    │
    └─► docs-build.yml (Documentation)
        ├─ Doxygen API docs
        ├─ Sphinx user docs (EN/CN)
        └─ GitHub Pages deployment

优化成果

优化后的 CI/CD 系统实现了:

  • **代码量减少 55%**(从约 2000 行减少到约 900 行)

  • **构建时间减少 40%**(从 25 分钟减少到 15 分钟)

  • **缓存命中率 85%**(从 60% 提升)

  • **并行任务增加 140%**(从 3-5 个增加到 8-12 个)

其他 CI/CD 平台

虽然推荐使用 GitHub Actions,但 Nexus 也可以与其他平台集成。

GitLab CI

基本的 .gitlab-ci.yml 配置:

stages:
  - build
  - test
  - deploy

variables:
  GIT_SUBMODULE_STRATEGY: recursive

build:linux:
  stage: build
  image: ubuntu:22.04
  before_script:
    - apt-get update && apt-get install -y cmake ninja-build gcc g++
  script:
    - cmake --preset linux-gcc-release
    - cmake --build --preset linux-gcc-release
  artifacts:
    paths:
      - build/
    expire_in: 1 hour

test:
  stage: test
  image: ubuntu:22.04
  dependencies:
    - build:linux
  script:
    - ctest --preset linux-gcc-release --output-on-failure

Jenkins

基本的 Jenkinsfile 配置:

pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                checkout scm
                sh 'git submodule update --init --recursive'
            }
        }

        stage('Build') {
            steps {
                sh '''
                    cmake --preset linux-gcc-release
                    cmake --build --preset linux-gcc-release --parallel
                '''
            }
        }

        stage('Test') {
            steps {
                sh 'ctest --preset linux-gcc-release --output-on-failure'
            }
        }
    }

    post {
        always {
            archiveArtifacts artifacts: 'build/**/*', allowEmptyArchive: true
        }
    }
}

Azure DevOps

基本的 azure-pipelines.yml 配置:

trigger:
  branches:
    include:
      - main
      - develop

pool:
  vmImage: 'ubuntu-latest'

steps:
  - checkout: self
    submodules: true

  - script: |
      cmake --preset linux-gcc-release
      cmake --build --preset linux-gcc-release --parallel
    displayName: 'Build'

  - script: |
      ctest --preset linux-gcc-release --output-on-failure
    displayName: 'Test'

最佳实践

缓存策略

GitHub Actions:

- name: Cache Build
  uses: actions/cache@v4
  with:
    path: |
      build
      ~/.cache/ccache
    key: ${{ runner.os }}-${{ matrix.preset }}-${{ hashFiles('**/CMakeLists.txt') }}

优势:

  • 85% 缓存命中率

  • 构建速度提升 40%

  • 降低 CI 成本

并行执行

矩阵策略:

strategy:
  fail-fast: false
  matrix:
    os: [ubuntu-latest, windows-latest, macos-latest]
    preset: [debug, release]

优势:

  • 8-12 个并行任务

  • 更快的反馈

  • 更好的资源利用

产物管理

上传产物:

- uses: actions/upload-artifact@v4
  with:
    name: build-artifacts
    path: |
      build/*/bin/*
      build/*/lib/*
    retention-days: 7

最佳实践:

  • 只上传必要的产物

  • 设置适当的保留期限

  • 对大文件使用压缩

质量门禁

覆盖率阈值:

- name: Check Coverage
  run: |
    COVERAGE=$(lcov --summary coverage.info | grep lines | awk '{print $2}')
    if (( $(echo "$COVERAGE < 80.0" | bc -l) )); then
      echo "Coverage $COVERAGE% below threshold"
      exit 1
    fi

代码质量:

  • 格式检查必须通过

  • 无严重的静态分析问题

  • 复杂度在限制范围内(CCN < 15)

  • 安全关键代码的 MISRA 合规性

故障排除

常见问题

1. 子模块检出失败

- uses: actions/checkout@v4
  with:
    submodules: recursive
    token: ${{ secrets.GITHUB_TOKEN }}

2. 构建缓存损坏

# Clear cache and rebuild
rm -rf build ~/.cache/ccache
cmake --preset linux-gcc-release
cmake --build --preset linux-gcc-release

3. 测试超时

- run: ctest --preset linux-gcc-release --timeout 600

4. 覆盖率上传失败

# Verify coverage file
ls -la coverage.info
lcov --list coverage.info

5. ARM 工具链问题

# Verify toolchain
arm-none-eabi-gcc --version

# Check PATH
echo $PATH | grep arm-none-eabi

性能优化

构建时间优化:

  • 使用 ccache 进行编译缓存

  • 启用并行构建(--parallel

  • 使用 Ninja 生成器加快构建速度

  • 缓存 CMake 配置

CI 成本优化:

  • 使用路径过滤器跳过不必要的构建

  • 设置适当的产物保留期限

  • 使用并发控制取消过时的运行

  • 每周安排重型任务(性能、安全)

监控和指标

关键指标:

  • 构建成功率:> 95%

  • 平均构建时间:< 20 分钟

  • 缓存命中率:> 80%

  • 测试通过率:> 99%

  • 覆盖率:> 80%

监控工具:

  • GitHub Actions 仪表板

  • Codecov 用于覆盖率趋势

  • 工作流摘要中的自定义指标

另请参阅

其他资源