Skip to content

Commit

Permalink
ci: use env variables to get git commit hash in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
heeckhau committed Oct 11, 2024
1 parent 1d4c50f commit 66db534
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions crates/notary/server/build.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
use std::process::Command;
use std::{env, process::Command};

fn main() {
// Used to extract latest HEAD commit hash and timestamp for the /info endpoint
let output = Command::new("git")
.args(["show", "HEAD", "-s", "--format=%H,%cI"])
.output()
.expect("Git command to get commit hash and timestamp should work during build process");
if env::var("GIT_COMMIT_HASH").is_err() || env::var("GIT_COMMIT_TIMESTAMP").is_err() {
// Used to extract latest HEAD commit hash and timestamp for the /info endpoint
let output = Command::new("git")
.args(["show", "HEAD", "-s", "--format=%H,%cI"])
.output()
.expect(
"Git command to get commit hash and timestamp should work during build process",
);

let output_string =
String::from_utf8(output.stdout).expect("Git command should produce valid string output");
let output_string = String::from_utf8(output.stdout)
.expect("Git command should produce valid string output");

let (commit_hash, commit_timestamp) = output_string
.as_str()
.split_once(',')
.expect("Git commit hash and timestamp string output should be comma separated");
let (commit_hash, commit_timestamp) = output_string
.as_str()
.split_once(',')
.expect("Git commit hash and timestamp string output should be comma separated");

// Pass these 2 values as env var to the program
println!("cargo:rustc-env=GIT_COMMIT_HASH={}", commit_hash);
println!("cargo:rustc-env=GIT_COMMIT_TIMESTAMP={}", commit_timestamp);
// Pass these 2 values as env var to the program
println!("cargo:rustc-env=GIT_COMMIT_HASH={}", commit_hash);
println!("cargo:rustc-env=GIT_COMMIT_TIMESTAMP={}", commit_timestamp);
}
}

0 comments on commit 66db534

Please sign in to comment.