use std::process::Command;
fn main() {
let commit_short = git(&["rev-parse", "--short", "HEAD"]);
println!("cargo:rustc-env=DEVBOY_BUILD_COMMIT={commit_short}");
let commit_full = git(&["rev-parse", "HEAD"]);
println!("cargo:rustc-env=DEVBOY_BUILD_COMMIT_FULL={commit_full}");
let dirty = !git(&["status", "--porcelain"]).is_empty();
println!("cargo:rustc-env=DEVBOY_BUILD_DIRTY={dirty}");
let timestamp = chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true);
println!("cargo:rustc-env=DEVBOY_BUILD_TIMESTAMP={timestamp}");
let ci_run_id = std::env::var("GITHUB_RUN_ID")
.or_else(|_| std::env::var("CI_PIPELINE_ID"))
.unwrap_or_default();
println!("cargo:rustc-env=DEVBOY_BUILD_CI_RUN_ID={ci_run_id}");
println!("cargo:rerun-if-changed=../../.git/HEAD");
println!("cargo:rerun-if-changed=../../.git/refs/");
println!("cargo:rerun-if-changed=../../.git/packed-refs");
println!("cargo:rerun-if-changed=../../.git/index");
}
fn git(args: &[&str]) -> String {
Command::new("git")
.args(args)
.output()
.ok()
.and_then(|o| {
if o.status.success() {
String::from_utf8(o.stdout).ok()
} else {
None
}
})
.unwrap_or_default()
.trim()
.to_string()
}