Golang项目在Github创建Release后如何自动生成二进制文件?

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

希望达到的效果

工具类的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在运行:

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

责任编辑:姜华 来源: 今日头条
相关推荐

2024-02-01 09:04:12

2022-10-31 08:02:42

二进制计算乘法

2009-08-12 18:06:53

C#读取二进制文件

2009-12-16 10:49:42

Ruby操作二进制文件

2009-12-10 09:24:50

PHP函数fwrite

2023-09-18 23:50:25

二进制文件裁剪Layout

2020-05-06 09:51:37

二进制Linux命令行工具

2013-04-28 15:37:35

JBoss

2009-11-02 11:27:42

VB.NET二进制文件

2021-11-10 09:15:00

CPU01 二进制Linux

2022-11-18 10:17:01

2018-10-22 14:37:16

二进制数据存储

2009-02-27 09:37:33

Google二进制代码

2020-05-22 18:00:26

Go二进制文件编程语言

2023-12-26 15:10:00

处理二进制文件

2022-01-26 00:02:01

Go二进制元信息

2010-06-09 13:02:29

MySQL启用二进制日

2010-10-13 15:45:23

MySQL二进制日志

2017-04-11 10:48:53

JS二进制

2022-07-26 13:00:01

安全符号源代码
点赞
收藏

51CTO技术栈公众号