|
| 1 | +name: Go Cross-Platform Build |
| 2 | + |
| 3 | +on: [push, pull_request] |
| 4 | + |
| 5 | +env: |
| 6 | + GO_VERSION: "1.24" |
| 7 | + OUTPUT_DIR: "dist" |
| 8 | + APP_SUFFIX: "" |
| 9 | + COMMIT_ID: "${{ github.sha }}" |
| 10 | + PR_PROMPT: "::warning:: Build artifact will not be uploaded due to the workflow is trigged by pull request." |
| 11 | + |
| 12 | +jobs: |
| 13 | + build: |
| 14 | + name: Build binary CI |
| 15 | + runs-on: ubuntu-latest |
| 16 | + strategy: |
| 17 | + matrix: |
| 18 | + # build and publish in parallel: linux/386, linux/amd64, windows/386, windows/amd64, darwin/amd64, darwin/arm64 |
| 19 | + goos: [linux, windows] |
| 20 | + goarch: ["386", amd64, arm, arm64] |
| 21 | + fail-fast: false |
| 22 | + steps: |
| 23 | + - uses: actions/checkout@v4 |
| 24 | + - name: Setup Go environment |
| 25 | + uses: actions/setup-go@v4 |
| 26 | + with: |
| 27 | + cache: true |
| 28 | + go-version: ${{ env.GO_VERSION }} |
| 29 | + - name: Set app name |
| 30 | + id: set-app-name |
| 31 | + run: | |
| 32 | + # 从go.mod获取模块名作为应用名 |
| 33 | + APP_NAME=$(grep '^module' go.mod | awk '{print $2}' | awk -F/ '{print $NF}') |
| 34 | + echo "APP_NAME=${APP_NAME}" >> $GITHUB_ENV |
| 35 | + echo "Using app name: ${APP_NAME}" |
| 36 | + - name: Build binary file |
| 37 | + env: |
| 38 | + GOOS: ${{ matrix.goos }} |
| 39 | + GOARCH: ${{ matrix.goarch }} |
| 40 | + IS_PR: ${{ !!github.head_ref }} |
| 41 | + run: | |
| 42 | + if [ $GOOS = "windows" ]; then export APP_SUFFIX="$APP_SUFFIX.exe"; fi |
| 43 | + if $IS_PR ; then echo $PR_PROMPT; fi |
| 44 | + export OUTPUT_NAME="${{ env.APP_NAME }}$APP_SUFFIX" |
| 45 | + export CGO_ENABLED=0 |
| 46 | + export LD_FLAGS="-w -s" |
| 47 | + go build -o "${{ env.OUTPUT_DIR }}/$OUTPUT_NAME" -trimpath -ldflags "$LD_FLAGS" . |
| 48 | + - name: Upload artifact |
| 49 | + uses: actions/upload-artifact@v4 |
| 50 | + if: ${{ !github.head_ref }} |
| 51 | + with: |
| 52 | + name: ${{ env.APP_NAME }}_${{ matrix.goos }}_${{ matrix.goarch }} |
| 53 | + path: ${{ env.OUTPUT_DIR }}/ |
0 commit comments