Fix reporting of version in prebuilt binaries & docker image

This commit is contained in:
Quentin Gliech
2025-01-27 10:34:21 +01:00
parent 057256305c
commit 3b744c7881
4 changed files with 60 additions and 3 deletions

View File

@@ -31,10 +31,37 @@ env:
DOCKER_METADATA_ANNOTATIONS_LEVELS: manifest,index DOCKER_METADATA_ANNOTATIONS_LEVELS: manifest,index
jobs: jobs:
compute-version:
name: Compute version using git describe
runs-on: ubuntu-24.04
outputs:
describe: ${{ steps.git.outputs.describe }}
timestamp: ${{ steps.git.outputs.timestamp }}
steps:
- name: Checkout the code
uses: actions/checkout@v4.2.2
with:
# Need a full clone so that `git describe` reports the right version
fetch-depth: 0
- name: Compute version and timestamp out of git history
id: git
run: |
echo "describe=$(git describe --tags --match 'v*.*.*' --always)" >> $GITHUB_OUTPUT
echo "timestamp=$(git log -1 --format=%ct)" >> $GITHUB_OUTPUT
build-binaries: build-binaries:
name: Build binaries name: Build binaries
runs-on: ubuntu-22.04 runs-on: ubuntu-22.04
needs:
- compute-version
env:
VERGEN_GIT_DESCRIBE: ${{ needs.compute-version.outputs.describe }}
SOURCE_DATE_EPOCH: ${{ needs.compute-version.outputs.timestamp }}
permissions: permissions:
contents: read contents: read
@@ -136,6 +163,13 @@ jobs:
packages: write packages: write
id-token: write id-token: write
needs:
- compute-version
env:
VERGEN_GIT_DESCRIBE: ${{ needs.compute-version.outputs.describe }}
SOURCE_DATE_EPOCH: ${{ needs.compute-version.outputs.timestamp }}
steps: steps:
- name: Docker meta - name: Docker meta
id: meta id: meta

View File

@@ -142,6 +142,10 @@ RUN --network=default \
# Build the rest # Build the rest
COPY ./ /app COPY ./ /app
ENV SQLX_OFFLINE=true ENV SQLX_OFFLINE=true
ARG VERGEN_GIT_DESCRIBE
ENV VERGEN_GIT_DESCRIBE=${VERGEN_GIT_DESCRIBE}
# Network access: cargo auditable needs it # Network access: cargo auditable needs it
RUN --network=default \ RUN --network=default \
cargo auditable build \ cargo auditable build \

View File

@@ -1,4 +1,4 @@
// Copyright 2024 New Vector Ltd. // Copyright 2024, 2025 New Vector Ltd.
// //
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details. // Please see LICENSE in the repository root for full details.
@@ -6,7 +6,18 @@
use vergen_gitcl::{Emitter, GitclBuilder, RustcBuilder}; use vergen_gitcl::{Emitter, GitclBuilder, RustcBuilder};
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
let gitcl = GitclBuilder::default().describe(true, true, None).build()?; // At build time, we override the version through the environment variable
// VERGEN_GIT_DESCRIBE. In some contexts, it means this variable is set but
// empty, so we unset it here.
if let Ok(ver) = std::env::var("VERGEN_GIT_DESCRIBE") {
if ver.is_empty() {
std::env::remove_var("VERGEN_GIT_DESCRIBE");
}
}
let gitcl = GitclBuilder::default()
.describe(true, false, Some("v*.*.*"))
.build()?;
let rustc = RustcBuilder::default().semver(true).build()?; let rustc = RustcBuilder::default().semver(true).build()?;
Emitter::default() Emitter::default()

View File

@@ -1,3 +1,8 @@
// This is used to set the version reported by the binary through an environment
// variable. This is mainly useful when building out of a git context, like in
// CI, where we don't have the full commit history available
variable "VERGEN_GIT_DESCRIBE" {}
// This is what is baked by GitHub Actions // This is what is baked by GitHub Actions
group "default" { targets = ["regular", "debug", "syn2mas"] } group "default" { targets = ["regular", "debug", "syn2mas"] }
@@ -11,8 +16,11 @@ target "docker-metadata-action-syn2mas" {}
target "base" { target "base" {
args = { args = {
// This is set so that when we use a git context, the .git directory is // This is set so that when we use a git context, the .git directory is
// present, as we infer the version at build time out of it // present, as we may be infering the version at build time out of it
BUILDKIT_CONTEXT_KEEP_GIT_DIR = 1 BUILDKIT_CONTEXT_KEEP_GIT_DIR = 1
// Pass down the version from an external git describe source
VERGEN_GIT_DESCRIBE = "${VERGEN_GIT_DESCRIBE}"
} }
platforms = [ platforms = [