Golang项目在github创建release后怎么自动生成二进制文件

本文讲解"Golang项目在github创建release后如何自动生成二进制文件",希望能够解决相关问题。

希望达到的效果

工具类的Golang项目需要编译成二进制文件后在命令行中运行,所以希望在github里面创建一个新的release后能自动编译成针对各个平台的二进制文件,如下图所示:

Golang项目在github创建release后怎么自动生成二进制文件

实现方式

借助 GoReleaser 这款工具配合 github actions 可以很方便实现这种效果,下面讲解下具体实现方法。

首先需要在 Golang 项目的根目录创建 GoReleaser 配置文件 .goreleaser.yaml,内容如下:

# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com
before:
  hooks:
    # You may remove this if you don't use go modules.
    - go mod tidy
    # you may remove this if you don't need go generate
    - go generate ./...
builds:
  - env:
      - CGO_ENABLED=0
    goos:
      - linux
      - windows
      - darwin
 
archives:
  - format: tar.gz
    # this name template makes the OS and Arch compatible with the results of uname.
    name_template: >-
      {{ .ProjectName }}_
      {{- title .Os }}_
      {{- if eq .Arch "amd64" }}x86_64
      {{- else if eq .Arch "386" }}i386
      {{- else }}{{ .Arch }}{{ end }}
      {{- if .Arm }}v{{ .Arm }}{{ end }}
    # use zip for windows archives
    format_overrides:
    - goos: windows
      format: zip
checksum:
  name_template: 'checksums.txt'
snapshot:
  name_template: "{{ incpatch .Version }}-next"
changelog:
  sort: asc
  filters:
    exclude:
      - '^docs:'
      - '^test:'
 
# The lines beneath this are called `modelines`. See `:help modeline`
# Feel free to remove those if you don't want/use them.
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj

然后创建 github actions 配置文件, 在Golang项目的根目录创建 .github 文件夹,在这个文件夹里面创建 workflows 文件夹,在 workflows 文件夹里面创建 release.yaml 文件,内容如下:

name: goreleaser
 
on:
  push:
    # run only against tags
    tags:
      - '*'
 
permissions:
  contents: write
  # packages: write
  # issues: write
 
jobs:
  goreleaser:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - run: git fetch --force --tags
      - uses: actions/setup-go@v3
        with:
          go-version: '>=1.20.2'
          cache: true
      # More assembly might be required: Docker logins, GPG, etc. It all depends
      # on your needs.
      - uses: goreleaser/goreleaser-action@v4
        with:
          # either 'goreleaser' (default) or 'goreleaser-pro':
          distribution: goreleaser
          version: latest
          args: release --clean
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          # Your GoReleaser Pro key, if you are using the 'goreleaser-pro'
          # distribution:
          # GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}

这个文件的内容不需要做任何修改,提交代码并将代码 push 到 github 后,在 github 的 release 页面新建一个 release 后,在 Actions 页面就可以看到有一个workflow在运行:

Golang项目在github创建release后怎么自动生成二进制文件

等这个 workflow 运行完成以后,在 release 的 Assets 里面就会出现针对各个平台的二进制文件。

关于 "Golang项目在github创建release后如何自动生成二进制文件" 就介绍到此。希望多多支持编程宝库

本文讲解"Golang中struct{}和struct{}{}的区别是什么",希望能够解决相关问题。首先说下Golang中的结构体,结构体是由一系列具有相同类型或不同类型的数 ...