fix: properly inject version into Docker image builds

The agent version was always showing 0.1.0 because build.rs tried to
run 'git describe' inside the Docker build, but .git directory wasn't
copied into the image.

Changes:
- Add VERSION build arg to Dockerfile
- Pass VERSION as env var to cargo build commands
- Update GitLab CI to pass --build-arg VERSION
- Modify build.rs to prefer BUILD_VERSION env var over git commands

This ensures the version displayed by the agent matches the Docker
image tag it was built with.
This commit is contained in:
Graham McIntire 2026-01-19 15:31:26 -06:00
parent da3088f87f
commit c0fe57cd44
No known key found for this signature in database
4 changed files with 14 additions and 84 deletions

View file

@ -77,6 +77,7 @@ build:main:
- | - |
docker buildx build \ docker buildx build \
--platform linux/amd64 \ --platform linux/amd64 \
--build-arg VERSION=$VERSION \
--cache-from type=registry,ref=$REGISTRY/$IMAGE_NAME:buildcache \ --cache-from type=registry,ref=$REGISTRY/$IMAGE_NAME:buildcache \
--cache-to type=registry,ref=$REGISTRY/$IMAGE_NAME:buildcache,mode=max \ --cache-to type=registry,ref=$REGISTRY/$IMAGE_NAME:buildcache,mode=max \
--tag $REGISTRY/$IMAGE_NAME:latest \ --tag $REGISTRY/$IMAGE_NAME:latest \
@ -116,6 +117,7 @@ release:multiarch:
docker buildx build \ docker buildx build \
--platform linux/amd64,linux/arm64 \ --platform linux/amd64,linux/arm64 \
--build-arg VERSION=$VERSION \
--cache-from type=registry,ref=$REGISTRY/$IMAGE_NAME:buildcache \ --cache-from type=registry,ref=$REGISTRY/$IMAGE_NAME:buildcache \
--cache-to type=registry,ref=$REGISTRY/$IMAGE_NAME:buildcache,mode=max \ --cache-to type=registry,ref=$REGISTRY/$IMAGE_NAME:buildcache,mode=max \
--tag $REGISTRY/$IMAGE_NAME:$VERSION \ --tag $REGISTRY/$IMAGE_NAME:$VERSION \

82
Cargo.lock generated
View file

@ -8,18 +8,6 @@ version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
[[package]]
name = "ahash"
version = "0.8.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75"
dependencies = [
"cfg-if",
"once_cell",
"version_check",
"zerocopy",
]
[[package]] [[package]]
name = "aho-corasick" name = "aho-corasick"
version = "1.1.4" version = "1.1.4"
@ -315,18 +303,6 @@ dependencies = [
"windows-sys 0.61.2", "windows-sys 0.61.2",
] ]
[[package]]
name = "fallible-iterator"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649"
[[package]]
name = "fallible-streaming-iterator"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
[[package]] [[package]]
name = "fastrand" name = "fastrand"
version = "2.3.0" version = "2.3.0"
@ -486,30 +462,12 @@ dependencies = [
"wasip2", "wasip2",
] ]
[[package]]
name = "hashbrown"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
dependencies = [
"ahash",
]
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.16.1" version = "0.16.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100"
[[package]]
name = "hashlink"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ba4ff7128dee98c7dc9794b6a411377e1404dba1c97deb8d1a55297bd25d8af"
dependencies = [
"hashbrown 0.14.5",
]
[[package]] [[package]]
name = "heck" name = "heck"
version = "0.5.0" version = "0.5.0"
@ -682,7 +640,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7714e70437a7dc3ac8eb7e6f8df75fd8eb422675fc7678aff7364301092b1017" checksum = "7714e70437a7dc3ac8eb7e6f8df75fd8eb422675fc7678aff7364301092b1017"
dependencies = [ dependencies = [
"equivalent", "equivalent",
"hashbrown 0.16.1", "hashbrown",
] ]
[[package]] [[package]]
@ -722,17 +680,6 @@ version = "0.2.180"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc" checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc"
[[package]]
name = "libsqlite3-sys"
version = "0.30.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e99fb7a497b1e3339bc746195567ed8d3e24945ecd636e3619d20b9de9e9149"
dependencies = [
"cc",
"pkg-config",
"vcpkg",
]
[[package]] [[package]]
name = "linux-raw-sys" name = "linux-raw-sys"
version = "0.11.0" version = "0.11.0"
@ -838,12 +785,6 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pkg-config"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
[[package]] [[package]]
name = "potential_utf" name = "potential_utf"
version = "0.1.4" version = "0.1.4"
@ -1021,20 +962,6 @@ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "rusqlite"
version = "0.32.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7753b721174eb8ff87a9a0e799e2d7bc3749323e773db92e0984debb00019d6e"
dependencies = [
"bitflags",
"fallible-iterator",
"fallible-streaming-iterator",
"hashlink",
"libsqlite3-sys",
"smallvec",
]
[[package]] [[package]]
name = "rustix" name = "rustix"
version = "1.1.3" version = "1.1.3"
@ -1375,7 +1302,6 @@ dependencies = [
"prost-build", "prost-build",
"prost-types", "prost-types",
"rand", "rand",
"rusqlite",
"serde", "serde",
"serde_json", "serde_json",
"snmp", "snmp",
@ -1474,12 +1400,6 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.5" version = "0.9.5"

View file

@ -3,6 +3,7 @@ FROM rust:1.83-alpine AS builder
# Build arguments provided by Docker buildx # Build arguments provided by Docker buildx
ARG TARGETPLATFORM ARG TARGETPLATFORM
ARG VERSION=0.1.0-unknown
WORKDIR /app WORKDIR /app
@ -27,7 +28,7 @@ RUN mkdir src && echo "fn main() {}" > src/main.rs
# Build dependencies (cached layer) # Build dependencies (cached layer)
RUN RUST_TARGET=$(cat /tmp/rust-target) && \ RUN RUST_TARGET=$(cat /tmp/rust-target) && \
cargo build --release --target "$RUST_TARGET" BUILD_VERSION="$VERSION" cargo build --release --target "$RUST_TARGET"
# Remove dummy src # Remove dummy src
RUN rm -rf src RUN rm -rf src
@ -38,7 +39,7 @@ COPY src ./src
# Build the actual application # Build the actual application
RUN RUST_TARGET=$(cat /tmp/rust-target) && \ RUN RUST_TARGET=$(cat /tmp/rust-target) && \
touch src/main.rs && \ touch src/main.rs && \
cargo build --release --target "$RUST_TARGET" && \ BUILD_VERSION="$VERSION" cargo build --release --target "$RUST_TARGET" && \
cp "target/$RUST_TARGET/release/towerops-agent" /tmp/towerops-agent cp "target/$RUST_TARGET/release/towerops-agent" /tmp/towerops-agent
# Runtime stage # Runtime stage

View file

@ -11,7 +11,14 @@ fn main() {
} }
fn get_version() -> String { fn get_version() -> String {
// Try git describe first - gives us the most descriptive version // First check if BUILD_VERSION is already set (e.g., from Docker build arg)
if let Ok(version) = std::env::var("BUILD_VERSION") {
if !version.is_empty() && version != "0.1.0-unknown" {
return version;
}
}
// Try git describe - gives us the most descriptive version
// Examples: // Examples:
// v0.2.0 -> "0.2.0" (exact tag) // v0.2.0 -> "0.2.0" (exact tag)
// v0.2.0-5-g831588e -> "0.2.0.5.831588e" (5 commits after v0.2.0) // v0.2.0-5-g831588e -> "0.2.0.5.831588e" (5 commits after v0.2.0)