如何将github开源项目发布
当我们的开源项目完成后,如果是一些可执行程序,就最好要将开源项目发布生成Releases,Releases包含各个系统架构的分发二进制,用户可以不用源码编译安装,而是直接下载安装,这样可以提高开源项目的用户体检。我们可以使用goreleaser
这个开源工具帮我们快速发布项目。本文将介绍goreleaser的基本使用。
安装
$ go install github.com/goreleaser/goreleaser@latest
- 1
使用
go install
命令安装需要安装Go,可以去https://go.dev/dl/ 下载安装即可
使用步骤
- 创建github项目并克隆到本地
git clone [email protected]:jagitch/go-downloader.git
- 1
- 初始化go项目
cd go-downloader
go mod init jagitch/go-downloader
touch main.go
- 1
- 2
- 3
- 编写应用
package main
func main() {
println("Hello, Welcome to Go downloader v0.0.0!")
}
- 1
- 2
- 3
- 4
- 5
- 初始化goreleaser
$ goreleaser init
• Generating .goreleaser.yaml file
• config created; please edit accordingly to your needs file=.goreleaser.yaml
• thanks for using goreleaser!
- 1
- 2
- 3
- 4
将会生成.goreleaser.yaml文件
before:
hooks:
- go mod tidy
- 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:'
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
build.goos 可以配置生成多个系统的发行包
- 在本地构建发布包,文件输出到dist目录
$ goreleaser release --snapshot --clean
- 1
- 修改.goreleaser.yaml符合自己需求,然后执行check检查该文件是否正确
$ goreleaser check
- 1
-
登录到github,打开settings -> Developer settinng -> Personal access tokens创建访问Token
-
设置GITHUB_TOKEN环境变量
export GITHUB_TOKEN="ghp_HHKqB7olfSYrIf6e0NUQIGaE1iAFoS3fSuX1"
- 1
- 提交代码并创建tag
git add .
git commit -m "feature x is finished"
git push
git tag -a v0.0.1 -m "version v0.0.1"
git push origin v0.0.1
- 1
- 2
- 3
- 4
- 5
注意,在发布之前一定要先执行此步骤提交代码和tag
- 执行发布
goreleaser release --clean
- 1
此条命令将会把二进制等发布到github上
执行此命令前必须先按第8步骤的顺序提交对应版本代码,创建对应版本tag,并push到远程仓库,否则发布的版本名称是新的但是代码却是上一次提交的代码。
Changelog是根据commit信息生成的,内容是基于上一个Release来的,即上一个Realse对应的commit到最新的commit之间的提交信息会体现在Changelog中。
推荐阅读
1. Go语言中局部变量的逃逸分析(从汇编的角度)
2. 使用VS Code调试Go程序
3. 使用树梅派搭建Golang、Python、NodeJs的开发服务器
评论记录:
回复评论: