iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🚀

How GoReleaser Simplifies Declarative Release Distribution

に公開

Recently, I created a tool called cc-flavors as a practice project for learning Go. I found GoReleaser, which I used for the project, to be extremely convenient, so I would like to introduce it here.

https://zenn.dev/hiruno_tarte/articles/cc-flavors-introduction

What is GoReleaser?

https://goreleaser.com/

It is a convenient tool that allows you to declaratively configure and perform the following tasks using a single .goreleaser.yaml configuration file:

  • Building binaries for multiple OS/architectures and creating tar.gz files
  • Generating checksums.txt and SBOMs
  • Creating GitHub Releases
  • Publishing to package managers like Homebrew, Docker, Scoop, etc.
  • Signing with GPG or cosign

In this article, I will introduce how easy GoReleaser is to use, taking the actual settings I use as an example.

Let's Configure It

List of Release Assets
A list of assets actually generated by GoReleaser

Multi-OS/Architecture Build

Specify the target OS/architecture in the builds section.

builds:
  - main: .
    binary: cc-flavors
    ldflags:
      - '-s -w -X main.revision={{ .ShortCommit }}'
    goos:
      - linux
      - darwin
    goarch:
      - amd64
      - arm64

This generates built binaries for each OS/architecture.

By the way, ldflags are flags for the linker.

  • -s / -w: Reduces the binary size by removing debug information such as symbol tables and DWARF (in this case, by about 30%).
  • main.revision={{ .ShortCommit }}: Embeds the revision into the binary.

Distribution Settings

In the archives section, you can determine the composition, naming, and format of the distributions.

# Output example: cc-flavors_1.0.0_darwin_arm64.tar.gz
archives:
  - formats:
      - tar.gz
    name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}'
    files:
      - LICENSE
      - README.md

Checksums

In the checksum section, you can generate hashes to check each binary for tampering or corruption.

checksum:
  name_template: 'checksums.txt'

For small-scale CLIs, people might think this is unnecessary, but since it only takes 1-2 lines to add to the configuration, it is well worth it.

Actual tampering/corruption checks are performed by the user side.

$ grep cc-flavors_1.0.0_linux_amd64.tar.gz checksums.txt | sha256sum -c -
cc-flavors_1.0.0_linux_amd64.tar.gz: OK

However, since there is a risk that you cannot detect tampering if both the distribution and the checksum are tampered with, it should be used in conjunction with signing as described below.

Signing

You can perform various types of signing in the signs section.

By signing the checksum file as checksums.txt.sig, you can prove that the checksum itself has not been tampered with.

signs:
  - artifacts: checksum
    cmd: cosign
    signature: '${artifact}.sig'
    output: true
    args:
      - sign-blob
      - --yes
      - --output-signature
      - ${signature}
      - ${artifact}

This time, I used a method called cosign instead of the standard GPG.
I would like to explain cosign in another article later, but roughly speaking, it is an OIDC-based keyless authentication method, and its biggest advantage is that you do not need to manage keys like you do with GPG.

(Added) I wrote it.

https://zenn.dev/hiruno_tarte/articles/cosign-signature

SBOM

I create an SBOM for each generated archive in the sbom section.

sboms:
  - artifacts: archive

By making dependencies transparent, it helps with auditing and protecting against supply chain attacks.
This is also over-spec for a small-scale CLI, but I have included it since it can be configured in two lines.

Release

I configure GitHub Release in the release section.

In addition to github, it supports various platforms such as gitlab, homebrew, npm, scoop, and docker (reference).

release:
  github:
    owner: takymt
    name: cc-flavors

GitHub Actions

https://goreleaser.com/ci/actions/

Make sure to set up the action for releases.

With the configuration above, you just need to install Go, cosign, and syft (for SBOM generation) and then run goreleaser.

Example Configuration

Conclusion

How did you like this introduction to how GoReleaser allows you to write release configurations in a simple and declarative manner?

I will continue to be grateful for convenient CD tools and make the most of them.
Everyone, have a great DevOps life! 🚀

GitHubで編集を提案

Discussion