From 946cb29a6cafeed1d305041e284c14f05ad23b53 Mon Sep 17 00:00:00 2001 From: Adriano Di Luzio Date: Mon, 16 Dec 2024 15:13:37 +0100 Subject: [PATCH] Fix: revision in CI Docker image (#1119) * Fix: get correct `git` revision in CI Docker images * Correctly set build arguments * Make it an env variable as well * Check `rev-parse` exit code * Also check that it's not empty * Address nit --- .../actions/dockerfiles/Dockerfile.signer.debian | 11 +++++++---- .github/workflows/image-build.yaml | 2 ++ signer/build.rs | 15 +++++++++++---- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/.github/actions/dockerfiles/Dockerfile.signer.debian b/.github/actions/dockerfiles/Dockerfile.signer.debian index 601856433..b3ebe84ca 100644 --- a/.github/actions/dockerfiles/Dockerfile.signer.debian +++ b/.github/actions/dockerfiles/Dockerfile.signer.debian @@ -1,5 +1,8 @@ FROM rust:1.81.0-slim-bookworm AS build +ARG GIT_COMMIT +RUN test -n "$GIT_COMMIT" || (echo "GIT_COMMIT not set" && false) + ARG CARGO_BUILD_ARGS="--release" # Install dependencies. @@ -14,10 +17,10 @@ RUN apt-get install -y --no-install-recommends \ npm \ default-jre \ g++ && \ - apt-get clean && rm -rf /var/lib/apt/lists/* + apt-get clean && rm -rf /var/lib/apt/lists/* -RUN npm install -g pnpm@9 -RUN npm install -g @openapitools/openapi-generator-cli +RUN npm install -g pnpm@9 && \ + npm install -g @openapitools/openapi-generator-cli WORKDIR /code/sbtc COPY . . @@ -32,5 +35,5 @@ COPY --from=build /code/sbtc/target/debug/signer /usr/local/bin/signer # gettext provides envsubst RUN apt-get update && apt-get install -y ca-certificates gettext --no-install-recommends && \ - apt-get clean && rm -rf /var/lib/apt/lists/* + apt-get clean && rm -rf /var/lib/apt/lists/* CMD ["/usr/local/bin/signer --config /signer-config.toml --migrate-db"] diff --git a/.github/workflows/image-build.yaml b/.github/workflows/image-build.yaml index 0dc895103..d1a317bc9 100644 --- a/.github/workflows/image-build.yaml +++ b/.github/workflows/image-build.yaml @@ -96,3 +96,5 @@ jobs: labels: ${{ steps.docker_metadata.outputs.labels }} target: ${{ matrix.docker_target }} push: true + build-args: | + GIT_COMMIT=${{ github.ref_name }} diff --git a/signer/build.rs b/signer/build.rs index 68979e64d..dc567e585 100644 --- a/signer/build.rs +++ b/signer/build.rs @@ -1,3 +1,5 @@ +use std::env; + fn main() { set_up_build_info(); // compile_protos(); @@ -11,11 +13,16 @@ pub fn set_up_build_info() { let version = String::from_utf8_lossy(&output.stdout); - let git_hash = std::process::Command::new("git") + let git_output = std::process::Command::new("git") .args(["rev-parse", "HEAD"]) - .output() - .map(|output| String::from_utf8_lossy(&output.stdout).to_string()) - .unwrap_or_default(); + .output(); + + let git_hash = match git_output { + Ok(output) if output.status.success() && !output.stdout.is_empty() => { + String::from_utf8_lossy(&output.stdout).to_string() + } + _ => std::env::var("GIT_COMMIT").unwrap_or_default(), + }; let env_abi = std::env::var("CARGO_CFG_TARGET_ENV").unwrap(); let arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap();