Skip to content

Commit

Permalink
feat: display git-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lgalabru committed Feb 9, 2024
1 parent 9c9e2a5 commit 3256427
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Expand Up @@ -109,6 +109,8 @@ jobs:
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
file: ./dockerfiles/components/ordhook.dockerfile
build-args: |
GIT_COMMIT=${{ env.GITHUB_SHA_SHORT }}
cache-from: type=gha
cache-to: type=gha,mode=max
# Only push if (there's a new release on main branch, or if building a non-main branch) and (Only run on non-PR events or only PRs that aren't from forks)
Expand Down
29 changes: 25 additions & 4 deletions components/ordhook-cli/build.rs
@@ -1,8 +1,29 @@
use std::process::Command;

fn current_git_hash() -> Option<String> {
if option_env!("GIT_COMMIT") == None {
let commit = Command::new("git")
.arg("log")
.arg("-1")
.arg("--pretty=format:%h") // Abbreviated commit hash
.current_dir(env!("CARGO_MANIFEST_DIR"))
.output();

if let Ok(commit) = commit {
if let Ok(commit) = String::from_utf8(commit.stdout) {
return Some(commit);
}
}
} else {
return option_env!("GIT_COMMIT").map(String::from);
}

None
}

fn main() {
// note: add error checking yourself.
let output = Command::new("git").args(&["rev-parse", "HEAD"]).output().unwrap();
let git_hash = String::from_utf8(output.stdout).unwrap();
println!("cargo:rustc-env=GIT_HASH={}", git_hash);
}
if let Some(git) = current_git_hash() {
println!("cargo:rustc-env=GIT_COMMIT={}", git);
}
}
7 changes: 5 additions & 2 deletions components/ordhook-cli/src/cli/mod.rs
Expand Up @@ -728,8 +728,11 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> {
}

let ordhook_config = config.get_ordhook_config();
let version = env!("GIT_HASH");
info!(ctx.expect_logger(), "Starting service (git_hash = {})...", version);
let version = env!("GIT_COMMIT");
info!(
ctx.expect_logger(),
"Starting service (git_commit = {})...", version
);

let start_block = match cmd.start_at_block {
Some(entry) => entry,
Expand Down
2 changes: 2 additions & 0 deletions dockerfiles/components/ordhook.dockerfile
@@ -1,5 +1,7 @@
FROM rust:bullseye as build

ARG GIT_COMMIT='0000000'

WORKDIR /src

RUN apt-get update && apt-get install -y ca-certificates pkg-config libssl-dev libclang-11-dev libunwind-dev libunwind8 curl gnupg
Expand Down

0 comments on commit 3256427

Please sign in to comment.