Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch Git commit hash from HEROKU_SLUG_COMMIT if Git info not available #95

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use anyhow::{bail, Context};
use std::env;
use std::env::{var, VarError};
use std::io::ErrorKind;
use std::process::{Command, Stdio};

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})");
Expand Down Expand Up @@ -58,6 +62,14 @@ fn get_commit_hash() -> anyhow::Result<Option<String>> {
}
}

fn get_heroku_slug_commit() -> anyhow::Result<Option<String>> {
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<String> {
env::var(name).with_context(|| format!("{name} envvar not set"))
var(name).with_context(|| format!("{name} envvar not set"))
}
Loading