Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions Dockerfile.unit
Original file line number Diff line number Diff line change
Expand Up @@ -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" && \
Expand Down
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
65 changes: 46 additions & 19 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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<String>,
Expand Down Expand Up @@ -91,17 +91,51 @@ impl BuildConstants {
}
}

fn git_short_hash() -> std::io::Result<String> {
let output_result = Command::new("git")
fn git_short_hash() -> Option<String> {
// 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()
{
if output.status.success() {
if let Ok(mut hash) = String::from_utf8(output.stdout) {
hash.retain(|c| !c.is_ascii_whitespace());
if !hash.is_empty() {
return Some(hash);
}
}
}
}

output_result.map(|output| {
let mut hash = String::from_utf8(output.stdout).expect("valid UTF-8");
hash.retain(|c| !c.is_ascii_whitespace());
// 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::<String>();
if !short_hash.is_empty() {
return Some(short_hash);
}
}
}
}
}
}

None
}

fn main() {
Expand Down Expand Up @@ -192,18 +226,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();
Expand Down Expand Up @@ -249,7 +276,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,
Expand Down
1 change: 1 addition & 0 deletions src/internal_events/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down