Skip to content

Commit

Permalink
chore: add reproducible builds
Browse files Browse the repository at this point in the history
So `make build`, `make install` and `goreleaser build` produce the exact
same binary.

The fix consists mainly of tuning the ldflags of the Makefile and of the
.goreleaser file:
- arguments must be in the same order
- removed `-X main.commit` and `-X main.date` as these are the default
  ldflags of goreleaser and were probably blindly reported in the custom
  version, but are actually useless as there is no `commit` or `date`
  field in the main package of the application.
- replaced tendermint/tendermint by cometbft/cometbft

A couple of other tiny things had to be updated:
- align the version name when there's no tag (snapshot.template_name in
  goreleaser and VERSION var in Makefile)
- fix Makefile VERSION var when there's a tag
- add CGO_ENABLED=0 in Makefile
- remove double quotes around the BuildTags ldflag because it's useless
  and goreleaser doesn't use double quotes.

Quick demo:
```sh
$ make build install

$ TM_VERSION=v0.37.4 goreleaser build --single-target --clean --snapshot

$ sha256sum $GOBIN/atomoned build/atomoned dist/atomoned_linux_amd64_v1/atomoned
9a654d794956e35ca25469c4bb4b074bf041acc1f579a02872eab20144dece5a  $GOBIN/atomoned
9a654d794956e35ca25469c4bb4b074bf041acc1f579a02872eab20144dece5a  build/atomoned
9a654d794956e35ca25469c4bb4b074bf041acc1f579a02872eab20144dece5a  dist/atomoned_linux_amd64_v1/atomoned
```

If go1.21 is not available you can install it using:
`go install golang.org/dl/go1.21.13@latest`
then prefix the first 2 commands with:
`GOROOT=$(go1.21.13 env GOROOT) PATH=$GOROOT/bin:$PATH`
  • Loading branch information
tbruyelle committed Aug 21, 2024
1 parent 87d4c1b commit 707f142
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ builds:
- CGO_ENABLED=0
ldflags:
# .Env.TM_VERSION is provided in the workflow runner environment -> see .github/workflows/release.yml
- -s -w -X main.commit={{.Commit}} -X main.date={{ .CommitDate }} -X github.com/cosmos/cosmos-sdk/version.Name=atomone -X github.com/cosmos/cosmos-sdk/version.AppName=atomeond -X github.com/cosmos/cosmos-sdk/version.Version=v{{ .Version }} -X github.com/cosmos/cosmos-sdk/version.Commit={{ .Commit }} -X github.com/cosmos/cosmos-sdk/version.BuildTags=netgo,ledger -X github.com/tendermint/tendermint/version.TMCoreSemVer={{ .Env.TM_VERSION }}
- -X github.com/cosmos/cosmos-sdk/version.Name=atomone -X github.com/cosmos/cosmos-sdk/version.AppName=atomoned -X github.com/cosmos/cosmos-sdk/version.Version=v{{ .Version }} -X github.com/cosmos/cosmos-sdk/version.Commit={{ .Commit }} -X github.com/cosmos/cosmos-sdk/version.BuildTags=netgo,ledger -X github.com/cometbft/cometbft/version.TMCoreSemVer={{ .Env.TM_VERSION }} -w -s
goos:
- darwin
- linux
Expand Down Expand Up @@ -50,7 +50,7 @@ checksum:
algorithm: sha256

snapshot:
name_template: SNAPSHOT-{{ .Commit }}
name_template: "{{ .Version }}-{{ .ShortCommit }}"

changelog:
skip: false
Expand Down
12 changes: 7 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/usr/bin/make -f

BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
COMMIT := $(shell git log -1 --format='%H')

# don't override user values
ifeq (,$(VERSION))
VERSION := $(shell git describe --exact-match 2>/dev/null)
VERSION := $(shell git describe --tags --exact-match 2>/dev/null)
# if VERSION is empty, then populate it with branch's name and raw commit hash
ifeq (,$(VERSION))
VERSION := $(BRANCH)-$(COMMIT)
PREVIOUS_TAG := $(shell git describe --tags --abbrev=0)
SHORT_COMMIT := $(shell git rev-parse --short HEAD)
VERSION := $(PREVIOUS_TAG)-$(SHORT_COMMIT)
endif
endif

Expand All @@ -24,6 +25,7 @@ GO_SYSTEM_VERSION = $(shell go version | cut -c 14- | cut -d' ' -f1 | cut -d'.'
REQUIRE_GO_VERSION = 1.21

export GO111MODULE = on
export CGO_ENABLED = 0

# process build tags

Expand Down Expand Up @@ -68,7 +70,7 @@ ldflags = -X github.com/cosmos/cosmos-sdk/version.Name=atomone \
-X github.com/cosmos/cosmos-sdk/version.AppName=atomoned \
-X github.com/cosmos/cosmos-sdk/version.Version=$(VERSION) \
-X github.com/cosmos/cosmos-sdk/version.Commit=$(COMMIT) \
-X "github.com/cosmos/cosmos-sdk/version.BuildTags=$(build_tags_comma_sep)" \
-X github.com/cosmos/cosmos-sdk/version.BuildTags=$(build_tags_comma_sep) \
-X github.com/cometbft/cometbft/version.TMCoreSemVer=$(TM_VERSION)

ifeq (cleveldb,$(findstring cleveldb,$(ATOMONE_BUILD_OPTIONS)))
Expand Down Expand Up @@ -97,7 +99,7 @@ include contrib/devtools/Makefile

check_version:
ifneq ($(GO_SYSTEM_VERSION), $(REQUIRE_GO_VERSION))
@echo "ERROR: Go version 1.21 is required for $(VERSION) of AtomOne."
@echo "ERROR: Go version $(REQUIRE_GO_VERSION) is required for $(VERSION) of AtomOne."
endif

all: install lint run-tests test-e2e vulncheck
Expand Down

0 comments on commit 707f142

Please sign in to comment.