refactor: Move git version info gather in into a build script

This commit is contained in:
Jade Ellis 2025-05-01 00:38:35 +01:00 committed by Jacob Taylor
parent d8c03d43f4
commit f0b2a0c362
13 changed files with 93 additions and 135 deletions

View file

@ -142,10 +142,10 @@ jobs:
context: .
file: "docker/Dockerfile"
build-args: |
CONDUWUIT_VERSION_EXTRA=${{ env.COMMIT_SHORT_SHA }}
COMMIT_SHA=${{ github.sha }})
REMOTE_URL=${{github.event.repository.html_url }}
REMOTE_COMMIT_URL=${{github.event.head_commit.url }}
GIT_COMMIT_HASH=${{ github.sha }})
GIT_COMMIT_HASH_SHORT=${{ env.COMMIT_SHORT_SHA }})
GIT_REMOTE_URL=${{github.event.repository.html_url }}
GIT_REMOTE_COMMIT_URL=${{github.event.head_commit.url }}
platforms: ${{ matrix.platform }}
labels: ${{ steps.meta.outputs.labels }}
annotations: ${{ steps.meta.outputs.annotations }}

44
Cargo.lock generated
View file

@ -584,6 +584,9 @@ name = "built"
version = "0.7.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56ed6191a7e78c36abdb16ab65341eefd73d64d303fffccdbb00d51e4205967b"
dependencies = [
"cargo-lock",
]
[[package]]
name = "bumpalo"
@ -631,6 +634,19 @@ dependencies = [
"pkg-config",
]
[[package]]
name = "cargo-lock"
version = "10.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c06acb4f71407ba205a07cb453211e0e6a67b21904e47f6ba1f9589e38f2e454"
dependencies = [
"petgraph",
"semver",
"serde",
"toml",
"url",
]
[[package]]
name = "cargo_toml"
version = "0.21.0"
@ -856,6 +872,13 @@ dependencies = [
"tracing",
]
[[package]]
name = "conduwuit_build_metadata"
version = "0.5.0-rc.5"
dependencies = [
"built",
]
[[package]]
name = "conduwuit_core"
version = "0.5.0-rc.5"
@ -870,6 +893,7 @@ dependencies = [
"checked_ops",
"chrono",
"clap",
"conduwuit_build_metadata",
"conduwuit_macros",
"const-str",
"core_affinity",
@ -1019,6 +1043,7 @@ version = "0.5.0-rc.5"
dependencies = [
"askama",
"axum",
"conduwuit_build_metadata",
"futures",
"rand 0.8.5",
"thiserror 2.0.12",
@ -1515,6 +1540,12 @@ dependencies = [
"winapi",
]
[[package]]
name = "fixedbitset"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
[[package]]
name = "flate2"
version = "1.1.1"
@ -3132,6 +3163,16 @@ version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
[[package]]
name = "petgraph"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db"
dependencies = [
"fixedbitset",
"indexmap 2.8.0",
]
[[package]]
name = "phf"
version = "0.11.3"
@ -4127,6 +4168,9 @@ name = "semver"
version = "1.0.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56e6fa9c48d24d85fb3de5ad847117517440f6beceb7798af16b4a87d616b8d0"
dependencies = [
"serde",
]
[[package]]
name = "sentry"

View file

@ -631,6 +631,12 @@ package = "conduwuit_web"
path = "src/web"
default-features = false
[workspace.dependencies.conduwuit-build-metadata]
package = "conduwuit_build_metadata"
path = "src/build_metadata"
default-features = false
###############################################################################
#
# Release profiles

View file

@ -115,116 +115,26 @@ FROM toolchain AS builder
# Get source
COPY . .
# Conduwuit version info
ARG COMMIT_SHA=
ARG SHORT_COMMIT_SHA=
ARG REMOTE_URL=
ARG CONDUWUIT_VERSION_EXTRA=
ENV COMMIT_SHA=$COMMIT_SHA
ENV SHORT_COMMIT_SHA=$SHORT_COMMIT_SHA
ENV REMOTE_URL=$REMOTE_URL
ENV CONDUWUIT_VERSION_EXTRA=$CONDUWUIT_VERSION_EXTRA
# Calculate version info from git if not provided via ARGs
# and write all relevant vars to /etc/environment
RUN <<'EOF'
set -e # Exit on error
# Use temp variables to store calculated values
calculated_commit_sha=""
calculated_remote_url=""
calculated_version_extra=""
# --- COMMIT_SHA ---
# Calculate COMMIT_SHA if ENV var (from ARG) is empty
if [ -z "${COMMIT_SHA}" ]; then
# Try to get short commit hash from git
calculated_commit_sha=$(git rev-parse HEAD 2>/dev/null || echo "")
if [ -n "${calculated_commit_sha}" ]; then
echo "COMMIT_SHA='${calculated_commit_sha}'" >> /etc/environment
fi
else
# Ensure ARG-provided value is in /etc/environment
echo "COMMIT_SHA='${COMMIT_SHA}'" >> /etc/environment
fi
if [ -z "${SHORT_COMMIT_SHA}" ]; then
# Try to get short commit hash from git
calculated_short_commit_sha=$(git rev-parse --short HEAD 2>/dev/null || echo "")
if [ -n "${calculated_short_commit_sha}" ]; then
echo "SHORT_COMMIT_SHA='${calculated_short_commit_sha}'" >> /etc/environment
fi
else
# Ensure ARG-provided value is in /etc/environment
echo "SHORT_COMMIT_SHA='${SHORT_COMMIT_SHA}'" >> /etc/environment
fi
# --- REMOTE_URL ---
# Calculate REMOTE_URL if ENV var (from ARG) is empty
if [ -z "${REMOTE_URL}" ]; then
# Try to get remote origin URL from git
remote_url_raw=$(git config --get remote.origin.url 2>/dev/null || echo "")
if [ -n "${remote_url_raw}" ]; then
# Transform git URL (SSH or HTTPS) to web URL
if [[ $remote_url_raw == "https://"* ]]; then
# Already HTTPS, just remove .git suffix
calculated_remote_url=$(echo "$remote_url_raw" | sed 's/\.git$//')
else
# Convert SSH URL to HTTPS URL
calculated_remote_url=$(echo "$remote_url_raw" | sed 's/\.git$//' | sed 's/:/\//' | sed 's/^git@/https:\/\//')
fi
# Write calculated web URL if transformation was successful
if [ -n "${calculated_remote_url}" ]; then
echo "REMOTE_URL='${calculated_remote_url}'" >> /etc/environment
fi
fi
else
# Ensure ARG-provided value is in /etc/environment (assume it's a valid web URL)
echo "REMOTE_URL='${REMOTE_URL}'" >> /etc/environment
# Use provided value for REMOTE_COMMIT_URL calculation below
calculated_remote_url="${REMOTE_URL}"
fi
# --- Determine effective values for subsequent calculations ---
# Use ENV var value if set (from ARG), otherwise use calculated value
effective_commit_sha="${COMMIT_SHA:-$calculated_commit_sha}"
effective_short_commit_sha="${SHORT_COMMIT_SHA:-$calculated_short_commit_sha}"
effective_remote_url="${REMOTE_URL:-$calculated_remote_url}"
# --- REMOTE_COMMIT_URL ---
# Calculate and write REMOTE_COMMIT_URL if both components are available
if [ -z "${REMOTE_COMMIT_URL}" ] && [ -n "${effective_remote_url}" ] && [ -n "${effective_commit_sha}" ]; then
echo "REMOTE_COMMIT_URL='${effective_remote_url}/commit/${effective_commit_sha}'" >> /etc/environment
else
# Ensure ARG-provided value is in /etc/environment
echo "REMOTE_COMMIT_URL='${REMOTE_COMMIT_URL}'" >> /etc/environment
fi
# --- CONDUWUIT_VERSION_EXTRA ---
# Calculate CONDUWUIT_VERSION_EXTRA if ENV var (from ARG) is empty
if [ -z "${CONDUWUIT_VERSION_EXTRA}" ]; then
# Use the effective short commit sha, fallback to "unknown revision"
calculated_version_extra="${effective_short_commit_sha:-unknown revision}"
# Handle case where commit sha calculation failed and ARG wasn't set
if [ -z "${calculated_version_extra}" ]; then
calculated_version_extra="unknown revision"
fi
echo "CONDUWUIT_VERSION_EXTRA='${calculated_version_extra}'" >> /etc/environment
else
# Ensure ARG-provided value is in /etc/environment
echo "CONDUWUIT_VERSION_EXTRA='${CONDUWUIT_VERSION_EXTRA}'" >> /etc/environment
fi
EOF
ARG TARGETPLATFORM
# Verify environment configuration
RUN cat /etc/environment
RUN xx-cargo --print-target-triple
# Conduwuit version info
ARG GIT_COMMIT_HASH=
ARG GIT_COMMIT_HASH_SHORT=
ARG GIT_REMOTE_URL=
ARG GIT_REMOTE_COMMIT_URL=
ARG CONDUWUIT_VERSION_EXTRA=
ARG CONTINUWUITY_VERSION_EXTRA=
ENV GIT_COMMIT_HASH=$GIT_COMMIT_HASH
ENV GIT_COMMIT_HASH_SHORT=$GIT_COMMIT_HASH_SHORT
ENV GIT_REMOTE_URL=$GIT_REMOTE_URL
ENV GIT_REMOTE_COMMIT_URL=$GIT_REMOTE_COMMIT_URL
ENV CONDUWUIT_VERSION_EXTRA=$CONDUWUIT_VERSION_EXTRA
ENV CONTINUWUITY_VERSION_EXTRA=$CONTINUWUITY_VERSION_EXTRA
# Build the binary
RUN --mount=type=cache,target=/usr/local/cargo/registry \

View file

@ -130,9 +130,8 @@ buildDepsOnlyEnv =
});
buildPackageEnv = {
COMMIT_SHA = inputs.self.rev or inputs.self.dirtyRev or "";
SHORT_COMMIT_SHA = inputs.self.shortRev or inputs.self.dirtyShortRev or "";
CONDUWUIT_VERSION_EXTRA = inputs.self.shortRev or inputs.self.dirtyShortRev or "";
GIT_COMMIT_HASH = inputs.self.rev or inputs.self.dirtyRev or "";
GIT_COMMIT_HASH_SHORT = inputs.self.shortRev or inputs.self.dirtyShortRev or "";
} // buildDepsOnlyEnv // {
# Only needed in static stdenv because these are transitive dependencies of rocksdb
CARGO_BUILD_RUSTFLAGS = buildDepsOnlyEnv.CARGO_BUILD_RUSTFLAGS

