Skip to content

Commit 4a9defd

Browse files
committed
build: Ensure clean go build environment for goreleaser and ko
Previous builds were overriding the `GOOS` and `GOARCH` enviroment variables, which is fine to do when using `ko` directly, but appears to be broken when building via ko using `goreleaser`. This commit ensures that `GOOS` and `GOARCH` are always unset when running builds by undefining them via `make` `undefine`. The `override` keyword ensures that the env vars are always undefined regardless of the source (e.g. enviroment, make variables, etc) to ensure a clean build environment. Instead of using `GOARCH` env var for building for the local build architecure, this commit uses `goreleaser`'s `{{ .Runtime.Goarch }}` variable available via templating which contains the architecture of the build machine. This commit also removes the `ldflags` config from the `ko` configuration in `goreleaser` config as this is automatically inherited from the `build` config referenced in the `ko` config.
1 parent 036d8f5 commit 4a9defd

File tree

2 files changed

+11
-20
lines changed

2 files changed

+11
-20
lines changed

.goreleaser.yml

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ before:
4242
$(helm template {{ .ProjectName }} ./charts/{{ .ProjectName }} \
4343
--namespace caren-system \
4444
--set-string image.tag=v{{ trimprefix .Version "v" }} \
45-
--set-string helmRepositoryImage.tag=v{{ trimprefix .Version "v" }}{{ if .IsSnapshot }}-{{ .Env.GOARCH }} \
45+
--set-string helmRepositoryImage.tag=v{{ trimprefix .Version "v" }}{{ if .IsSnapshot }}-{{ .Runtime.Goarch }} \
4646
--set-string image.repository=ko.local/{{ .ProjectName }}{{ end }} \
4747
)
4848
EOF'
@@ -80,11 +80,12 @@ builds:
8080
post:
8181
- |
8282
sh -ec 'if [ {{ .IsSnapshot }} == true ] && [ {{ .Runtime.Goarch }} == {{ .Arch }} ]; then
83-
env GOOS=linux GOARCH={{ .Arch }} \
84-
SOURCE_DATE_EPOCH=$(date +%s) \
83+
env SOURCE_DATE_EPOCH=$(date +%s) \
84+
KO_DATA_DATE_EPOCH=$(date +%s) \
8585
KO_DOCKER_REPO=ko.local/{{ .ProjectName }} \
8686
ko build \
8787
--bare \
88+
--platform linux/{{ .Runtime.Goarch }} \
8889
-t v{{ trimprefix .Version "v" }} \
8990
./cmd
9091
fi'
@@ -135,16 +136,6 @@ docker_manifests:
135136
kos:
136137
- id: cluster-api-runtime-extensions-nutanix
137138
build: cluster-api-runtime-extensions-nutanix
138-
ldflags:
139-
- -s
140-
- -w
141-
- -X 'k8s.io/component-base/version.buildDate={{ .CommitDate }}'
142-
- -X 'k8s.io/component-base/version.gitCommit={{ .FullCommit }}'
143-
- -X 'k8s.io/component-base/version.gitTreeState={{ .Env.GIT_TREE_STATE }}'
144-
- -X 'k8s.io/component-base/version.gitVersion=v{{ trimprefix .Version "v" }}'
145-
- -X 'k8s.io/component-base/version.major={{ .Major }}'
146-
- -X 'k8s.io/component-base/version.minor={{ .Minor }}'
147-
- -X 'k8s.io/component-base/version/verflag.programName={{ .ProjectName }}'
148139
labels:
149140
org.opencontainers.image.created: "{{ .CommitDate }}"
150141
org.opencontainers.image.title: "{{ .ProjectName }}"
@@ -156,6 +147,8 @@ kos:
156147
- linux/arm64
157148
repository: ghcr.io/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix
158149
bare: true
150+
creation_time: "{{.CommitTimestamp}}"
151+
ko_data_creation_time: "{{.CommitTimestamp}}"
159152
tags:
160153
- 'v{{ trimprefix .Version "v" }}'
161154

make/go.mk

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@
55
# to be private (not available publicly) and should therefore not use the proxy or checksum database
66
export GOPRIVATE ?=
77

8+
# Always ensure that GOOS and GOARCH are unset in the evironment, otherwise this can cause issues
9+
# with goreleaser and ko building images for the wrong platform.
10+
override undefine GOOS
11+
override undefine GOARCH
12+
813
ALL_GO_SUBMODULES := $(shell find -mindepth 2 -maxdepth 2 -name go.mod -printf '%P\n' | sort)
914
GO_SUBMODULES_NO_DOCS := $(filter-out $(addsuffix /go.mod,docs),$(ALL_GO_SUBMODULES))
1015
THIRD_PARTY_GO_SUBMODULES := $(shell find hack/third-party -mindepth 2 -name go.mod -printf 'hack/third-party/%P\n' | sort)
1116

12-
ifndef GOOS
13-
export GOOS := $(OS)
14-
endif
15-
ifndef GOARCH
16-
export GOARCH := $(shell go env GOARCH)
17-
endif
18-
1917
define go_test
2018
source <(setup-envtest use -p env $(ENVTEST_VERSION)) && \
2119
gotestsum \

0 commit comments

Comments
 (0)