记录下使用 github-action 打包 golang 多平台部署包脚本;

自定义执行步骤

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
name: Build and Release

# 触发条件 - 触发条件为 push 到 master 分支,并且 tag 为 v*.*.*
on:
  push:
    tags:
      - 'v*.*.*'

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        goos: [linux]
        goarch: [amd64, arm64]
    steps:
    - name: Checkout code
      uses: actions/checkout@v4

    - name: Set up Go
      uses: actions/setup-go@v5
      with:
        go-version: ${{ env.GO_VERSION || '1.21' }}  # 动态指定 Go 版本

    - name: Build
      run: |
        GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -o loginfopush-${{ matrix.goos }}-${{ github.ref_name }}-${{ matrix.goarch }}

    - name: Upload artifact
      uses: actions/upload-artifact@v4
      with:
        name: loginfopush-${{ matrix.goos }}-${{ github.ref_name }}-${{ matrix.goarch }}
        path: loginfopush-${{ matrix.goos }}-${{ github.ref_name }}-${{ matrix.goarch }}

  release:
    needs: build
    runs-on: ubuntu-latest
    steps:
    - name: Download artifacts
      uses: actions/download-artifact@v4
      with:
        path: ./artifacts
        pattern: '*'  # 将所有构件下载到同一目录

    - name: List artifacts
      run: ls -R ./artifacts

    - name: Create Release and Upload Assets
      if: success()
      uses: softprops/action-gh-release@v1
      env: 
        # 能操作仓库的 token
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ github.ref_name }}
        files: ./artifacts/**/*  # 递归匹配所有子目录中的文件

使用第三方脚本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
name: build

on:
  release:
    types: [created] # 表示在创建新的 Release 时触发

permissions:
    contents: write
    packages: write

jobs:
  releases-matrix:
    name: Release Go Binary
    runs-on: ubuntu-latest
    strategy:
      matrix:
        # build and publish in parallel: linux/386, linux/amd64, linux/arm64, windows/386, windows/amd64, darwin/amd64, darwin/arm64
        goos: [linux, darwin]
        goarch: [amd64, arm64]
        exclude:
          - goarch: arm64
            goos: darwin
    steps:
    - uses: actions/checkout@v4
    - uses: wangyoucao577/[email protected]
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        goos: ${{ matrix.goos }}
        goarch: ${{ matrix.goarch }}
        goversion: 1.17 # 可以指定编译使用的 Golang 版本
        binary_name: "二进制名称"
        md5sum: 'FALSE'
        compress_assets: 'FALSE'  # 禁止压缩