From 707f142613794e1fc8dc6371390d003f9245a457 Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Wed, 21 Aug 2024 18:11:02 +0200 Subject: [PATCH] chore: add reproducible builds 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` --- .goreleaser.yml | 4 ++-- Makefile | 12 +++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index e09ff4f7..51d5da2d 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -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 @@ -50,7 +50,7 @@ checksum: algorithm: sha256 snapshot: - name_template: SNAPSHOT-{{ .Commit }} + name_template: "{{ .Version }}-{{ .ShortCommit }}" changelog: skip: false diff --git a/Makefile b/Makefile index de9673e6..7ba594f0 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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 @@ -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))) @@ -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