Skip to content

chore: better maintainability #1

chore: better maintainability

chore: better maintainability #1

Workflow file for this run

name: CI/CD
on:
push:
branches: [ main, master, develop ]
pull_request:
branches: [ main, master, develop ]
workflow_dispatch:
env:
BUILD_TYPE: Release
jobs:
build-and-test:
name: Build and Test - ${{ matrix.os }} (${{ matrix.arch }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
# Linux x86_64 (主要测试平台)
- os: ubuntu-latest
arch: x86_64
cmake_build_type: Release
use_ros2: false
use_qemu: false
# Linux x86_64 Debug 构建
- os: ubuntu-latest
arch: x86_64
cmake_build_type: Debug
use_ros2: false
use_qemu: false
# Linux x86_64 with ROS2
- os: ubuntu-22.04
arch: x86_64
cmake_build_type: Release
use_ros2: true
ros2_distro: humble
use_qemu: false
# Linux aarch64 Release 构建 (使用 QEMU 模拟)
- os: ubuntu-latest
arch: aarch64
cmake_build_type: Release
use_ros2: false
use_qemu: true
steps:
- name: 检出代码
uses: actions/checkout@v4
- name: 设置 QEMU (aarch64)
if: matrix.use_qemu == true
uses: docker/setup-qemu-action@v3
with:
platforms: arm64
- name: 缓存 CMake 构建
uses: actions/cache@v4
with:
path: |
build
install
log
~/.cmake
key: ${{ runner.os }}-cmake-${{ matrix.arch }}-${{ matrix.cmake_build_type }}-${{ matrix.use_ros2 }}-${{ hashFiles('**/CMakeLists.txt') }}
restore-keys: |
${{ runner.os }}-cmake-${{ matrix.arch }}-${{ matrix.cmake_build_type }}-${{ matrix.use_ros2 }}-
- name: 安装构建依赖
run: |
sudo apt-get update
sudo apt-get install -y \
build-essential \
cmake \
git \
libpthread-stubs0-dev \
can-utils
# 对于 aarch64,安装交叉编译工具链或 QEMU 用户模式
if [ "${{ matrix.arch }}" = "aarch64" ]; then
sudo apt-get install -y \
gcc-aarch64-linux-gnu \
g++-aarch64-linux-gnu \
qemu-user-static \
binfmt-support
fi
- name: 安装 ROS2 (如果需要)
if: matrix.use_ros2 == true
run: |
sudo apt-get update
sudo apt-get install -y \
locales \
software-properties-common
sudo locale-gen en_US en_US.UTF-8
sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
export LANG=en_US.UTF-8
sudo apt-get install -y \
curl \
gnupg2 \
lsb-release
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -
sudo sh -c 'echo "deb http://packages.ros.org/ros2/ubuntu $(lsb_release -cs) main" > /etc/apt/sources.list.d/ros2-latest.list'
sudo apt-get update
sudo apt-get install -y \
ros-${{ matrix.ros2_distro }}-desktop \
python3-colcon-common-extensions \
python3-rosdep
- name: 初始化 ROS2 (如果需要)
if: matrix.use_ros2 == true
run: |
source /opt/ros/${{ matrix.ros2_distro }}/setup.bash
sudo rosdep init || true
rosdep update
- name: 验证工具链版本
run: |
echo "=== CMake 版本 ==="
cmake --version
echo ""
echo "=== 编译器版本 ==="
if [ "${{ matrix.arch }}" = "aarch64" ]; then
aarch64-linux-gnu-gcc --version
aarch64-linux-gnu-g++ --version
else
gcc --version
g++ --version
fi
echo ""
echo "=== 系统信息 ==="
uname -a
echo "目标架构: ${{ matrix.arch }}"
echo "实际架构: $(uname -m)"
if [ "${{ matrix.use_ros2 }}" = "true" ]; then
echo "=== ROS2 版本 ==="
source /opt/ros/${{ matrix.ros2_distro }}/setup.bash
ros2 --version || echo "ROS2 not available"
fi
- name: 创建构建目录
run: mkdir -p build
- name: 配置 CMake (非 ROS2)
if: matrix.use_ros2 == false
working-directory: build
run: |
if [ "${{ matrix.arch }}" = "aarch64" ]; then
# 使用交叉编译工具链
cmake ../src/linkerta \
-DCMAKE_BUILD_TYPE=${{ matrix.cmake_build_type }} \
-DCMAKE_SYSTEM_NAME=Linux \
-DCMAKE_SYSTEM_PROCESSOR=aarch64 \
-DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc \
-DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++ \
-DCMAKE_FIND_ROOT_PATH=/usr/aarch64-linux-gnu
else
cmake ../src/linkerta \
-DCMAKE_BUILD_TYPE=${{ matrix.cmake_build_type }}
fi
- name: 配置 ROS2 构建
if: matrix.use_ros2 == true
working-directory: ${{ github.workspace }}
run: |
source /opt/ros/${{ matrix.ros2_distro }}/setup.bash
colcon build \
--cmake-args \
-DCMAKE_BUILD_TYPE=${{ matrix.cmake_build_type }} \
-DUSE_ROS2=ON \
--packages-select linkerta
- name: 构建项目 (非 ROS2)
if: matrix.use_ros2 == false
working-directory: build
run: |
cmake --build . \
--config ${{ matrix.cmake_build_type }} \
--parallel $(nproc) \
--verbose
- name: 验证构建产物 (非 ROS2)
if: matrix.use_ros2 == false
working-directory: build
run: |
echo "=== 验证构建产物 ==="
if [ -f linkerta ]; then
echo "✓ linkerta 构建成功"
file linkerta
ls -lh linkerta
else
echo "✗ linkerta 未找到"
exit 1
fi
- name: 验证 ROS2 构建
if: matrix.use_ros2 == true
working-directory: ${{ github.workspace }}
run: |
source /opt/ros/${{ matrix.ros2_distro }}/setup.bash
source install/setup.bash
echo "=== 验证 ROS2 包 ==="
ros2 pkg list | grep linkerta || exit 1
echo "✓ linkerta ROS2 包构建成功"
code-quality:
name: Code Quality Checks
runs-on: ubuntu-latest
steps:
- name: 检出代码
uses: actions/checkout@v4
- name: 检查文件编码和换行符
run: |
echo "检查文件编码和换行符..."
# 检查是否有 Windows 换行符
CRLF_FILES=$(find . -type f \
\( -name "*.cpp" -o -name "*.h" -o -name "*.cmake" -o -name "CMakeLists.txt" -o -name "*.py" -o -name "*.yaml" -o -name "*.yml" \) \
-exec file {} \; | grep -i "CRLF" || true)
if [ -n "$CRLF_FILES" ]; then
echo "错误: 发现 Windows 换行符 (CRLF)"
echo ""
echo "以下文件需要修复:"
echo "$CRLF_FILES"
echo ""
echo "修复方法(Linux/macOS):"
echo " find . -type f \\( -name '*.cpp' -o -name '*.h' -o -name '*.cmake' -o -name 'CMakeLists.txt' \\) -exec sed -i 's/\\r$//' {} \\;"
exit 1
fi
echo "✓ 文件编码检查通过"
- name: 检查 CMake 语法
run: |
echo "检查 CMake 语法..."
cmake --version
# 尝试解析 CMakeLists.txt
mkdir -p /tmp/cmake-check
cd /tmp/cmake-check
cmake ${{ github.workspace }}/src/linkerta -Wno-dev > /dev/null 2>&1 || true
echo "✓ CMake 语法检查完成"
- name: 检查代码格式 (可选)
continue-on-error: true
run: |
echo "检查代码格式..."
if command -v clang-format &> /dev/null; then
sudo apt-get update
sudo apt-get install -y clang-format
fi
if command -v clang-format &> /dev/null; then
find src -name "*.cpp" -o -name "*.h" | head -5 | xargs clang-format --dry-run --Werror || echo "代码格式检查完成(部分文件可能需要格式化)"
else
echo "clang-format 不可用,跳过格式检查"
fi
summary:
name: CI Summary
runs-on: ubuntu-latest
needs: [build-and-test, code-quality]
if: always()
steps:
- name: 生成 CI 摘要
run: |
echo "## CI/CD 构建摘要" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### 构建状态" >> $GITHUB_STEP_SUMMARY
echo "- 构建和测试: ${{ needs.build-and-test.result }}" >> $GITHUB_STEP_SUMMARY
echo "- 代码质量: ${{ needs.code-quality.result }}" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### 详细信息" >> $GITHUB_STEP_SUMMARY
echo "查看上方的作业详情以获取更多信息。" >> $GITHUB_STEP_SUMMARY