Skip to content

Commit

Permalink
Feat(service): add current commit at build time to /info (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
SHAcollision authored Oct 8, 2024
1 parent 3f00cf0 commit d113d4c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ homepage = "https://github.com/pubky"
repository = "https://github.com/pubky/pubky-nexus"
license = "MIT"
default-run = "service"
build = "build.rs"

[dependencies]
pkarr = { git = "https://github.com/Pubky/pkarr", branch = "v3", package = "pkarr", features = [
Expand Down
19 changes: 19 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::process::Command;

fn main() {
// Get the latest commit hash
let output = Command::new("git")
.args(["rev-parse", "HEAD"])
.output()
.expect("Failed to execute git");

let git_hash = String::from_utf8(output.stdout).expect("Invalid UTF-8 sequence");
let git_hash = git_hash.trim();

// Write the commit hash to an environment variable
println!("cargo:rustc-env=GIT_COMMIT_HASH={}", git_hash);

// Re-run build.rs if .git/HEAD changes
println!("cargo:rerun-if-changed=.git/HEAD");
println!("cargo:rerun-if-changed=.git/refs/heads");
}
4 changes: 4 additions & 0 deletions src/models/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct ServerInfo {
pub name: String,
pub repository: String,
pub version: String,
pub commit_hash: String,
pub last_index_snapshot: u64,
pub base_file_url: String,
}
Expand All @@ -26,6 +27,9 @@ impl ServerInfo {
name: env!("CARGO_PKG_NAME").to_string(),
repository: env!("CARGO_PKG_REPOSITORY").to_string(),
version: env!("CARGO_PKG_VERSION").to_string(),
commit_hash: option_env!("GIT_COMMIT_HASH")
.unwrap_or("unknown")
.to_string(),
last_index_snapshot,
base_file_url: config.base_file_url,
}
Expand Down

0 comments on commit d113d4c

Please sign in to comment.