Skip to content

Commit

Permalink
Merge pull request #3881 from element-hq/quenting/fix-version-reporting
Browse files Browse the repository at this point in the history
Fix reporting of version in prebuilt binaries & docker image
  • Loading branch information
sandhose authored Jan 27, 2025
2 parents 3a18200 + 1016357 commit 060d8ad
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
38 changes: 37 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ on:

# Only run for pull requests if relevant files were changed
pull_request:
branches: [main]
branches:
- main
- 'release/**'
paths:
- Dockerfile
- docker-bake.hcl
Expand All @@ -31,10 +33,37 @@ env:
DOCKER_METADATA_ANNOTATIONS_LEVELS: manifest,index

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:
name: Build binaries
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:
contents: read

Expand Down Expand Up @@ -136,6 +165,13 @@ jobs:
packages: 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:
- name: Docker meta
id: meta
Expand Down
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ RUN --network=default \
# Build the rest
COPY ./ /app
ENV SQLX_OFFLINE=true

ARG VERGEN_GIT_DESCRIBE
ENV VERGEN_GIT_DESCRIBE=${VERGEN_GIT_DESCRIBE}

# Network access: cargo auditable needs it
RUN --network=default \
cargo auditable build \
Expand Down
15 changes: 13 additions & 2 deletions crates/cli/build.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
// Copyright 2024 New Vector Ltd.
// Copyright 2024, 2025 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.

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

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()?;

Emitter::default()
Expand Down
10 changes: 9 additions & 1 deletion docker-bake.hcl
Original file line number Diff line number Diff line change
@@ -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
group "default" { targets = ["regular", "debug", "syn2mas"] }

Expand All @@ -11,8 +16,11 @@ target "docker-metadata-action-syn2mas" {}
target "base" {
args = {
// 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

// Pass down the version from an external git describe source
VERGEN_GIT_DESCRIBE = "${VERGEN_GIT_DESCRIBE}"
}

platforms = [
Expand Down

0 comments on commit 060d8ad

Please sign in to comment.