diff --git a/.dockerignore b/.dockerignore index 0b450aea0dbf1..41b81bd7c7641 100644 --- a/.dockerignore +++ b/.dockerignore @@ -15,6 +15,11 @@ !thirdparty !patch !.cargo +# Include minimal .git metadata for build.rs to detect git commit SHA +# (~208KB total - needed for docker builds to include git_commit in startup log) +!.git/HEAD +!.git/refs/heads +!.git/packed-refs !deny.toml !clippy.toml diff --git a/Dockerfile.unit b/Dockerfile.unit index 7f9f7b6895ab8..8b18548a8ae22 100644 --- a/Dockerfile.unit +++ b/Dockerfile.unit @@ -32,7 +32,9 @@ COPY . /src RUN /src/scripts/environment/install-protoc.sh # Initialize git repository for build scripts that need git metadata +# Remove minimal .git from build context first to avoid reinit issues RUN cd /src && \ + rm -rf .git && \ git init && \ git config user.email "ci@redhat.com" && \ git config user.name "CI" && \ diff --git a/Makefile b/Makefile index cd65c79a7699f..d2962014e5d6e 100644 --- a/Makefile +++ b/Makefile @@ -211,6 +211,7 @@ check-bans: .PHONY: build build: check-build-tools build: export CFLAGS += -g0 -O3 +build: export GIT_COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null) build: ## Build the project in release mode (Supports `ENVIRONMENT=true`) if [ "$(shell arch)" = "ppc64le" ]; then export CARGO_PROFILE_RELEASE_OPT_LEVEL=2; fi ${MAYBE_ENVIRONMENT_EXEC} cargo build --release --no-default-features --features ${FEATURES} @@ -219,6 +220,7 @@ build: ## Build the project in release mode (Supports `ENVIRONMENT=true`) .PHONY: build-offline build-offline: check-build-tools build-offline: export CFLAGS += -g0 -O3 +build-offline: export GIT_COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null) build-offline: ## Build the project in release mode (Supports `ENVIRONMENT=true`) if [ "$(shell arch)" = "ppc64le" ]; then export CARGO_PROFILE_RELEASE_OPT_LEVEL=2; fi ${MAYBE_ENVIRONMENT_EXEC} cargo build --release --no-default-features --features ${FEATURES} --offline @@ -648,6 +650,25 @@ package-rpm-aarch64: package-aarch64-unknown-linux-gnu ## Build the aarch64 rpm package-rpm-armv7hl-gnu: package-armv7-unknown-linux-gnueabihf ## Build the armv7hl-unknown-linux-gnueabihf rpm package TARGET=armv7-unknown-linux-gnueabihf ARCH=armv7hl $(VDEV) package rpm +##@ Docker + +# Detect platform for Docker builds +ifeq ($(shell uname),Darwin) + DOCKER_PLATFORM := --platform linux/amd64 +else + DOCKER_PLATFORM := +endif + +IMAGE_NAME ?= quay.io/openshift-logging/vector +IMAGE_TAG ?= $(shell git rev-parse --abbrev-ref HEAD) + +.PHONY: image +image: ## Build container image for local development (auto-detects platform) + podman build $(DOCKER_PLATFORM) \ + -t $(IMAGE_NAME):$(IMAGE_TAG) \ + -t $(IMAGE_NAME):latest \ + -f Dockerfile . + ##@ Releasing .PHONY: release diff --git a/build.rs b/build.rs index cf35846cbe61c..d321aaab12592 100644 --- a/build.rs +++ b/build.rs @@ -1,4 +1,11 @@ -use std::{collections::HashSet, env, fs::File, io::Write, path::Path, process::Command}; +use std::{ + collections::HashSet, + env, + fs::File, + io::{Read, Write}, + path::Path, + process::Command, +}; struct TrackedEnv { tracked: HashSet, @@ -91,17 +98,49 @@ impl BuildConstants { } } -fn git_short_hash() -> std::io::Result { - let output_result = Command::new("git") +fn git_short_hash() -> Option { + // 1. Try running git command (normal case - local builds with full .git) + if let Ok(output) = Command::new("git") .args(["rev-parse", "--short", "HEAD"]) - .output(); - - output_result.map(|output| { - let mut hash = String::from_utf8(output.stdout).expect("valid UTF-8"); + .output() + && output.status.success() + && let Ok(mut hash) = String::from_utf8(output.stdout) + { hash.retain(|c| !c.is_ascii_whitespace()); + if !hash.is_empty() { + return Some(hash); + } + } + + // 2. Try GIT_COMMIT environment variable (set by Makefile) + if let Ok(commit) = env::var("GIT_COMMIT") { + let trimmed = commit.trim().to_string(); + if !trimmed.is_empty() { + return Some(trimmed); + } + } - hash - }) + // 3. Fallback: read directly from .git files (Docker builds with minimal .git) + if let Ok(mut head_file) = File::open(".git/HEAD") { + let mut head_content = String::new(); + if head_file.read_to_string(&mut head_content).is_ok() { + // Parse "ref: refs/heads/branch-name" + if let Some(ref_path) = head_content.trim().strip_prefix("ref: ") { + let commit_file_path = format!(".git/{}", ref_path); + if let Ok(mut commit_file) = File::open(&commit_file_path) { + let mut commit_sha = String::new(); + if commit_file.read_to_string(&mut commit_sha).is_ok() { + let short_hash = commit_sha.trim().chars().take(10).collect::(); + if !short_hash.is_empty() { + return Some(short_hash); + } + } + } + } + } + } + + None } fn main() { @@ -192,18 +231,11 @@ fn main() { // Get the git short hash of the HEAD. // Note that if Vector is compiled within a container, proper git permissions must be set for - // the repo directory. + // the repo directory, or GIT_COMMIT environment variable should be passed (recommended for Docker builds). // In CI build workflows this will have been pre-configured by running the command // "git config --global --add safe.directory /git/vectordotdev/vector", from the vdev package // subcommands. - let git_short_hash = git_short_hash() - .map_err(|e| { - #[allow(clippy::print_stderr)] - { - eprintln!("Unable to determine git short hash from rev-parse command: {e}"); - } - }) - .expect("git hash detection failed"); + let git_short_hash = git_short_hash(); // Gather up the constants and write them out to our build constants file. let mut constants = BuildConstants::new(); @@ -249,7 +281,7 @@ fn main() { "Special build description, related to versioned releases.", build_desc, ); - constants.add_required_constant( + constants.add_optional_constant( "GIT_SHORT_HASH", "The short hash of the Git HEAD", git_short_hash, diff --git a/src/internal_events/process.rs b/src/internal_events/process.rs index cc3609737aea9..e0e0a3a2c13fa 100644 --- a/src/internal_events/process.rs +++ b/src/internal_events/process.rs @@ -16,6 +16,7 @@ impl InternalEvent for VectorStarted { version = built_info::PKG_VERSION, arch = built_info::TARGET_ARCH, revision = built_info::VECTOR_BUILD_DESC.unwrap_or(""), + git_commit = built_info::GIT_SHORT_HASH, ); counter!("started_total").increment(1); } diff --git a/src/lib.rs b/src/lib.rs index d4d4ab1eeb38a..846c968d97b88 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -191,7 +191,7 @@ pub fn vector_version() -> impl std::fmt::Display { format!( "{}-custom-{}", built_info::PKG_VERSION, - built_info::GIT_SHORT_HASH + built_info::GIT_SHORT_HASH.unwrap_or("unknown") ) } _ => built_info::PKG_VERSION.to_string(),