We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
在开发完 Go 项目时,通常我们还需持续构建,打包 tar.gz 或 docker image, 然后推送到对应平台上。
通常在打包 tar.gz 或者 docker image 还是比较繁琐的,再结合 CI (github action / jenkins) 心智负担还是比较大的。
我们只希望专注在代码开发上,其它活动尽可能缩减心智负担,这时可以通过 GoReleaser 来简化。
安装 Goreleaser
$ brew install goreleaser # or $ go install github.com/goreleaser/goreleaser@latest
其它安装方式参考 install
初始化 Goreleaser
$ cat << 'EOF' > main.go package main func main() { println("Ba dum, tss!") } EOF $ go mod init hello # 上述过程只是创建一个示例 go 项目 # 主要生成一个 .goreleaser.yaml 文件 $ goreleaser init
调整构建目标平台
修改 .goreleaser.yaml 文件的 builds 字段
.goreleaser.yaml
builds
builds: - env: - CGO_ENABLED=0 goos: - linux - windows - darwin goarch: - amd64 - arm64 binary: my-hello
更多配置选项参考 build
调整打包 tar.gz
通常来说使用缺省生成的即可,可以根据自己需要进行微调。更多参考 archive
构建 docker image
Goreleaser 会复用 build 阶段产生的 binary,不需要在 Dockerfile 描述 build 构建(额外描述 build 会报错)
$ cat << 'EOF' > Dockerfile FROM alpine:latest COPY my-hello /bin/my-hello EXPOSE 8080 ENTRYPOINT [ "/bin/my-hello" ] EOF
dockers 描述构建 2 个平台的 Docker Image, docker_manifests 描述在 push 到 docker registry 时额外合并成一个名字 Image (这种单一 image name 用户无需关心所在的是 amd64 还是 arm64 平台,docker pull 自动根据平台进行拉取)
dockers
docker_manifests
$ cat << 'EOF' >> .goreleaser.yaml dockers: - image_templates: - "ghcr.io/wutz/hello:{{ .Tag }}-amd64" use: buildx build_flag_templates: - "--pull" - "--platform=linux/amd64" - image_templates: - "ghcr.io/wutz/hello:{{ .Tag }}-arm64" use: buildx build_flag_templates: - "--pull" - "--platform=linux/arm64" goarch: arm64 docker_manifests: - name_template: "ghcr.io/wutz/hello:{{ .Tag }}" image_templates: - "ghcr.io/wutz/hello:{{ .Tag }}-amd64" - "ghcr.io/wutz/hello:{{ .Tag }}-arm64" EOF
设置 github action
当打 tag 时会触发 github action 进行构建,打包和推送
$ mkdir -p .github/workflows $ cat << 'EOF' > .github/workflows/release.yml name: goreleaser on: pull_request: push: # run only against tags tags: - "*" permissions: contents: write packages: write # issues: write jobs: goreleaser: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4 with: fetch-depth: 0 - name: Set up Go uses: actions/setup-go@v4 with: go-version: stable - uses: docker/login-action@v3 # login to ghcr with: registry: ghcr.io username: ${{ github.repository_owner }} password: ${{ secrets.GITHUB_TOKEN }} - name: Run GoReleaser uses: goreleaser/goreleaser-action@v5 with: # either 'goreleaser' (default) or 'goreleaser-pro' distribution: goreleaser version: latest args: release --clean env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} EOF
提交到 github
# 在本地构建和打包测试一下 $ goreleaser release --snapshot --clean # 提交到 github $ git add . && git commit -m "Init" && git push $ git tag v0.0.1 && git push --tags
如果遇到一些 docker 构建错误参考 docker-error
The text was updated successfully, but these errors were encountered:
No branches or pull requests
背景
在开发完 Go 项目时,通常我们还需持续构建,打包 tar.gz 或 docker image, 然后推送到对应平台上。
通常在打包 tar.gz 或者 docker image 还是比较繁琐的,再结合 CI (github action / jenkins) 心智负担还是比较大的。
我们只希望专注在代码开发上,其它活动尽可能缩减心智负担,这时可以通过 GoReleaser 来简化。
使用
安装 Goreleaser
$ brew install goreleaser # or $ go install github.com/goreleaser/goreleaser@latest
初始化 Goreleaser
调整构建目标平台
修改
.goreleaser.yaml
文件的builds
字段调整打包 tar.gz
通常来说使用缺省生成的即可,可以根据自己需要进行微调。更多参考 archive
构建 docker image
Goreleaser 会复用 build 阶段产生的 binary,不需要在 Dockerfile 描述 build 构建(额外描述 build 会报错)
dockers
描述构建 2 个平台的 Docker Image,docker_manifests
描述在 push 到 docker registry 时额外合并成一个名字 Image (这种单一 image name 用户无需关心所在的是 amd64 还是 arm64 平台,docker pull 自动根据平台进行拉取)设置 github action
当打 tag 时会触发 github action 进行构建,打包和推送
提交到 github
其它
如果遇到一些 docker 构建错误参考 docker-error
The text was updated successfully, but these errors were encountered: