-
Notifications
You must be signed in to change notification settings - Fork 191
256 lines (223 loc) · 9.05 KB
/
Copy pathci-test.yml
File metadata and controls
256 lines (223 loc) · 9.05 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
name: CI Build and Test
on:
#push:
# branches:
# - main
pull_request:
branches:
- main
schedule:
# 每3天的凌晨2点运行一次清理任务
- cron: '0 2 */3 * *'
workflow_dispatch: # 允许手动触发
permissions:
contents: read
# 同一分支上新提交进来时,取消还在跑的旧 run,避免卡住的 run 长期占坑
concurrency:
group: ci-test-${{ github.ref }}
cancel-in-progress: true
jobs:
# Linux 构建和测试
build-and-test-linux:
runs-on: ubuntu-latest
timeout-minutes: 45
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v5
with:
# 与 Win7 专用版同一版本,且不低于 go.mod 里的 toolchain,避免构建时再下载一次工具链
go-version: '1.25.11'
check-latest: false
- name: Install dependencies
timeout-minutes: 15
run: bash .github/scripts/apt-install.sh build-essential musl-tools
- name: Resolve version (align with latest GitHub Release)
id: ver
env:
GH_TOKEN: ${{ github.token }}
run: |
# 版本号对齐 GitHub Releases 上的最新版本,避免测试者下载后被
# "先升正式版、再升测试版" 连续推着升级,导致测不到本次构建的代码。
# 注意:beta 也是以正式 release 发布的(goreleaser prerelease:false),
# 所以 /releases/latest 拿到的就是 releases 页面顶部那个 tag。
LATEST=$(curl -sSL -H "Authorization: Bearer $GH_TOKEN" \
https://api.github.com/repos/samwafgo/SamWaf/releases/latest | jq -r '.tag_name // empty') || true
# 兜底1:本地 git tag(checkout 已 fetch-depth:0)
if [ -z "$LATEST" ]; then LATEST=$(git describe --tags --abbrev=0 2>/dev/null || true); fi
# 兜底2:拿不到就用默认值,不让构建挂掉
if [ -z "$LATEST" ]; then LATEST="v1.0.0"; fi
echo "base=$LATEST" >> $GITHUB_OUTPUT
# 版本名带 citest- 前缀作为"仅供单独测试"的标记(只用于展示,不参与版本比较)
echo "vname=citest-$(date +'%Y%m%d')-run${{ github.run_number }}-$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
- name: Download SamWafWeb
run: |
curl -fSL --retry 3 --retry-all-errors --connect-timeout 15 --max-time 300 https://github.com/samwafgo/SamWafWeb/releases/latest/download/dist.tar.gz -o dist.tar.gz
tar -zxvf dist.tar.gz
rm -rf public/dist
mv -f dist public
rm -rf dist.tar.gz
- name: Go mod tidy
run: go mod tidy
- name: Build Linux binary
env:
CGO_ENABLED: 1
CC: x86_64-linux-musl-gcc
CXX: x86_64-linux-musl-g++
CGO_CFLAGS: -Wno-unused-variable -D_LARGEFILE64_SOURCE
GOAMD64: v1
run: |
go build \
-ldflags="-X SamWaf/global.GWAF_RELEASE=true -X SamWaf/global.GWAF_RUNTIME_WIN7_VERSION=false -X SamWaf/global.GWAF_RELEASE_VERSION_NAME=${{ steps.ver.outputs.vname }} -X SamWaf/global.GWAF_RELEASE_VERSION=${{ steps.ver.outputs.base }} -s -w -extldflags '-static'" \
-o ./SamWafLinux64 ./cmd/samwaf/main.go
- name: Test Linux binary
timeout-minutes: 2
run: |
echo "Starting SamWaf service..."
# 在后台启动服务
./SamWafLinux64 &
SERVICE_PID=$!
echo "Service started with PID: $SERVICE_PID"
# 等待服务启动(最多等待30秒)
echo "Waiting for service to start..."
for i in {1..30}; do
if curl -f http://localhost:26666 2>/dev/null; then
echo "Service is up and running!"
break
fi
if [ $i -eq 30 ]; then
echo "Service failed to start within 30 seconds"
kill $SERVICE_PID || true
exit 1
fi
echo "Attempt $i/30: Service not ready yet, waiting..."
sleep 1
done
# 执行健康检查
echo "Performing health check..."
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:26666)
echo "HTTP Status Code: $HTTP_CODE"
if [ "$HTTP_CODE" -ge 200 ] && [ "$HTTP_CODE" -lt 500 ]; then
echo "Health check passed!"
else
echo "Health check failed with status code: $HTTP_CODE"
kill $SERVICE_PID || true
exit 1
fi
# 停止服务
echo "Stopping service..."
kill $SERVICE_PID || true
wait $SERVICE_PID 2>/dev/null || true
echo "Service stopped successfully"
- name: Write CI test build marker
env:
VERSION: ${{ steps.ver.outputs.base }}
VERSION_NAME: ${{ steps.ver.outputs.vname }}
run: bash .github/scripts/write-citest-marker.sh
- name: Upload Linux binary
uses: actions/upload-artifact@v4
with:
name: SamWafLinux64-citest-run${{ github.run_number }}-${{ github.sha }}
path: |
./SamWafLinux64
./README-CITEST.txt
retention-days: 3
# Windows 构建和基本测试
build-and-test-windows:
runs-on: ubuntu-latest
timeout-minutes: 45
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v5
with:
# 与 Win7 专用版同一版本,且不低于 go.mod 里的 toolchain,避免构建时再下载一次工具链
go-version: '1.25.11'
check-latest: false
- name: Install dependencies for Windows cross-compilation
timeout-minutes: 15
run: bash .github/scripts/apt-install.sh gcc-mingw-w64-x86-64 gcc-mingw-w64-i686
- name: Resolve version (align with latest GitHub Release)
id: ver
env:
GH_TOKEN: ${{ github.token }}
run: |
# 同 Linux 任务:版本号对齐 GitHub Releases 上的最新版本,避免连续升级
LATEST=$(curl -sSL -H "Authorization: Bearer $GH_TOKEN" \
https://api.github.com/repos/samwafgo/SamWaf/releases/latest | jq -r '.tag_name // empty') || true
if [ -z "$LATEST" ]; then LATEST=$(git describe --tags --abbrev=0 2>/dev/null || true); fi
if [ -z "$LATEST" ]; then LATEST="v1.0.0"; fi
echo "base=$LATEST" >> $GITHUB_OUTPUT
echo "vname=citest-$(date +'%Y%m%d')-run${{ github.run_number }}-$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
- name: Download SamWafWeb
run: |
curl -fSL --retry 3 --retry-all-errors --connect-timeout 15 --max-time 300 https://github.com/samwafgo/SamWafWeb/releases/latest/download/dist.tar.gz -o dist.tar.gz
tar -zxvf dist.tar.gz
rm -rf public/dist
mv -f dist public
rm -rf dist.tar.gz
- name: Go mod tidy
run: go mod tidy
- name: Build Windows binary
env:
GOOS: windows
GOARCH: amd64
CGO_ENABLED: 1
CC: x86_64-w64-mingw32-gcc
CXX: x86_64-w64-mingw32-g++
CGO_CFLAGS: -Wno-unused-variable
run: |
go build \
-ldflags="-X SamWaf/global.GWAF_RELEASE=true -X SamWaf/global.GWAF_RUNTIME_WIN7_VERSION=false -X SamWaf/global.GWAF_RELEASE_VERSION_NAME=${{ steps.ver.outputs.vname }} -X SamWaf/global.GWAF_RELEASE_VERSION=${{ steps.ver.outputs.base }} -s -w -extldflags '-static'" \
-o ./SamWaf64.exe ./cmd/samwaf/main.go
- name: Check Windows binary
run: |
echo "Windows binary built successfully:"
ls -lh SamWaf64.exe
file SamWaf64.exe
- name: Write CI test build marker
env:
VERSION: ${{ steps.ver.outputs.base }}
VERSION_NAME: ${{ steps.ver.outputs.vname }}
run: bash .github/scripts/write-citest-marker.sh
- name: Upload Windows binary
uses: actions/upload-artifact@v4
with:
name: SamWaf64-citest-run${{ github.run_number }}-${{ github.sha }}
path: |
./SamWaf64.exe
./README-CITEST.txt
retention-days: 3
# # 代码质量检查
# code-quality:
# runs-on: ubuntu-latest
# steps:
# - name: Checkout
# uses: actions/checkout@v4
# - name: Set up Go
# uses: actions/setup-go@v5
# with:
# go-version: '1.25.11'
# - name: Go mod verify
# run: go mod verify
# - name: Go vet
# run: go vet ./...
# - name: Install staticcheck
# run: go install honnef.co/go/tools/cmd/staticcheck@latest
# - name: Run staticcheck
# run: staticcheck ./...
# continue-on-error: true
# - name: Check formatting
# run: |
# if [ -n "$(gofmt -s -l .)" ]; then
# echo "以下文件需要格式化:"
# gofmt -s -l .
# exit 1
# fi
# continue-on-error: true