Allow overriding of build.sh for downstream projects (#1544)

This commit is contained in:
Chris Suszynski 2021-12-10 13:50:29 +01:00 committed by GitHub
parent 404cda5ee5
commit e7d7cad429
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 15 deletions

View File

@ -14,20 +14,30 @@
function build_flags() { function build_flags() {
local base="${1}" local base="${1}"
local now="$(date -u '+%Y-%m-%d %H:%M:%S')" local now rev
local rev="$(git rev-parse --short HEAD)" now="$(date -u '+%Y-%m-%d %H:%M:%S')"
rev="$(git rev-parse --short HEAD)"
local pkg="knative.dev/client/pkg/kn/commands/version" local pkg="knative.dev/client/pkg/kn/commands/version"
local version="${TAG:-}" local version="${TAG:-}"
# Use vYYYYMMDD-local-<hash> for the version string, if not passed. # Use vYYYYMMDD-local-<hash> for the version string, if not passed.
if [[ -z "${version}" ]]; then if [[ -z "${version}" ]]; then
# Get the commit, excluding any tags but keeping the "dirty" flag # Get the commit, excluding any tags but keeping the "dirty" flag
local commit="$(git describe --always --dirty --match '^$')" local commit
commit="$(git describe --always --dirty --match '^$')"
[[ -n "${commit}" ]] || abort "error getting the current commit" [[ -n "${commit}" ]] || abort "error getting the current commit"
version="v$(date +%Y%m%d)-local-${commit}" version="v$(date +%Y%m%d)-local-${commit}"
fi fi
# Extract Eventing and Serving versions from go.mod # Extract Eventing and Serving versions from go.mod
local version_serving=$(cat go.mod | grep "knative.dev/serving " | sed -s 's/.* \(v.[\.0-9]*\).*/\1/') local version_serving version_eventing
local version_eventing=$(cat go.mod | grep "knative.dev/eventing " | sed -s 's/.* \(v.[\.0-9]*\).*/\1/') version_serving=$(grep "knative.dev/serving " "${base}/go.mod" \
| sed -s 's/.* \(v.[\.0-9]*\).*/\1/')
version_eventing=$(grep "knative.dev/eventing " "${base}/go.mod" \
| sed -s 's/.* \(v.[\.0-9]*\).*/\1/')
echo "-X '${pkg}.BuildDate=${now}' -X ${pkg}.Version=${version} -X ${pkg}.GitRevision=${rev} -X ${pkg}.VersionServing=${version_serving} -X ${pkg}.VersionEventing=${version_eventing}" echo "-X '${pkg}.BuildDate=${now}' \
-X ${pkg}.Version=${version} \
-X ${pkg}.GitRevision=${rev} \
-X ${pkg}.VersionServing=${version_serving} \
-X ${pkg}.VersionEventing=${version_eventing}\
${EXTERNAL_LD_FLAGS:-}"
} }

View File

@ -128,7 +128,7 @@ source_lint() {
go_build() { go_build() {
echo "🚧 Compile" echo "🚧 Compile"
go build -mod=vendor -ldflags "$(build_flags $(basedir))" -o kn ./cmd/... go build -mod=vendor -ldflags "$(build_flags "$(basedir)")" -o kn ./cmd/...
if $(file kn | grep -q -i "Windows"); then if $(file kn | grep -q -i "Windows"); then
mv kn kn.exe mv kn kn.exe
@ -136,7 +136,8 @@ go_build() {
} }
go_test() { go_test() {
local test_output=$(mktemp /tmp/kn-client-test-output.XXXXXX) local test_output
test_output="$(mktemp /tmp/kn-client-test-output.XXXXXX)"
local red="" local red=""
local reset="" local reset=""
@ -164,7 +165,8 @@ check_license() {
local required_keywords=("Authors" "Apache License" "LICENSE-2.0") local required_keywords=("Authors" "Apache License" "LICENSE-2.0")
local extensions_to_check=("sh" "go" "yaml" "yml" "json") local extensions_to_check=("sh" "go" "yaml" "yml" "json")
local check_output=$(mktemp /tmp/kn-client-licence-check.XXXXXX) local check_output
check_output="$(mktemp /tmp/kn-client-licence-check.XXXXXX)"
for ext in "${extensions_to_check[@]}"; do for ext in "${extensions_to_check[@]}"; do
find . -name "*.$ext" -a \! -path "./vendor/*" -a \! -path "./.*" -a \! -path "./third_party/*" -print0 | find . -name "*.$ext" -a \! -path "./vendor/*" -a \! -path "./.*" -a \! -path "./third_party/*" -print0 |
while IFS= read -r -d '' path; do while IFS= read -r -d '' path; do
@ -242,9 +244,10 @@ basedir() {
fi fi
fi fi
local dir=$(dirname "$script") local dir full_dir
local full_dir=$(cd "${dir}/.." && pwd) dir=$(dirname "$script")
echo ${full_dir} full_dir=$(cd "${dir}/.." && pwd)
echo "${full_dir}"
} }
# Checks if a flag is present in the arguments. # Checks if a flag is present in the arguments.
@ -262,8 +265,9 @@ has_flag() {
} }
cross_build() { cross_build() {
local basedir=$(basedir) local basedir ld_flags
local ld_flags="$(build_flags $basedir)" basedir=$(basedir)
ld_flags="$(build_flags $basedir)"
local failed=0 local failed=0
echo "⚔️ ${S}Compile" echo "⚔️ ${S}Compile"
@ -353,9 +357,12 @@ fi
source $(basedir)/vendor/knative.dev/hack/library.sh source $(basedir)/vendor/knative.dev/hack/library.sh
# Shared funcs with CI # Shared funcs with CI
while IFS= read -r -d '' file; do
source "${file}"
done < <(find "$(basedir)/hack/build.sh.d" -name '*.sh' -print0)
source $(basedir)/hack/build-flags.sh source $(basedir)/hack/build-flags.sh
# Fixe emoji labels for certain terminals # Fixe emoji labels for certain terminals
apply_emoji_fixes apply_emoji_fixes
run $* run "$@"

View File

@ -0,0 +1,4 @@
# Extending build.sh directory
This directory contains shell scripts named `*.sh`, that can be sourced by
build script, and can be used to extend or override the build script.