利用 Github Action 和GoReleaser全自动编译发布多平台Release包

摘要

使用github aciton 可以帮我们自动执行某些操作
那么我们就可以使用他来进行自动编译我们的程序

程序环境

  Go: v1.18

  需要编译平台: Macos,Linux,Windows

创建Action

  1. 找到Action菜单

file

  1. 搜索go 并且点击Configure 进行设置工作流
    file

  默认配置如下

# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on: # 工作流什么时候开始
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs: # 具体工作执行流程

  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3

    - name: Set up Go
      uses: actions/setup-go@v3
      with:
        go-version: 1.18

    - name: Build
      run: go build -v ./...

    - name: Test
      run: go test -v ./...

  修改为如下配置,修改后点击一下保存

# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Build Release

on:
  push:
    # Pattern matched against refs/tags
    tags:      
      - '*'           # 创建所有tag都运行打包 v* 则 v1.0这种也行

jobs:

  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3

    - name: Set up Go
      uses: actions/setup-go@v3
      with:
        go-version: 1.18
        cache: true

    - name: Build
      run: go build -v ./...

      # 使用 goreleaser 进行生成多平台代码并且上传到github release进行发布
    - name: Create release on GitHub
      uses: docker://goreleaser/goreleaser:latest
      with:
        args: release
      env:
        GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

Releaser 配置: .goreleaser.yaml

  此文件配置于项目根目录

  具体帮助文档可以查询官网 https://goreleaser.com/quick-start

  注:CGO开启的话感觉goreleaser 并不是这么好用了~毕竟需要多环境进行编译

project_name: rustdesk-api-server
builds:
  - env: [ CGO_ENABLED=0 ]
    goos: # 编译平台列表 
      - linux
      - windows
      - darwin
checksum:
  name_template: 'checksums.txt'
archives:
  - replacements:
      linux: Linux
      windows: Windows
      darwin: MacOs
      386: i386
      amd64: x86_64
    format_overrides:
      - goos: windows
        format: zip
    files: # 添加到压缩包内的文件列表
      - README.md
      - img.png
      - conf/app.conf

测试

  此时配置已配置完成,则可以开始操作

git tag v1.2
git push --tags

  此时action中就可以看到工作流已经开始工作了

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享
评论抢沙发

请登录后发表评论