Github使用Action自动编译发布go项目

准备

  • GitHub personal access 部署的 Action 需要有对应仓库的权限,因此提前设置好 GitHub personal access(个人访问令牌)。生成教程可以看 GitHub 官方的帮助文档:创建用于命令行的个人访问令牌
  1. 选择账户设置的setting image.png
  2. 选择Developer settings->Personal access tokens->Generate new token image.png image.png 3.生成新的token
    • 设置token名GOACTION
    • 授予repo权限 image.png

编写action的yml文件

编写.github/workflows/main.yml文件

编写触发条件

项目目前有且只有一个main分支,设置action的触发条件为pushmain分支后,对应yaml内容如下:

1
2
3
4
5
# 触发条件:在 push 到 main 分支后
on:
  push:
    branches:
      - main

设置jobs和运行环境

设置jobs名字为:buildAndDeploy(这个可以根据自己的喜好起),通过runs-on设置运行环境为:ubuntu-latest

1
2
3
jobs:
  buildAndDeploy:
    runs-on:  ubuntu-latest

设置go交叉编译

针对 arm64、amd64 和 Linux、Darwin的组合进行交叉编译:

  • 使用checkout@v3获取代码
  • setup-go@v3 设置 golang 环境,并在 with 字句中指定了go版本需大于1.16.0
  • make 这里使用了 Matrix 定义的变量,循环构建每个组合; 对应yaml内容如下:
 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
    # 交叉构建,多平台交叉编译
    strategy:
      matrix:
        include:
          - goarch: amd64
            goos: linux
          - goarch: amd64
            goos: darwin
          - goarch: arm64
            goos: linux
          - goarch: arm64
            goos: darwin
    # 使用checkout
    steps:
    - uses: actions/checkout@v3
    - name: Set up Go
      uses: actions/setup-go@v3
      with: 
        go-version: '>=1.16.0'
    - run: go version
    - name: make
      run: make
      env:
        GOOS: ${{ matrix.goos }}
        GOARCH: ${{ matrix.goarch }} 

自动发布编译后的二进制

使用upload-release-asset进行二进制发布:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
    - name: Packaging...
      run: tar czf nodeUpgrade-${{ matrix.goos }}-${{ matrix.goarch }}.tgz nodeUpgrade
    - name: Uploading assets...
    #   if: ${{ !env.ACT }}
      id: upload-release-asset 
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GOACTION }}
      with:
        upload_url: ${{ github.event.release.upload_url }}
        asset_path: ./nodeUpgrade-${{ matrix.goos }}-${{ matrix.goarch }}.tgz
        asset_name: nodeUpgrade-${{ matrix.goos }}-${{ matrix.goarch }}.tgz
        asset_content_type: application/gzip

完整的yaml文件

 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
57
58
59
60
61
62
63
64
name: Publish Go Binary

# 触发条件:在 push 到 main 分支后
on:
  push:
    branches:
      - main
env:
  GITHUB_TOKEN: ${{ secrets.GOACTION }}      
jobs:
  buildAndDeploy:
    runs-on:  ubuntu-latest
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    # 交叉构建,多平台交叉编译
    strategy:
      matrix:
        include:
          - goarch: amd64
            goos: linux
          - goarch: amd64
            goos: darwin
          - goarch: arm64
            goos: linux
          - goarch: arm64
            goos: darwin
    # 使用checkout
    steps:
    - uses: actions/checkout@v3
    - name: Set up Go
      uses: actions/setup-go@v3
      with: 
        go-version: '>=1.16.0'
    - run: go version
    - name: make
      run: make
      env:
        GOOS: ${{ matrix.goos }}
        GOARCH: ${{ matrix.goarch }}            
    - name: Packaging...
      run: tar czf nodeUpgrade-${{ matrix.goos }}-${{ matrix.goarch }}.tgz nodeUpgrade
    - name: Get version
      id: version
      run: echo "::set-output name=version::$(git rev-parse --short HEAD)"
    - name: Gets latest created release info
      uses: "marvinpinto/action-automatic-releases@latest"
      with:
          repo_token: "${{ secrets.GITHUB_TOKEN }}"
          automatic_release_tag:  ${{ steps.version.outputs.version }}
          prerelease: true
          title: "Development Build"
          files: |
            ./nodeUpgrade-${{ matrix.goos }}-${{ matrix.goarch }}.tgz              
#     - name: Uploading assets...
#       # if: ${{ !env.ACT }}
#       # id: upload-release-asset 
#       uses: actions/upload-release-asset@v1
#       env:
#         GITHUB_TOKEN: ${{ secrets.GOACTION }}
#       with:
#         upload_url: ${{ steps.latest_release_info.outputs.upload_url }}
#         asset_path: ./nodeUpgrade-${{ matrix.goos }}-${{ matrix.goarch }}.tgz
#         asset_name: nodeUpgrade-${{ matrix.goos }}-${{ matrix.goarch }}.tgz
#         asset_content_type: application/gzip

Reference: https://cloud.tencent.com/developer/article/1884133

Built with Hugo
主题 StackJimmy 设计