Skip to content
New issue

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

使用 GoReleaser 构建,打包和发布 Go 项目 #2

Open
wutz opened this issue Jan 7, 2024 · 0 comments
Open

使用 GoReleaser 构建,打包和发布 Go 项目 #2

wutz opened this issue Jan 7, 2024 · 0 comments
Labels

Comments

@wutz
Copy link
Owner

wutz commented Jan 7, 2024

背景

在开发完 Go 项目时,通常我们还需持续构建,打包 tar.gz 或 docker image, 然后推送到对应平台上。

通常在打包 tar.gz 或者 docker image 还是比较繁琐的,再结合 CI (github action / jenkins) 心智负担还是比较大的。

我们只希望专注在代码开发上,其它活动尽可能缩减心智负担,这时可以通过 GoReleaser 来简化。

使用

  1. 安装 Goreleaser

    $ brew install goreleaser
    # or
    $ go install github.com/goreleaser/goreleaser@latest

    其它安装方式参考 install

  2. 初始化 Goreleaser

    $ cat << 'EOF' > main.go
    package main
    
    func main() {
      println("Ba dum, tss!")
    }
    EOF
    $ go mod init hello
    # 上述过程只是创建一个示例 go 项目
    
    # 主要生成一个 .goreleaser.yaml 文件
    $ goreleaser init
  3. 调整构建目标平台

    修改 .goreleaser.yaml 文件的 builds 字段

    builds:
    - env:
       - CGO_ENABLED=0
       goos:
       - linux
       - windows
       - darwin
       goarch:
       - amd64
       - arm64
       binary: my-hello

    更多配置选项参考 build

  4. 调整打包 tar.gz

    通常来说使用缺省生成的即可,可以根据自己需要进行微调。更多参考 archive

  5. 构建 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 自动根据平台进行拉取)

    $ 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
  6. 设置 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
  7. 提交到 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

@wutz wutz changed the title 使用 Goreleaser 构建,打包和发布 Go 项目 使用 GoReleaser 构建,打包和发布 Go 项目 Jan 7, 2024
@wutz wutz added the go label Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant