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

免费教程   2024年04月21日 20:54  

本文小编为大家详细介绍“Golang项目在github创建release后怎么自动生成二进制文件”,内容详细,步骤清晰,细节处理妥当,希望这篇“Golang项目在github创建release后怎么自动生成二进制文件”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

希望达到的效果

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

实现方式

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

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

#Thisisanexample.goreleaser.ymlfilewithsomesensibledefaults.#Makesuretocheckthedocumentationathttps://goreleaser.combefore:hooks:#Youmayremovethisifyoudon'tusegomodules.-gomodtidy#youmayremovethisifyoudon'tneedgogenerate-gogenerate./...builds:-env:-CGO_ENABLED=0goos:-linux-windows-darwinarchives:-format:tar.gz#thisnametemplatemakestheOSandArchcompatiblewiththeresultsofuname.name_template:>-{{.ProjectName}}_{{-title.Os}}_{{-ifeq.Arch"amd64"}}x86_64{{-elseifeq.Arch"386"}}i386{{-else}}{{.Arch}}{{end}}{{-if.Arm}}v{{.Arm}}{{end}}#usezipforwindowsarchivesformat_overrides:-goos:windowsformat:zipchecksum:name_template:'checksums.txt'snapshot:name_template:"{{incpatch.Version}}-next"changelog:sort:ascfilters:exclude:-'^docs:'-'^test:'#Thelinesbeneaththisarecalled`modelines`.See`:helpmodeline`#Feelfreetoremovethoseifyoudon'twant/usethem.#yaml-language-server:$schema=https://goreleaser.com/static/schema.json#vim:setts=2sw=2tw=0fo=cnqoj

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

name:goreleaseron:push:#runonlyagainsttagstags:-'*'permissions:contents:write#packages:write#issues:writejobs:goreleaser:runs-on:ubuntu-lateststeps:-uses:actions/checkout@v3with:fetch-depth:0-run:gitfetch--force--tags-uses:actions/setup-go@v3with:go-version:'>=1.20.2'cache:true#Moreassemblymightberequired:Dockerlogins,GPG,etc.Italldepends#onyourneeds.-uses:goreleaser/goreleaser-action@v4with:#either'goreleaser'(default)or'goreleaser-pro':distribution:goreleaserversion:latestargs:release--cleanenv:GITHUB_TOKEN:${{secrets.GITHUB_TOKEN}}#YourGoReleaserProkey,ifyouareusingthe'goreleaser-pro'#distribution:#GORELEASER_KEY:${{secrets.GORELEASER_KEY}}

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

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

读到这里,这篇“Golang项目在github创建release后怎么自动生成二进制文件”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注行业资讯频道。

域名注册
购买VPS主机

您或许对下面这些文章有兴趣:                    本月吐槽辛苦排行榜

看贴要回贴有N种理由!看帖不回贴的后果你懂得的!


评论内容 (*必填):
(Ctrl + Enter提交)   

部落快速搜索栏

各类专题梳理

网站导航栏

X
返回顶部