diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..4c7208a --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,145 @@ +name: Build Multi-Platform Binaries +on: + push: + paths: + - '**.go' + - 'go.mod' + - 'go.sum' + - 'web/**' + - '.github/workflows/**' + +jobs: + build-frontend: + runs-on: node + steps: + - name: Checkout code + uses: actions/checkout@v4 + + # 直接挂载宿主机的缓存目录 + - name: Build Frontend + run: | + cd web + # 使用共享的 node_modules 缓存 + if [ -d /data/cache/node_modules_cache ]; then + echo "Restoring node_modules from cache..." + cp -r /data/cache/node_modules_cache/node_modules . 2>/dev/null || true + fi + + npm ci --prefer-offline --no-audit + + # 保存缓存 + mkdir -p /data/cache/node_modules_cache + cp -r node_modules /data/cache/node_modules_cache/ 2>/dev/null || true + + npm run build + + - name: Upload Frontend Artifact + uses: actions/upload-artifact@v3 + with: + name: frontend-dist + path: web/dist + retention-days: 1 + + build-binaries: + needs: build-frontend + runs-on: golang + strategy: + fail-fast: false + matrix: + include: + - { 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 } + - { goos: windows, goarch: amd64, target: server, upx: true } + - { goos: windows, goarch: amd64, target: client, upx: true } + - { goos: windows, goarch: arm64, target: server, upx: false } + - { goos: darwin, goarch: amd64, target: server, upx: false } + - { goos: darwin, goarch: arm64, target: server, upx: false } + + steps: + - 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: Setup Go Cache + run: | + # 创建缓存目录 + mkdir -p /data/cache/go-pkg-mod + mkdir -p /data/cache/go-build-cache + mkdir -p ~/go/pkg/mod + mkdir -p ~/.cache/go-build + + # 恢复缓存(使用硬链接以节省空间和时间) + if [ -d /data/cache/go-pkg-mod ] && [ "$(ls -A /data/cache/go-pkg-mod)" ]; then + echo "Restoring Go pkg cache..." + cp -al /data/cache/go-pkg-mod/* ~/go/pkg/mod/ 2>/dev/null || true + fi + + if [ -d /data/cache/go-build-cache ] && [ "$(ls -A /data/cache/go-build-cache)" ]; then + echo "Restoring Go build cache..." + cp -al /data/cache/go-build-cache/* ~/.cache/go-build/ 2>/dev/null || true + fi + + - 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}" + + go build -trimpath -ldflags="-s -w" -o "${FILENAME}" ./cmd/${{ matrix.target }} + + echo "CURRENT_FILENAME=${FILENAME}" >> $GITHUB_ENV + + # 保存 Go 缓存(异步,不阻塞主流程) + - name: Save Go Cache + if: always() + run: | + # 只在缓存有更新时保存(使用 rsync 可以更高效) + rsync -a --delete ~/go/pkg/mod/ /data/cache/go-pkg-mod/ 2>/dev/null || \ + cp -r ~/go/pkg/mod/* /data/cache/go-pkg-mod/ 2>/dev/null || true + + rsync -a --delete ~/.cache/go-build/ /data/cache/go-build-cache/ 2>/dev/null || \ + cp -r ~/.cache/go-build/* /data/cache/go-build-cache/ 2>/dev/null || true + + - 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