Skip to content

Commit

Permalink
Build manager binary in a modular way
Browse files Browse the repository at this point in the history
Allow overriding CGO_ENABLED value, debug flags as part of ldflags, and exporting GOEXPERIMENT
  • Loading branch information
razo7 committed Sep 18, 2024
1 parent 2632f16 commit 1e4b696
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions hack/build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!/bin/bash
set -ex

#!/bin/bash -ex
DEST_DIR=$1

GIT_VERSION=$(git describe --always --tags || true)
Expand All @@ -11,8 +9,24 @@ BUILD_DATE=$(date --utc -Iseconds)

mkdir -p bin

LDFLAGS="-s -w "
LDFLAGS+="-X github.com/medik8s/machine-deletion-remediation/version.Version=${VERSION} "
LDFLAGS+="-X github.com/medik8s/machine-deletion-remediation/version.GitCommit=${COMMIT} "
LDFLAGS+="-X github.com/medik8s/machine-deletion-remediation/version.BuildDate=${BUILD_DATE} "
GOFLAGS=-mod=vendor CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="${LDFLAGS}" -o "${DEST_DIR}"/manager main.go
LDFLAGS_VALUE="-X github.com/medik8s/machine-deletion-remediation/version.Version=${VERSION} "
LDFLAGS_VALUE+="-X github.com/medik8s/machine-deletion-remediation/version.GitCommit=${COMMIT} "
LDFLAGS_VALUE+="-X github.com/medik8s/machine-deletion-remediation/version.BuildDate=${BUILD_DATE} "
# allow override for debugging flags
LDFLAGS_DEBUG="${LDFLAGS_DEBUG:-" -s -w"}"
LDFLAGS_VALUE+="${LDFLAGS_DEBUG}"
# must be single quoted for use in GOFLAGS, and for more options see https://pkg.go.dev/cmd/link
LDFLAGS="'-ldflags=${LDFLAGS_VALUE}'"

# add ldflags to goflags
export GOFLAGS+=" ${LDFLAGS}"
echo "goflags: ${GOFLAGS}"

# allow override and use zero by default- static linking
export CGO_ENABLED=${CGO_ENABLED:-0}
echo "cgo: ${CGO_ENABLED}"

# export in case it was set
export GOEXPERIMENT="${GOEXPERIMENT}"

GOOS=linux GOARCH=amd64 go build -o "${DEST_DIR}"/manager main.go

0 comments on commit 1e4b696

Please sign in to comment.