From 1776dbda16224e5f5cf3523b87264ac7eacee2c3 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Fri, 18 Apr 2025 15:20:36 +0200 Subject: [PATCH] Include git based version --- .cargo/config.toml | 2 ++ .gitea/workflows/build.yaml | 16 +++++++--------- Cargo.lock | 21 +++++++++++++++++++++ Cargo.toml | 1 + Dockerfile | 3 ++- src/lib.rs | 4 ++++ src/main.rs | 3 ++- src/version.rs | 11 +++++++++++ 8 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 .cargo/config.toml create mode 100644 src/version.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..7520df8 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[env] +RUSTC_BOOTSTRAP = "1" diff --git a/.gitea/workflows/build.yaml b/.gitea/workflows/build.yaml index 6abc384..cb6f4e1 100644 --- a/.gitea/workflows/build.yaml +++ b/.gitea/workflows/build.yaml @@ -17,17 +17,13 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 - with: - fetch-depth: 0 - fetch-tags: true - # TODO: Actually set an env variable and use it in the application - - name: Set version string + - name: Set timestamp and release version run: | - git describe --always --dirty="-modified" - - - name: Get Git commit timestamps - run: echo "TIMESTAMP=$(git log -1 --pretty=%ct)" >> $GITHUB_ENV + echo "TIMESTAMP=$(git log -1 --pretty=%ct)" >> $GITHUB_ENV + git fetch --prune --unshallow --tags --force + echo "RELEASE_VERSION=$(git describe --always --dirty='--modified')" >> $GITHUB_ENV + cat $GITHUB_ENV - name: Login to registry uses: docker/login-action@v3 @@ -72,6 +68,8 @@ jobs: annotations: ${{ steps.meta.outputs.annotations }} cache-from: type=gha cache-to: type=gha,mode=max + build-args: | + "RELEASE_VERSION=${{ env.RELEASE_VERSION }}" env: SOURCE_DATE_EPOCH: ${{ env.TIMESTAMP }} diff --git a/Cargo.lock b/Cargo.lock index 0fef2fa..0ee942c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -97,6 +97,7 @@ dependencies = [ "color-eyre", "dotenvy", "futures-util", + "git-version", "k8s-openapi", "kube", "schemars", @@ -555,6 +556,26 @@ version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" +[[package]] +name = "git-version" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad568aa3db0fcbc81f2f116137f263d7304f512a1209b35b85150d3ef88ad19" +dependencies = [ + "git-version-macro", +] + +[[package]] +name = "git-version-macro" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53010ccb100b96a67bc32c0175f0ed1426b31b655d562898e57325f81c023ac0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "gloo-timers" version = "0.3.0" diff --git a/Cargo.toml b/Cargo.toml index 2dca49e..59204d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ default-run = "authelia-controller" color-eyre = "0.6.3" dotenvy = "0.15.7" futures-util = "0.3.31" +git-version = "0.3.9" k8s-openapi = { version = "0.24.0", features = ["v1_31"] } kube = { version = "0.99.0", features = ["derive", "runtime"] } schemars = "0.8.22" diff --git a/Dockerfile b/Dockerfile index d05d8a4..12bfbcc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,8 @@ COPY --from=planner /app/recipe.json recipe.json RUN cargo chef cook --release --recipe-path recipe.json COPY . . -ENV RUSTC_BOOTSTRAP=1 +ARG RELEASE_VERSION +ENV RELEASE_VERSION=${RELEASE_VERSION} RUN cargo auditable build --release FROM gcr.io/distroless/cc-debian12:nonroot AS runtime diff --git a/src/lib.rs b/src/lib.rs index 75a410e..8687f0d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,6 @@ +#![feature(let_chains)] pub mod context; pub mod resources; +mod version; + +pub use version::VERSION; diff --git a/src/main.rs b/src/main.rs index a286147..b6eaa31 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use std::sync::Arc; use std::time::Duration; +use authelia_controller::VERSION; use authelia_controller::context::Context; use authelia_controller::resources::AccessControlRule; use color_eyre::eyre::Context as _; @@ -39,7 +40,7 @@ async fn main() -> color_eyre::Result<()> { }) .unwrap_or(Ok(15))?; - info!("Starting"); + info!(version = VERSION, "Starting"); let client = Client::try_default().await?; let access_control_rules = Api::::all(client.clone()); diff --git a/src/version.rs b/src/version.rs new file mode 100644 index 0000000..8eb59de --- /dev/null +++ b/src/version.rs @@ -0,0 +1,11 @@ +pub const VERSION: &str = get_version(); + +const fn get_version() -> &'static str { + if let Some(version) = std::option_env!("RELEASE_VERSION") + && !version.is_empty() + { + version + } else { + git_version::git_version!(fallback = "unknown") + } +}