chore: update ci #5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: CI | |
on: | |
push: | |
branches: [ "main" ] | |
tags: | |
- 'v*' # 当推送版本标签时触发,如 v1.0.0 | |
pull_request: | |
branches: [ "main" ] | |
permissions: | |
contents: write # 这给予了创建 release 的权限 | |
packages: write | |
pull-requests: write | |
env: | |
CARGO_TERM_COLOR: always | |
# 设置二进制文件名称 | |
BINARY_NAME: system_monitor | |
jobs: | |
build: | |
name: Build ${{ matrix.target }} | |
strategy: | |
matrix: | |
include: | |
# Linux 构建 | |
- target: x86_64-unknown-linux-gnu | |
os: ubuntu-latest | |
# macOS Intel 构建 | |
- target: x86_64-apple-darwin | |
os: macos-latest | |
# macOS ARM (M1/M2) 构建 | |
- target: aarch64-apple-darwin | |
os: macos-latest | |
# Windows 构建 | |
- target: x86_64-pc-windows-gnu | |
os: windows-latest | |
runs-on: ${{ matrix.os }} | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Install Rust | |
uses: dtolnay/rust-toolchain@stable | |
with: | |
targets: ${{ matrix.target }} | |
- name: Build Binary | |
run: | | |
cargo build --verbose --release --target ${{ matrix.target }} | |
# 创建发布包 | |
- name: Package Binary | |
shell: bash | |
run: | | |
# 创建发布目录 | |
mkdir -p releases | |
# 根据目标平台设置二进制文件扩展名 | |
if [[ "${{ matrix.target }}" == *"windows"* ]]; then | |
BINARY_SUFFIX=".exe" | |
else | |
BINARY_SUFFIX="" | |
fi | |
# 复制二进制文件到发布目录 | |
cp "target/${{ matrix.target }}/release/$BINARY_NAME$BINARY_SUFFIX" \ | |
"releases/$BINARY_NAME-${{ matrix.target }}$BINARY_SUFFIX" | |
# 进入发布目录创建压缩包 | |
cd releases | |
if [[ "${{ matrix.os }}" == "windows-latest" ]]; then | |
7z a "$BINARY_NAME-${{ matrix.target }}.zip" "$BINARY_NAME-${{ matrix.target }}$BINARY_SUFFIX" | |
else | |
tar czf "$BINARY_NAME-${{ matrix.target }}.tar.gz" "$BINARY_NAME-${{ matrix.target }}$BINARY_SUFFIX" | |
fi | |
# 上传构建产物 | |
- name: Upload artifacts | |
uses: actions/upload-artifact@v3 | |
with: | |
name: ${{ matrix.target }} | |
path: releases/* | |
# 当推送标签时创建 GitHub Release | |
- name: Create Release | |
if: startsWith(github.ref, 'refs/tags/') | |
uses: softprops/action-gh-release@v1 | |
with: | |
files: releases/* | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
# 运行测试 | |
test: | |
name: Run Tests | |
strategy: | |
matrix: | |
os: [ubuntu-latest, macos-latest, windows-latest] | |
runs-on: ${{ matrix.os }} | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Install Rust | |
uses: dtolnay/rust-toolchain@stable | |
- name: Run tests | |
run: cargo test --verbose |