diff --git a/.gitea/workflows/build.yaml b/.gitea/workflows/build.yaml index f942fbf..7ed60a5 100644 --- a/.gitea/workflows/build.yaml +++ b/.gitea/workflows/build.yaml @@ -16,10 +16,19 @@ jobs: - 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 + npm ci --prefer-offline --no-audit npm run build - name: Upload Frontend Artifact @@ -34,15 +43,14 @@ jobs: needs: build-frontend runs-on: golang-latest strategy: - fail-fast: false # 即使某个平台失败,也继续构建其他平台 + 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 } # ARMv8 64-bit + - { goos: linux, goarch: arm64, target: server, upx: true } - { goos: linux, goarch: arm64, target: client, upx: true } - # 针对 ARMv8 (v8l) 32位模式,使用 GOARM=7 确保最大兼容性 - { goos: linux, goarch: arm, goarm: 7, target: server, upx: true } - { goos: linux, goarch: arm, goarm: 7, target: client, upx: true } @@ -54,10 +62,10 @@ jobs: # Darwin (macOS) 平台 - { goos: darwin, goarch: amd64, target: server, upx: false } - { goos: darwin, goarch: arm64, target: server, upx: false } - + steps: - # 关键步骤:在 checkout 之前安装 Node.js,否则 checkout@v4 会报错 - - name: Install Node.js & UPX + # 在 checkout 前安装依赖 + - name: Install Dependencies run: | if command -v apk > /dev/null; then apk add --no-cache nodejs upx @@ -66,16 +74,31 @@ jobs: 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 }} @@ -83,33 +106,28 @@ jobs: GOARM: ${{ matrix.goarm }} CGO_ENABLED: 0 run: | - # 处理文件名后缀 + # 处理文件名 ARM_VAL="" - if [ "${{ matrix.goarch }}" = "arm" ]; then - ARM_VAL="v${{ matrix.goarm }}" - fi + [ "${{ matrix.goarch }}" = "arm" ] && ARM_VAL="v${{ matrix.goarm }}" EXT="" - if [ "${{ matrix.goos }}" = "windows" ]; then - EXT=".exe" - fi + [ "${{ matrix.goos }}" = "windows" ] && EXT=".exe" FILENAME="gotunnel-${{ matrix.target }}-${{ matrix.goos }}-${{ matrix.goarch }}${ARM_VAL}${EXT}" - # 执行编译 - go build -ldflags="-s -w" -o "${FILENAME}" ./cmd/${{ matrix.target }} + # 编译(使用 -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) - upx -9 "${{ env.CURRENT_FILENAME }}" || echo "UPX skipped for this platform" - + 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 }} \ No newline at end of file + path: ${{ env.CURRENT_FILENAME }} + retention-days: 7 \ No newline at end of file