diff --git a/build.rs b/build.rs index 2689b98..d9e9de4 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,5 @@ use anyhow::{bail, Context}; -use std::env; +use std::env::{var, VarError}; use std::io::ErrorKind; use std::process::{Command, Stdio}; @@ -7,7 +7,11 @@ fn main() -> anyhow::Result<()> { println!("cargo:rerun-if-changed=.git/HEAD"); println!("cargo:rerun-if-changed=.git/refs"); let pkg_version = getenv("CARGO_PKG_VERSION")?; - match get_commit_hash()? { + let mut commit = get_commit_hash()?; + if commit.is_none() { + commit = get_heroku_slug_commit()?; + } + match commit { Some(commit) => { println!("cargo:rustc-env=GIT_COMMIT={commit}"); println!("cargo:rustc-env=VERSION_WITH_GIT={pkg_version} (commit: {commit})"); @@ -58,6 +62,14 @@ fn get_commit_hash() -> anyhow::Result> { } } +fn get_heroku_slug_commit() -> anyhow::Result> { + match var("HEROKU_SLUG_COMMIT") { + Ok(slug) => Ok(Some(slug.chars().take(7).collect())), + Err(VarError::NotPresent) => Ok(None), + Err(VarError::NotUnicode(_)) => bail!("HEROKU_SLUG_COMMIT is not UTF-8"), + } +} + fn getenv(name: &str) -> anyhow::Result { - env::var(name).with_context(|| format!("{name} envvar not set")) + var(name).with_context(|| format!("{name} envvar not set")) }