前言
最近开发了一款 Markdown 文本格式化工具 mdfmt,开发好后想通过 Homebrew 一键安装,在探索+踩坑之后有了这篇文章。
本文以
mdfmt
为例,通过在 mdfmt
的 Github Repo 中配置 GoReleaser,来做到打版本 tag (比如 v1.2.3)后在触发的流水线中自动将新版本发布到自己的 Homebrew tap 仓库。注意本文只适应于使用 GoReleaser 进行发布管理的 Go 项目。
步骤
1. 创建 Homebrew Tap 仓库
在你自己的 Github 账号中创建名为
homebrew-tap
的仓库,注意仓库名称是固定的,仓库内容可以为空,不过最好添加一个 README 和 LICENSE,可以参考我的 homebrew-tap 仓库。2. 创建 Person Access Token(PAT)
在 Github 账号 Settings 中创建具有写权限的 Github Person Access Token(PAT),后续要配置到项目的 GoReleaser 配置中,因为 GoReleaser 要向 homebrew-tap 仓库自动提交 commit,需要有写权限。
创建 PAT 的时候,scope 选择 repo:

3. 在 GoReleaser 配置文件中添加 brews 配置
在项目的 .goreleaser.yml 文件中添加 brew 配置,同时将 PAT 配置到 GoReleaser 的 Github Action 中。
3.1 将上一步创建的 PAT 值配置到仓库的 Secret 中:

3.2 在 GoReleaser Action 中引用 Secret 中的 PAT 变量,具体可参考这里:

3.3 在项目的 .goreleaser.yml 文件中添加 brews 配置,具体可参考这里:
brews:
- tap:
owner: elliotxx
name: homebrew-tap
# token: "{{ .Env.HOMEBREW_TAP_GITHUB_TOKEN }}"
url_template: "https://github.com/elliotxx/mdfmt/releases/download/{{ .Tag }}/{{ .ArtifactName }}"
download_strategy: CurlDownloadStrategy
# Git author used to commit to the repository.
# Defaults are shown.
commit_author:
name: GoReleaser Bot
email: goreleaser@carlosbecker.com
folder: HomebrewFormula
homepage: "https://github.com/elliotxx/mdfmt"
description: "A Markdown formatter that follow the CommonMark. Like gofmt, but for Markdown."
license: "MIT"
skip_upload: false
test: |
system "#{bin}/mdfmt -V"
注意 PAT 必须配置到 GoReleaser Action 配置中,否则 GoReleaser 流水线会报错:Resource not accessible by integration。

效果
- 打版本号 tag,比如 v0.2.0
- Github 仓库中自动触发一条流水线

- 流水线执行成功后,版本更新的信息 GoReleaser 会自动提交到自己的 homebrew-tap 仓库中

- 本地执行
brew install elliotxx/tap/mdfmt
,会安装最新版本 v0.2.0

参考资料
- GoReleaser - Github Actions https://goreleaser.com/ci/actions/
- GoReleaser - Homebrew Taps https://goreleaser.com/customization/homebrew/