-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.gitlab-ci.yml
145 lines (133 loc) · 3.42 KB
/
.gitlab-ci.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
image: rust:latest
variables:
CARGO_HOME: $CI_PROJECT_DIR/.cargo
RUST_BACKTRACE: 1
CARGO_TERM_COLOR: always
# 定义缓存策略
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- .cargo
- target/
# 定义阶段
stages:
- test
- build
- package
- release
# 在运行任何作业之前的准备工作
before_script:
- apt-get update -yq
- apt-get install -y pkg-config libudev-dev gcc-aarch64-linux-gnu gcc-arm-linux-gnueabihf zip
- rustup default stable
- rustup component add rustfmt clippy
- cargo install cross
# 代码格式检查和 lint
lint:
stage: test
script:
- cargo fmt -- --check
- cargo clippy -- -D warnings
rules:
- if: $CI_COMMIT_TAG
when: never
- if: $CI_COMMIT_BRANCH
# 运行测试
test:
stage: test
script:
- cargo test --verbose
rules:
- if: $CI_COMMIT_TAG
when: never
- if: $CI_COMMIT_BRANCH
# Linux 构建
build-linux:
stage: build
parallel:
matrix:
- TARGET: [x86_64-unknown-linux-gnu, aarch64-unknown-linux-gnu, armv7-unknown-linux-gnueabihf]
script:
- rustup target add $TARGET
- |
if [ "$TARGET" = "x86_64-unknown-linux-gnu" ]; then
cargo build --release --target $TARGET
else
cross build --release --target $TARGET
fi
artifacts:
paths:
- target/$TARGET/release/hardware_monitor
expire_in: 1 week
# Windows 构建
build-windows:
stage: build
parallel:
matrix:
- TARGET: [x86_64-pc-windows-msvc, i686-pc-windows-msvc]
script:
- rustup target add $TARGET
- cargo build --release --target $TARGET
artifacts:
paths:
- target/$TARGET/release/hardware_monitor.exe
expire_in: 1 week
# 打包构建产物
package:
stage: package
script:
- |
mkdir -p release
for target in target/*/release/hardware_monitor*; do
if [[ -f "$target" ]]; then
filename=$(basename "$target")
target_arch=$(echo "$target" | cut -d'/' -f2)
if [[ "$filename" == *.exe ]]; then
zip -j "release/hardware_monitor-${target_arch}.zip" "$target"
sha256sum "$target" > "release/hardware_monitor-${target_arch}.sha256"
else
tar czf "release/hardware_monitor-${target_arch}.tar.gz" -C "$(dirname "$target")" "$filename"
sha256sum "$target" > "release/hardware_monitor-${target_arch}.sha256"
fi
fi
done
artifacts:
paths:
- release/*
expire_in: 1 week
dependencies:
- build-linux
- build-windows
# 发布版本
release:
stage: release
script:
- |
if [[ $CI_COMMIT_TAG =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Creating release for tag $CI_COMMIT_TAG"
# 这里可以使用 GitLab API 创建发布
curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file release/* "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/hardware_monitor/${CI_COMMIT_TAG#v}/"
fi
rules:
- if: $CI_COMMIT_TAG =~ /^v\d+\.\d+\.\d+$/
dependencies:
- package
# 部署到测试环境
deploy_staging:
stage: .post
environment: staging
script:
- echo "Deploying to staging server"
# 这里添加部署脚本
rules:
- if: $CI_COMMIT_BRANCH == "main"
# 部署到生产环境
deploy_production:
stage: .post
environment: production
script:
- echo "Deploying to production server"
# 这里添加部署脚本
rules:
- if: $CI_COMMIT_TAG =~ /^v\d+\.\d+\.\d+$/
when: manual