-
Notifications
You must be signed in to change notification settings - Fork 174
perf(ci): version refactor (remove Xref/status.sh) #19424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,16 @@ | ||
| package internal | ||
|
|
||
| // Version variables are populated at init time by the generated zversion.go | ||
| // file (created by go-tool.sh). Without go-tool.sh, all values remain empty. | ||
| var ( | ||
| // MainVersion is the Rox version. | ||
| MainVersion string //XDef:STABLE_MAIN_VERSION | ||
| MainVersion string | ||
| // CollectorVersion is the collector version to be used by default. | ||
| CollectorVersion string //XDef:STABLE_COLLECTOR_VERSION | ||
| CollectorVersion string | ||
| // FactVersion is the fact version to be used by default. | ||
| FactVersion string //XDef:STABLE_FACT_VERSION | ||
| FactVersion string | ||
| // ScannerVersion is the scanner version to be used with this Rox version. | ||
| ScannerVersion string //XDef:STABLE_SCANNER_VERSION | ||
| ScannerVersion string | ||
| // GitShortSha is the (short) Git SHA that was built. | ||
| GitShortSha string //XDef:STABLE_GIT_SHORT_SHA | ||
| GitShortSha string | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,46 +12,55 @@ die() { | |
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Consider guarding git invocations when TOOL=test to avoid failures in build contexts without a .git directory In the To avoid that, you could either (a) skip version generation in test mode when |
||
|
|
||
| RACE="${RACE:-false}" | ||
|
|
||
| x_defs=() | ||
| x_def_errors=() | ||
|
|
||
| while read -r line || [[ -n "$line" ]]; do | ||
| if [[ "$line" =~ ^[[:space:]]*$ ]]; then | ||
| continue | ||
| elif [[ "$line" =~ ^([^[:space:]]+)[[:space:]]+(.*)[[:space:]]*$ ]]; then | ||
| var="${BASH_REMATCH[1]}" | ||
| def="${BASH_REMATCH[2]}" | ||
| eval "status_${var}=$(printf '%q' "$def")" | ||
| REPO_ROOT="${SCRIPT_DIR}/.." | ||
|
|
||
| # Read version data from committed files. | ||
| COLLECTOR_VERSION="$(cat "${REPO_ROOT}/COLLECTOR_VERSION")" || die "Missing COLLECTOR_VERSION" | ||
| SCANNER_VERSION="$(cat "${REPO_ROOT}/SCANNER_VERSION")" || die "Missing SCANNER_VERSION" | ||
| FACT_VERSION="$(cat "${REPO_ROOT}/FACT_VERSION")" || die "Missing FACT_VERSION" | ||
|
|
||
| # Generate version data file. Tests use only the base tag (stable across | ||
| # commits) to keep ActionIDs stable for test result caching. Builds get | ||
| # the full git-describe version with commit count and SHA. | ||
| generate_version_file() { | ||
| local target="${REPO_ROOT}/pkg/version/internal/zversion.go" | ||
| local main_version git_short_sha | ||
|
|
||
| if [[ "$TOOL" == "test" ]]; then | ||
| # Base tag only (e.g. "4.11.x") — stable across commits. | ||
| main_version="$(cd "${REPO_ROOT}"; git describe --tags --abbrev=0 --exclude '*-nightly-*' 2>/dev/null)" || die "git describe failed" | ||
| git_short_sha="" | ||
| elif [[ -n "${BUILD_TAG:-}" ]]; then | ||
| # Konflux/release builds set BUILD_TAG to the full version string. | ||
| # Use it directly (the Docker build context has no .git directory). | ||
| main_version="${BUILD_TAG}" | ||
| git_short_sha="$(echo "$BUILD_TAG" | sed -n 's/.*g\([0-9a-f]\{1,\}\)$/\1/p')" | ||
| else | ||
| die "Malformed status.sh output line ${line}" | ||
| # Full version from git describe (e.g. "4.11.x-193-g7257553280"). | ||
| main_version="$(cd "${REPO_ROOT}"; git describe --tags --abbrev=10 --long --exclude '*-nightly-*' 2>/dev/null)" || die "git describe failed" | ||
| git_short_sha="$(cd "${REPO_ROOT}"; git rev-parse --short HEAD 2>/dev/null || echo "")" | ||
| fi | ||
| done < <(cd "${SCRIPT_DIR}/.."; ./status.sh) | ||
|
|
||
| while read -r line || [[ -n "$line" ]]; do | ||
| if [[ "$line" =~ ^[[:space:]]*$ ]]; then | ||
| continue | ||
| elif [[ "$line" =~ ^([^:]+):([[:digit:]]+):[[:space:]]*(var[[:space:]]+)?([^[:space:]]+)[[:space:]].*//XDef:([^[:space:]]+)[[:space:]]*$ ]]; then | ||
| go_file="${BASH_REMATCH[1]}" | ||
| go_line="${BASH_REMATCH[2]}" | ||
| go_var="${BASH_REMATCH[4]}" | ||
| status_var="${BASH_REMATCH[5]}" | ||
|
|
||
| varname="status_${status_var}" | ||
| [[ -n "${!varname}" ]] || x_def_errors+=( | ||
| "Variable ${go_var} defined in ${go_file}:${go_line} references status var ${status_var} that is not part of the status.sh output" | ||
| ) | ||
| go_package="$(cd "${SCRIPT_DIR}/.."; go list -e "./$(dirname "$go_file")")" | ||
|
|
||
| x_defs+=(-X "\"${go_package}.${go_var}=${!varname}\"") | ||
|
|
||
| local new_content | ||
| new_content="// Code generated by go-tool.sh; DO NOT EDIT. | ||
|
|
||
| package internal | ||
|
|
||
| func init() { | ||
| MainVersion = \"${main_version}\" | ||
| CollectorVersion = \"${COLLECTOR_VERSION}\" | ||
| FactVersion = \"${FACT_VERSION}\" | ||
| ScannerVersion = \"${SCANNER_VERSION}\" | ||
| GitShortSha = \"${git_short_sha}\" | ||
| }" | ||
| if [[ -f "$target" ]] && [[ "$(cat "$target")" == "$new_content" ]]; then | ||
| return | ||
| fi | ||
| done < <(git -C "${SCRIPT_DIR}/.." grep -n '//XDef:' -- '*.go') | ||
| if [[ "${#x_def_errors[@]}" -gt 0 ]]; then | ||
| printf >&2 "%s\n" "${x_def_errors[@]}" | ||
| exit 1 | ||
| fi | ||
| echo "$new_content" > "$target" | ||
| } | ||
| generate_version_file | ||
|
|
||
| ldflags=("${x_defs[@]}") | ||
| ldflags=() | ||
| if [[ "$DEBUG_BUILD" != "yes" ]]; then | ||
| ldflags+=(-s -w) | ||
| fi | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question (bug_risk): Clarify behavior when BUILD_TAG is not provided to the Docker build
With the current setup, a plain
docker buildwithout--build-arg BUILD_TAG=...will leaveBUILD_TAGempty. In images without.git,go-tool.shwill then fail when it triesgit describe. Consider either enforcing thatBUILD_TAGis set (e.g.,RUN test -n "$BUILD_TAG") or adding a non-.git-based fallback for version generation.