name: Build Multi-Platform Binaries on: push: paths: - '**.go' - 'go.mod' - 'go.sum' - 'web/**' - '.gitea/workflows/**' jobs: # --- 任务 1: 构建前端 --- build-frontend: runs-on: node steps: - name: Checkout code uses: actions/checkout@v4 # 缓存 node_modules - name: Cache Node modules uses: actions/cache@v3 with: path: web/node_modules key: node-modules-${{ hashFiles('web/package-lock.json') }} restore-keys: | node-modules- - name: Build Frontend run: | cd web npm ci --prefer-offline --no-audit npm run build - name: Upload Frontend Artifact uses: actions/upload-artifact@v3 with: name: frontend-dist path: web/dist retention-days: 1 # --- 任务 2: 构建多平台二进制文件 --- build-binaries: needs: build-frontend runs-on: golang strategy: fail-fast: false matrix: include: # Linux 平台 - { goos: linux, goarch: amd64, target: server, upx: true } - { goos: linux, goarch: amd64, target: client, upx: true } - { goos: linux, goarch: arm64, target: server, upx: true } - { goos: linux, goarch: arm64, target: client, upx: true } - { goos: linux, goarch: arm, goarm: 7, target: server, upx: true } - { goos: linux, goarch: arm, goarm: 7, target: client, upx: true } # Windows 平台 - { goos: windows, goarch: amd64, target: server, upx: true } - { goos: windows, goarch: amd64, target: client, upx: true } - { goos: windows, goarch: arm64, target: server, upx: false } # Darwin (macOS) 平台 - { goos: darwin, goarch: amd64, target: server, upx: false } - { goos: darwin, goarch: arm64, target: server, upx: false } steps: # 在 checkout 前安装依赖 - name: Install Dependencies run: | if command -v apk > /dev/null; then apk add --no-cache nodejs upx elif command -v apt-get > /dev/null; then apt-get update && apt-get install -y nodejs upx-ucl else echo "Unsupported package manager" && exit 1 fi - name: Checkout code uses: actions/checkout@v4 # 缓存 Go 模块 - name: Cache Go modules uses: actions/cache@v3 with: path: | ~/.cache/go-build ~/go/pkg/mod key: go-${{ runner.os }}-${{ hashFiles('go.sum') }} restore-keys: | go-${{ runner.os }}- # 下载依赖(只在缓存未命中时执行) - name: Download Go dependencies run: go mod download - name: Download Frontend Artifact uses: actions/download-artifact@v3 with: name: frontend-dist path: internal/server/app/dist - name: Build Binary env: GOOS: ${{ matrix.goos }} GOARCH: ${{ matrix.goarch }} GOARM: ${{ matrix.goarm }} CGO_ENABLED: 0 run: | # 处理文件名 ARM_VAL="" [ "${{ matrix.goarch }}" = "arm" ] && ARM_VAL="v${{ matrix.goarm }}" EXT="" [ "${{ matrix.goos }}" = "windows" ] && EXT=".exe" FILENAME="gotunnel-${{ matrix.target }}-${{ matrix.goos }}-${{ matrix.goarch }}${ARM_VAL}${EXT}" # 编译(使用 -trimpath 减小体积,利用缓存) go build -trimpath -ldflags="-s -w" -o "${FILENAME}" ./cmd/${{ matrix.target }} echo "CURRENT_FILENAME=${FILENAME}" >> $GITHUB_ENV - name: Run UPX Compression if: matrix.upx == true run: | upx --best --lzma "${{ env.CURRENT_FILENAME }}" || echo "UPX skipped for this platform" - name: Upload Binary uses: actions/upload-artifact@v3 with: name: ${{ env.CURRENT_FILENAME }} path: ${{ env.CURRENT_FILENAME }} retention-days: 7