From e0f4f51e6743f144078a9a7abfb84a1ab340638b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20Hu=C3=9F?= Date: Sat, 18 May 2019 00:17:09 +0200 Subject: [PATCH] fix(build.sh): Reintroduced build-flags.sh (#122) so that it can be shared between build.sh and release.sh Fixes #120 --- hack/build-flags.sh | 14 ++++++++++++++ hack/build.sh | 5 ++--- hack/release.sh | 3 ++- 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 hack/build-flags.sh diff --git a/hack/build-flags.sh b/hack/build-flags.sh new file mode 100644 index 000000000..deef5575a --- /dev/null +++ b/hack/build-flags.sh @@ -0,0 +1,14 @@ +function build_flags() { + local now="$(date -u '+%Y-%m-%d %H:%M:%S')" + local rev="$(git rev-parse --short HEAD)" + local pkg="github.com/knative/client/pkg/kn/commands" + local version="${TAG:-}" + # Use vYYYYMMDD-local- for the version string, if not passed. + if [[ -z "${version}" ]]; then + # Get the commit, excluding any tags but keeping the "dirty" flag + local commit="$(git describe --always --dirty --match '^$')" + [[ -n "${commit}" ]] || abort "error getting the current commit" + version="v$(date +%Y%m%d)-local-${commit}" + fi + echo "-X '${pkg}.BuildDate=${now}' -X ${pkg}.Version=${version} -X ${pkg}.GitRevision=${rev}" +} diff --git a/hack/build.sh b/hack/build.sh index b945665fc..a01a46dbf 100755 --- a/hack/build.sh +++ b/hack/build.sh @@ -19,16 +19,15 @@ set -eu dir=$(dirname "${BASH_SOURCE[0]}") base=$(cd "$dir/.." && pwd) -source ${base}/hack/util/flags.sh +source ${base}/hack/build-flags.sh export GO111MODULE=on echo "📋 Formatting" go fmt ${base}/cmd/... ${base}/pkg/... echo "🚧 Building" -go build -mod=vendor -ldflags "$(ld_flags ${base}/hack)" -o ${base}/kn ${base}/cmd/... +go build -mod=vendor -ldflags "$(build_flags)" -o ${base}/kn ${base}/cmd/... echo "🌞 Success" - ${base}/hack/generate-docs.sh ${base}/kn version diff --git a/hack/release.sh b/hack/release.sh index dd143682f..c1555acef 100755 --- a/hack/release.sh +++ b/hack/release.sh @@ -15,13 +15,14 @@ # limitations under the License. source $(dirname $0)/../vendor/github.com/knative/test-infra/scripts/release.sh +source $(dirname $0)/build-flags.sh function build_release() { + local ld_flags="$(build_flags)" local pkg="github.com/knative/client/pkg/kn/commands" local version="${TAG}" # Use vYYYYMMDD--local for the version string, if not passed. [[ -z "${version}" ]] && version="v${BUILD_TAG}-local" - local ld_flags="-X '${pkg}.BuildDate=${BUILD_TIMESTAMP}' -X ${pkg}.Version=${version} -X ${pkg}.GitRevision=${BUILD_COMMIT_HASH}" export GO111MODULE=on export CGO_ENABLED=0