Compile Git SHA Into Rust

It often make sense to compile the Git SHA into the binary to be able to debug issues.

To achieve this in Rust , add the following to your

use anyhow::Result;
use std::process::Command;

fn main() -> Result<()> {
    let output = Command::new("git")
	.args(&["rev-parse", "--short", "HEAD"])
    let git_hash = String::from_utf8(output.stdout)?;
    println!("cargo:rustc-env=GIT_SHA={}", git_hash);


Printing cargo:rustc-env=MY_ENV_KEY=<my-env-value> from your will make it available in your application with env!("MY_ENV_KEY") (which resolves to an ENV at compile time).

Edit this page on GitHub