CI Build and Test #278
This file contains hidden or 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 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 |