chore(ci): bring git hash and git branch on build

This commit is contained in:
Edouard Vanbelle
2026-06-02 09:44:50 +02:00
parent a05e99175f
commit ce25bfa209
2 changed files with 68 additions and 1 deletions
+62
View File
@@ -13,9 +13,11 @@
//! **Debug mode** (`cargo build`):
//! • Copies HTML files to `$OUT_DIR` for `include_str!()` only.
use std::env;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::process::Command;
// ─── HTML files embedded via include_str!() in Rust source ───────────────────
const HTML_INCLUDE: &[&str] = &[
@@ -39,6 +41,8 @@ fn main() {
println!("cargo:rerun-if-changed=static");
println!("cargo:rerun-if-changed=build.rs");
git_status();
// ── Guard: Docker cacher stage has no static/ ────────────────────────────
if !static_dir.exists() {
for name in HTML_INCLUDE {
@@ -62,6 +66,64 @@ fn main() {
}
}
// ═══════════════════════════════════════════════════════════════════════════════
// Grab git values
// Support Github, is treated (need upgrade if move to gitlab CircleCI, ...)
// ═══════════════════════════════════════════════════════════════════════════════
fn git_status() {
// Rerun the build script when the commit or branch changes
println!("cargo:rerun-if-changed=.git/HEAD");
println!("cargo:rerun-if-changed=.git/refs/heads");
let git_hash = first_env(&["GITHUB_SHA", "CI_COMMIT_SHA", "CIRCLE_SHA1", "GIT_COMMIT"])
.or_else(|| git(&["rev-parse", "HEAD"]))
.unwrap_or_else(|| "unknown".into());
println!("cargo:rustc-env=GIT_HASH={git_hash}");
let git_branch = first_env(&[
"GITHUB_HEAD_REF", // GitHub: PR source branch (empty on push)
"GITHUB_REF_NAME", // GitHub: branch/tag on push
"CI_COMMIT_REF_NAME", // GitLab
"CIRCLE_BRANCH", // CircleCI
"GIT_BRANCH", // Jenkins
])
.or_else(|| git(&["rev-parse", "--abbrev-ref", "HEAD"]))
.filter(|b| b != "HEAD") // detached HEAD is not a real branch name
.unwrap_or_else(|| "unknown".into());
println!("cargo:rustc-env=GIT_BRANCH={git_branch}");
// CI builds: rerun if the injected env changes
for k in [
"GITHUB_SHA",
"GITHUB_HEAD_REF",
"GITHUB_REF_NAME",
"CI_COMMIT_SHA",
"CI_COMMIT_REF_NAME",
"CIRCLE_SHA1",
"CIRCLE_BRANCH",
"GIT_COMMIT",
"GIT_BRANCH",
] {
println!("cargo:rerun-if-env-changed={k}");
}
println!("cargo:warning=OxiCloud building with git hash: {git_hash} and branch: {git_branch}");
}
fn git(args: &[&str]) -> Option<String> {
let out = Command::new("git").args(args).output().ok()?;
out.status.success().then_some(())?;
let s = String::from_utf8(out.stdout).ok()?.trim().to_string();
(!s.is_empty()).then_some(s)
}
fn first_env(keys: &[&str]) -> Option<String> {
keys.iter()
.find_map(|k| env::var(k).ok())
.filter(|s| !s.is_empty())
}
// ═══════════════════════════════════════════════════════════════════════════════
// Release pipeline
// ═══════════════════════════════════════════════════════════════════════════════
+6 -1
View File
@@ -133,7 +133,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
oxicloud::interfaces::middleware::trusted_proxy::log_config();
tracing::info!("OxiCloud v{}", env!("CARGO_PKG_VERSION"));
tracing::info!(
"OxiCloud v{} | branch={} commit={}",
env!("CARGO_PKG_VERSION"),
env!("GIT_BRANCH"),
env!("GIT_HASH")
);
// Load configuration from environment variables
let config = common::config::AppConfig::from_env();