View file

@ -143,7 +143,10 @@ pub(crate) async fn get_message_events_route(
if let Some(registration) = body.appservice_info.as_ref() {
<&DeviceId>::from(registration.registration.id.as_str())
} else {
panic!("No device_id provided and no appservice registration found, this should be unreachable");
panic!(
"No device_id provided and no appservice registration found, this \
should be unreachable"
);
},
},
room_id,

View file

@ -19,7 +19,7 @@ build = "build.rs"
path = "mod.rs"
crate-type = [
"rlib",
# "dylib",
# "dylib",
]
[features]
@ -28,7 +28,7 @@ crate-type = [
[dependencies]
[build-dependencies]
built = { version = "0.8", features = [] }
built = {version = "0.7", features = ["cargo-lock", "dependency-tree"]}
[lints]
workspace = true

View file

@ -78,13 +78,12 @@ fn main() {
}
// --- Rerun Triggers ---
// TODO: The git rerun triggers seem to always run
// // Rerun if the git HEAD changes
// println!("cargo:rerun-if-changed=.git/HEAD");
// // Rerun if the ref pointed to by HEAD changes (e.g., new commit on branch)
// if let Some(ref_path) = run_git_command(&["symbolic-ref", "--quiet", "HEAD"])
// { println!("cargo:rerun-if-changed=.git/{ref_path}");
// }
// Rerun if the git HEAD changes
println!("cargo:rerun-if-changed=.git/HEAD");
// Rerun if the ref pointed to by HEAD changes (e.g., new commit on branch)
if let Some(ref_path) = run_git_command(&["symbolic-ref", "--quiet", "HEAD"]) {
println!("cargo:rerun-if-changed=.git/{ref_path}");
}
println!("cargo:rerun-if-env-changed=GIT_COMMIT_HASH");
println!("cargo:rerun-if-env-changed=GIT_COMMIT_HASH_SHORT");

View file

@ -12,17 +12,11 @@ pub static VERSION_EXTRA: Option<&str> =
v
} else if let v @ Some(_) = option_env!("CONDUWUIT_VERSION_EXTRA") {
v
} else if let v @ Some(_) = option_env!("CONDUIT_VERSION_EXTRA") {
v
} else {
option_env!("CONDUIT_VERSION_EXTRA")
GIT_COMMIT_HASH_SHORT
};
#[must_use]
pub fn version_tag() -> Option<&'static str> {
VERSION_EXTRA
.filter(|s| !s.is_empty())
.or(GIT_COMMIT_HASH_SHORT)
}
pub static GIT_REMOTE_WEB_URL: Option<&str> = option_env!("GIT_REMOTE_WEB_URL");
pub static GIT_REMOTE_COMMIT_URL: Option<&str> = option_env!("GIT_REMOTE_COMMIT_URL");

