Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .github/actions/deploy-ecs/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ runs:
push: true
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: GIT_VERSION=${{ github.sha }}
build-args: GIT_SHA=${{ github.sha }}

- name: Kick ECS to deploy new version
shell: bash
Expand Down
61 changes: 38 additions & 23 deletions crates/lib/docs_rs_utils/build.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
use anyhow::Result;
use std::env;
use anyhow::{Context as _, Result, anyhow};
use std::{env, path::PathBuf, process::Command};

fn main() -> Result<()> {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-env-changed=GIT_SHA");
read_git_version()?;
Ok(())
}

fn read_git_version() -> Result<()> {
if let Ok(v) = env::var("GIT_SHA") {
// first try to read an externally provided git SAH, e.g., from CI
// first try to read an externally provided git SHA, e.g., from CI
println!("cargo:rustc-env=GIT_SHA={v}");
} else {
if let Some(path) = git_head_ref_path().context("error trying to get git head ref path")? {
println!("cargo:rerun-if-changed={}", path.display());
}

// then try to read the git repo.
let maybe_hash = get_git_hash()?;
let maybe_hash = match git_output(["rev-parse", "--short", "HEAD"]) {
Ok(hash) => Some(hash),
Err(err) => {
eprintln!("error trying to get git head ref path: {:?}", err);
None
}
};

let git_hash = maybe_hash.as_deref().unwrap_or("???????");
println!("cargo:rustc-env=GIT_SHA={git_hash}");
}
Expand All @@ -25,27 +38,29 @@ fn read_git_version() -> Result<()> {
Ok(())
}

fn get_git_hash() -> Result<Option<String>> {
use std::process::Command;

fn git_head_ref_path() -> Result<Option<PathBuf>> {
let git_dir = git_output(["rev-parse", "--git-dir"])?;
let output = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output();
.args(["symbolic-ref", "-q", "HEAD"])
.output()?;

if output.status.success() {
let head_ref = String::from_utf8_lossy(&output.stdout).trim().to_string();
Ok(Some(PathBuf::from(git_dir).join(head_ref)))
} else {
Ok(None)
}
}

match output {
Ok(output) if output.status.success() => {
let hash = String::from_utf8(output.stdout)?.trim().to_string();
fn git_output<const N: usize>(args: [&str; N]) -> Result<String> {
let output = Command::new("git").args(args).output()?;

Ok(Some(hash))
}
Ok(output) => {
let err = String::from_utf8_lossy(&output.stderr);
eprintln!("failed to get git repo: {}", err.trim());
Ok(None)
}
Err(err) => {
eprintln!("failed to execute git: {err}");
Ok(None)
}
if output.status.success() {
Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
} else {
Err(
anyhow!(String::from_utf8_lossy(&output.stderr).trim().to_string())
.context(format!("error running git command: {:?}", args)),
)
}
}
Loading