name: CI Cross-Compile on: push: branches: [ main ] pull_request: workflow_dispatch: jobs: build: runs-on: ubuntu-latest strategy: fail-fast: false matrix: goos: [linux, windows, darwin] goarch: [amd64] steps: - name: Checkout uses: actions/checkout@v4 - name: Setup Go uses: actions/setup-go@v5 with: go-version: '1.22.x' - name: Cache Go build/mod uses: actions/cache@v4 with: path: | ~/.cache/go-build ~/go/pkg/mod key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}-${{ matrix.goos }}-${{ matrix.goarch }} restore-keys: | ${{ runner.os }}-go- - name: Download deps run: go mod download - name: Cross-compile (${{ matrix.goos }}/${{ matrix.goarch }}) env: CGO_ENABLED: "0" # 关闭 cgo,便于纯交叉构建 GOOS: ${{ matrix.goos }} GOARCH: ${{ matrix.goarch }} run: | mkdir -p dist/${GOOS}-${GOARCH} OUT="dist/${GOOS}-${GOARCH}/app" if [ "${GOOS}" = "windows" ]; then OUT="${OUT}.exe"; fi # -trimpath/-ldflags 让产物更小更可复现;按你的入口调整 ./cmd/app go build -trimpath -ldflags="-s -w" -o "${OUT}" ./cmd/app echo "Built ${OUT}" - name: Upload artifact uses: actions/upload-artifact@v4 with: name: app-${{ matrix.goos }}-${{ matrix.goarch }} path: dist/${{ matrix.goos }}-${{ matrix.goarch }}/* if-no-files-found: error