View file

@ -26,6 +26,6 @@ pub fn user_agent() -> &'static str { USER_AGENT.get_or_init(init_user_agent) }
fn init_user_agent() -> String { format!("{}/{}", name(), version()) }
fn init_version() -> String {
conduwuit_build_metadata::version_tag()
conduwuit_build_metadata::VERSION_EXTRA
.map_or(SEMANTIC.to_owned(), |extra| format!("{SEMANTIC} ({extra})"))
}

View file

@ -20,6 +20,8 @@ crate-type = [
[dependencies]
conduwuit-build-metadata.workspace = true
askama = "0.14.0"
axum.workspace = true

View file

@ -5,6 +5,7 @@ use axum::{
response::{Html, IntoResponse, Response},
routing::get,
};
use conduwuit_build_metadata::{GIT_REMOTE_COMMIT_URL, GIT_REMOTE_WEB_URL, VERSION_EXTRA};
pub fn build<S>() -> Router<()> { Router::new().route("/", get(index_handler)) }

View file

@ -18,8 +18,8 @@
{%~ block footer ~%}
<footer>
<p>Powered by <a href="https://continuwuity.org">Continuwuity</a>
{%~ if let Some(version_info) = option_env!("CONDUWUIT_VERSION_EXTRA").or(option_env!("SHORT_COMMIT_SHA")) ~%}
{%~ if let Some(url) = option_env!("REMOTE_COMMIT_URL").or(option_env!("REMOTE_URL")) ~%}
{%~ if let Some(version_info) = VERSION_EXTRA ~%}
{%~ if let Some(url) = GIT_REMOTE_COMMIT_URL.or(GIT_REMOTE_WEB_URL) ~%}
(<a href="{{ url }}">{{ version_info }}</a>)
{%~ else ~%}
({{ version_info }})