From ce25bfa20948c79771d15d49d00ef43675e518ee Mon Sep 17 00:00:00 2001 From: Edouard Vanbelle Date: Tue, 2 Jun 2026 09:44:50 +0200 Subject: [PATCH] chore(ci): bring git hash and git branch on build --- build.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 7 +++++- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/build.rs b/build.rs index de115e8c..6627d670 100644 --- a/build.rs +++ b/build.rs @@ -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 { + 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 { + keys.iter() + .find_map(|k| env::var(k).ok()) + .filter(|s| !s.is_empty()) +} + // ═══════════════════════════════════════════════════════════════════════════════ // Release pipeline // ═══════════════════════════════════════════════════════════════════════════════ diff --git a/src/main.rs b/src/main.rs index c397174b..19a5130d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -133,7 +133,12 @@ async fn main() -> Result<(), Box> { 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();