Skip to content

Commit c9b89a9

Browse files
committed
Fix reporting of version in prebuilt binaries & docker image
1 parent 3a18200 commit c9b89a9

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

.github/workflows/build.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,37 @@ env:
3131
DOCKER_METADATA_ANNOTATIONS_LEVELS: manifest,index
3232

3333
jobs:
34+
compute-version:
35+
name: Compute version using git describe
36+
runs-on: ubuntu-24.04
37+
outputs:
38+
describe: ${{ steps.git.outputs.describe }}
39+
timestamp: ${{ steps.git.outputs.timestamp }}
40+
steps:
41+
- name: Checkout the code
42+
uses: actions/checkout@v4.2.2
43+
with:
44+
# Need a full clone so that `git describe` reports the right version
45+
fetch-depth: 0
46+
47+
- name: Compute version and timestamp out of git history
48+
id: git
49+
run: |
50+
echo "describe=$(git describe --tags --match 'v*.*.*' --always)" >> $GITHUB_OUTPUT
51+
echo "timestamp=$(git log -1 --format=%ct)" >> $GITHUB_OUTPUT
52+
53+
3454
build-binaries:
3555
name: Build binaries
3656
runs-on: ubuntu-22.04
3757

58+
needs:
59+
- compute-version
60+
61+
env:
62+
VERGEN_GIT_DESCRIBE: ${{ needs.compute-version.outputs.describe }}
63+
SOURCE_DATE_EPOCH: ${{ needs.compute-version.outputs.timestamp }}
64+
3865
permissions:
3966
contents: read
4067

@@ -136,6 +163,13 @@ jobs:
136163
packages: write
137164
id-token: write
138165

166+
needs:
167+
- compute-version
168+
169+
env:
170+
VERGEN_GIT_DESCRIBE: ${{ needs.compute-version.outputs.describe }}
171+
SOURCE_DATE_EPOCH: ${{ needs.compute-version.outputs.timestamp }}
172+
139173
steps:
140174
- name: Docker meta
141175
id: meta

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ RUN --network=default \
142142
# Build the rest
143143
COPY ./ /app
144144
ENV SQLX_OFFLINE=true
145+
146+
ARG VERGEN_GIT_DESCRIBE
147+
ENV VERGEN_GIT_DESCRIBE=${VERGEN_GIT_DESCRIBE}
148+
145149
# Network access: cargo auditable needs it
146150
RUN --network=default \
147151
cargo auditable build \

crates/cli/build.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1-
// Copyright 2024 New Vector Ltd.
1+
// Copyright 2024, 2025 New Vector Ltd.
22
//
33
// SPDX-License-Identifier: AGPL-3.0-only
44
// Please see LICENSE in the repository root for full details.
55

66
use vergen_gitcl::{Emitter, GitclBuilder, RustcBuilder};
77

88
fn main() -> anyhow::Result<()> {
9-
let gitcl = GitclBuilder::default().describe(true, true, None).build()?;
9+
// At build time, we override the version through the environment variable
10+
// VERGEN_GIT_DESCRIBE. In some contexts, it means this variable is set but
11+
// empty, so we unset it here.
12+
if let Ok(ver) = std::env::var("VERGEN_GIT_DESCRIBE") {
13+
if ver.is_empty() {
14+
std::env::remove_var("VERGEN_GIT_DESCRIBE");
15+
}
16+
}
17+
18+
let gitcl = GitclBuilder::default()
19+
.describe(true, false, Some("v*.*.*"))
20+
.build()?;
1021
let rustc = RustcBuilder::default().semver(true).build()?;
1122

1223
Emitter::default()

docker-bake.hcl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// This is used to set the version reported by the binary through an environment
2+
// variable. This is mainly useful when building out of a git context, like in
3+
// CI, where we don't have the full commit history available
4+
variable "VERGEN_GIT_DESCRIBE" {}
5+
16
// This is what is baked by GitHub Actions
27
group "default" { targets = ["regular", "debug", "syn2mas"] }
38

@@ -11,8 +16,11 @@ target "docker-metadata-action-syn2mas" {}
1116
target "base" {
1217
args = {
1318
// This is set so that when we use a git context, the .git directory is
14-
// present, as we infer the version at build time out of it
19+
// present, as we may be infering the version at build time out of it
1520
BUILDKIT_CONTEXT_KEEP_GIT_DIR = 1
21+
22+
// Pass down the version from an external git describe source
23+
VERGEN_GIT_DESCRIBE = "${VERGEN_GIT_DESCRIBE}"
1624
}
1725

1826
platforms = [

0 commit comments

Comments
 (0)