Skip to content

Commit

Permalink
fix: attempt to use runtime variable in cross
Browse files Browse the repository at this point in the history
  • Loading branch information
merklefruit committed Dec 20, 2024
1 parent 8abbf66 commit c817a1f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion bolt-sidecar/Cross.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ pre-build = [

[build.env]
volumes = [
"GIT_DIR=../.git"
"GIT_DIR=./../.git"
]
12 changes: 12 additions & 0 deletions bolt-sidecar/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
fn main() {
// Built uses the `MANIFEST_DIR` environment variable to find the location of `Cargo.toml` and
// `.git`. When building from PWD=$BOLT_REPO/bolt-sidecar, the `.git` directory won't be
// found, therefore the git commit info will be missing from the build-time information.
//
// To work around this, we also attempt to read the commit hash from the `.git/FETCH_HEAD` file
// and make it available as an environment variable at runtime.

// make the commit hash available as an environment variable at runtime
if let Ok(commit_hash) = std::fs::read_to_string("../.git/FETCH_HEAD") {
println!("cargo:rustc-env=GIT_COMMIT_HASH={}", commit_hash.trim());
}

built::write_built_file().expect("Failed to acquire build-time information");
}
26 changes: 24 additions & 2 deletions bolt-sidecar/src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use lazy_static::lazy_static;

use crate::built_info;

/// Utilities for retrying a future with backoff.
pub mod backoff;

Expand All @@ -14,6 +16,26 @@ pub mod transactions;

lazy_static! {
/// The version of the Bolt sidecar binary.
pub static ref BOLT_SIDECAR_VERSION: String =
format!("v{}-{}", env!("CARGO_PKG_VERSION"), crate::built_info::GIT_COMMIT_HASH_SHORT.unwrap_or("unknown"));
///
/// Example format: "v0.1.0-alpha-abcdefg"
pub static ref BOLT_SIDECAR_VERSION: String = format!(
"v{}{}",
built_info::PKG_VERSION,
// Include the git commit hash, if available
built_info::GIT_COMMIT_HASH_SHORT.map(|s| format!("-{}", s)).unwrap_or_else(|| {
// If built info is not available, try the environment variable
let from_env = std::env::var("GIT_COMMIT_HASH").map(|s| {
// take only the first 7 characters of the full hash
format!("-{}", s.chars().take(7).collect::<String>())
});

// If the environment variable is not set either, return an empty string
if let Ok(s) = from_env {
s
} else {
println!("Warning: Could not determine the git commit hash");
String::new()
}
})
);
}

0 comments on commit c817a1f

Please sign in to comment.