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

发布时间:2023-03-22 16:19:29 作者:iii
来源:亿速云 阅读:70

本文小编为大家详细介绍“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后怎么自动生成二进制文件”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 怎么在vscode中搭建一个golang开发环境
  2. Golang浮点数精度丢失问题扩展包的解决方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

golang release github

上一篇:java中如何优化大量if...else...

下一篇:Python没有main函数的原因是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》