Skip to content

Commit 3e4e3a6

Browse files
committed
feat: Add a CI workflow to support testing and deployment
1 parent 4420d9a commit 3e4e3a6

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

.github/workflows/ci.yml

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main]
6+
tags: ["v*"]
7+
pull_request:
8+
branches: [main]
9+
10+
env:
11+
CARGO_TERM_COLOR: always
12+
13+
jobs:
14+
# 对所有推送和 PR 运行测试
15+
test:
16+
name: Test (${{ matrix.os }})
17+
runs-on: ${{ matrix.os }}
18+
strategy:
19+
fail-fast: false
20+
matrix:
21+
os: [ubuntu-latest, macos-latest, windows-latest]
22+
steps:
23+
- uses: actions/checkout@v4
24+
25+
- name: Install Rust toolchain
26+
uses: dtolnay/rust-action@nightly
27+
28+
- name: Install Linux dependencies
29+
if: runner.os == 'Linux'
30+
run: |
31+
sudo apt-get update
32+
sudo apt-get install -y libxcb1-dev libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev libxkbcommon-dev
33+
34+
- name: Cache cargo registry
35+
uses: actions/cache@v4
36+
with:
37+
path: |
38+
~/.cargo/registry
39+
~/.cargo/git
40+
target
41+
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
42+
restore-keys: |
43+
${{ runner.os }}-cargo-
44+
45+
- name: Run tests
46+
run: cargo test --verbose
47+
48+
# 仅在 tag 推送时构建并发布
49+
release:
50+
name: Build & Release (${{ matrix.target }})
51+
needs: test
52+
if: startsWith(github.ref, 'refs/tags/v')
53+
runs-on: ${{ matrix.os }}
54+
strategy:
55+
fail-fast: false
56+
matrix:
57+
include:
58+
# Linux
59+
- target: x86_64-unknown-linux-gnu
60+
os: ubuntu-latest
61+
archive: tar.gz
62+
# macOS
63+
- target: x86_64-apple-darwin
64+
os: macos-13 # Intel runner
65+
archive: tar.gz
66+
- target: aarch64-apple-darwin
67+
os: macos-latest # Apple Silicon runner
68+
archive: tar.gz
69+
# Windows
70+
- target: x86_64-pc-windows-msvc
71+
os: windows-latest
72+
archive: zip
73+
74+
steps:
75+
- uses: actions/checkout@v4
76+
77+
- name: Install Rust toolchain
78+
uses: dtolnay/rust-action@nightly
79+
with:
80+
targets: ${{ matrix.target }}
81+
82+
- name: Install Linux dependencies
83+
if: runner.os == 'Linux'
84+
run: |
85+
sudo apt-get update
86+
sudo apt-get install -y libxcb1-dev libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev libxkbcommon-dev
87+
88+
- name: Cache cargo registry
89+
uses: actions/cache@v4
90+
with:
91+
path: |
92+
~/.cargo/registry
93+
~/.cargo/git
94+
target
95+
key: ${{ runner.os }}-${{ matrix.target }}-cargo-release-${{ hashFiles('**/Cargo.lock') }}
96+
restore-keys: |
97+
${{ runner.os }}-${{ matrix.target }}-cargo-release-
98+
99+
- name: Build release binary
100+
run: cargo build --release --target ${{ matrix.target }}
101+
102+
- name: Prepare artifacts (Unix)
103+
if: runner.os != 'Windows'
104+
run: |
105+
mkdir -p dist
106+
cp target/${{ matrix.target }}/release/omni-grip dist/
107+
cp -r res dist/
108+
cd dist
109+
tar czvf ../omni-grip-${{ github.ref_name }}-${{ matrix.target }}.tar.gz .
110+
111+
- name: Prepare artifacts (Windows)
112+
if: runner.os == 'Windows'
113+
run: |
114+
mkdir dist
115+
copy target\${{ matrix.target }}\release\omni-grip.exe dist\
116+
xcopy /E /I res dist\res
117+
cd dist
118+
7z a ..\omni-grip-${{ github.ref_name }}-${{ matrix.target }}.zip .
119+
120+
- name: Upload artifact
121+
uses: actions/upload-artifact@v4
122+
with:
123+
name: omni-grip-${{ matrix.target }}
124+
path: omni-grip-${{ github.ref_name }}-${{ matrix.target }}.*
125+
126+
# 发布到 GitHub Releases
127+
publish:
128+
name: Publish Release
129+
needs: release
130+
if: startsWith(github.ref, 'refs/tags/v')
131+
runs-on: ubuntu-latest
132+
permissions:
133+
contents: write
134+
steps:
135+
- name: Download all artifacts
136+
uses: actions/download-artifact@v4
137+
with:
138+
path: artifacts
139+
merge-multiple: true
140+
141+
- name: List artifacts
142+
run: ls -la artifacts/
143+
144+
- name: Create GitHub Release
145+
uses: softprops/action-gh-release@v2
146+
with:
147+
files: artifacts/*
148+
generate_release_notes: true
149+
draft: false
150+
prerelease: ${{ contains(github.ref_name, '-') }}
151+
env:
152+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)