GoReleaser can help you, to some extent, to have reproducible builds.

Reproducible Builds

Reproducible Builds

What are reproducible builds?

According to Reproducible-Builds.org:

A build is reproducible if given the same source code, build environment and build instructions, any party can recreate bit-by-bit identical copies of all specified artifacts.

So, things we need to pay attention here are:

  • the source is the same
  • the dependencies are the same, in the same versions
  • chtimes are the same
  • build path is the same
  • any other tools needed to compile must be the same, in the same versions

While this might sound complicated, rest assured GoReleaser can help you with most of these items!

Reproducible Builds with GoReleaser

GoReleaser will by default inject a ldflag with the current timestamp as main.date, which you can use to display build time information. We will want to change that to use some fixed date, for instance, the date of the commit being built.

To avoid changes from one machine to another, we will also want to use -trimpath.

Finally, we’ll want to make sure the repo code haven’t changed, e.g., when building a tag, we want to make sure it wasn’t deleted and pushed again (i.e., moved).

We can achieve that with a config that looks like this:

- env:
  - linux
  - darwin
  - amd64
  - arm64
  mod_timestamp: '{{ .CommitTimestamp }}'
    - -trimpath
    - -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{ .CommitDate }}

  proxy: true

From now on, we basically only need to ensure the Go version is the same.

That’s out of the scope of GoReleaser’s scope, but easy enough to do in GitHub Actions by pinning to a specific version of Go.

So, there you have it: reproducible Go binary builds with GoReleaser!

This is a cross-post from GoReleaser’s blog!