-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathbuild_shared.rs
More file actions
59 lines (54 loc) · 2.17 KB
/
Copy pathbuild_shared.rs
File metadata and controls
59 lines (54 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::process::Command;
fn main() {
// Git SHA: prefer CI-provided env, then invoke git, fallback to "unknown"
let git_sha = std::env::var("TEMPO_GIT_SHA").unwrap_or_else(|_| {
Command::new("git")
.args(["rev-parse", "--short=7", "HEAD"])
.output()
.ok()
.and_then(|o| {
if o.status.success() {
String::from_utf8(o.stdout)
.ok()
.map(|s| s.trim().to_string())
} else {
None
}
})
.unwrap_or_else(|| "unknown".to_string())
});
// Build date: prefer CI-provided env, fallback to current UTC via `date`
let build_date = std::env::var("TEMPO_BUILD_DATE").unwrap_or_else(|_| {
Command::new("date")
.args(["-u", "+%Y-%m-%dT%H:%M:%SZ"])
.output()
.ok()
.and_then(|o| {
if o.status.success() {
String::from_utf8(o.stdout)
.ok()
.map(|s| s.trim().to_string())
} else {
None
}
})
.unwrap_or_else(|| "unknown".to_string())
});
// Build profile: prefer CI-provided env, otherwise "local" for dev builds
let profile =
std::env::var("TEMPO_BUILD_PROFILE").unwrap_or_else(|_| "local".to_string());
println!("cargo:rustc-env=TEMPO_GIT_SHA={git_sha}");
println!("cargo:rustc-env=TEMPO_BUILD_DATE={build_date}");
println!("cargo:rustc-env=TEMPO_BUILD_PROFILE={profile}");
// Re-run when build script, git HEAD, or CI env vars change
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=../../.git/HEAD");
if let Ok(head) = std::fs::read_to_string("../../.git/HEAD") {
if let Some(ref_path) = head.strip_prefix("ref: ") {
println!("cargo:rerun-if-changed=../../.git/{}", ref_path.trim());
}
}
println!("cargo:rerun-if-env-changed=TEMPO_GIT_SHA");
println!("cargo:rerun-if-env-changed=TEMPO_BUILD_DATE");
println!("cargo:rerun-if-env-changed=TEMPO_BUILD_PROFILE");
}