Golang项目在github创建release后自动生成二进制文件的方法

作者:路多辛 时间:2024-05-22 10:17:50 

希望达到的效果

工具类的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 里面就会出现针对各个平台的二进制文件。

来源:https://blog.csdn.net/luduoyuan/article/details/129572520

标签:go,release,自动生成,二进制文件
0
投稿

猜你喜欢

  • 对变量赋值的理解--Pyton中让两个值互换的实现方法

    2022-07-05 02:51:56
  • Vue使用localStorage存储数据的方法

    2024-04-30 10:23:47
  • DreamWeaver制作会移动的广告条

    2008-02-03 11:34:00
  • 深入分析JavaScript 事件循环(Event Loop)

    2024-04-18 10:51:52
  • Python文本文件的合并操作方法代码实例

    2022-12-07 21:52:54
  • Python实现的质因式分解算法示例

    2021-12-16 23:10:05
  • Python创建系统目录的方法

    2023-11-22 11:52:47
  • sqlserver 数据库连接字符串中的可选项收集

    2024-01-16 17:47:31
  • Access 2002的三个实用技巧

    2007-10-22 12:22:00
  • JS中的forEach、$.each、map方法推荐

    2024-04-29 13:19:59
  • Python SELENIUM上传文件或图片实现过程

    2021-12-22 09:11:53
  • Python Tkinter Menu组件详解

    2021-07-24 06:51:18
  • MySQL触发器trigger的使用

    2024-01-23 15:08:23
  • Python自定义sorted排序实现方法详解

    2022-08-03 05:40:02
  • 详解Python flask的前后端交互

    2023-03-19 06:41:05
  • css学习笔记: 重置默认样式 css reset

    2009-07-19 14:30:00
  • python实现字符串和字典的转换

    2023-03-02 02:57:18
  • 我对SQL SERVER 存储过程见解

    2009-10-31 18:51:00
  • python 实现一个简单的线性回归案例

    2023-05-08 23:40:25
  • Go语言基础变量的声明及初始化示例详解

    2024-04-27 15:46:37
  • asp之家 网络编程 m.aspxhome.com