Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

Build-time version injection via exec hooks (config-based)

Inject the GitVersion-computed version into a Rust build without writing any Rust glue — just configure an exec hook in GitVersion.yml and run the CLI.

When gitversion-rs runs an exec command it exports the computed version into the environment under two sets of names:

Name Example Source variable
CARGO_PKG_VERSION 1.4.0-alpha.3 SemVer
CARGO_PKG_VERSION_MAJOR 1 Major
CARGO_PKG_VERSION_MINOR 4 Minor
CARGO_PKG_VERSION_PATCH 0 Patch
CARGO_PKG_VERSION_PRE alpha.3 PreReleaseTag
GitVersion_* GitVersion_FullSemVer=1.4.0-alpha.3+5 every output variable

{SemVer} / {FullSemVer} template tokens are also substituted into the command string itself (see GitVersion.yml).

Run it

From a Rust project that contains this GitVersion.yml:

gitversion-rs --config GitVersion.yml

The prepare hook stamps the version into Cargo.toml and runs cargo build --release.

Dry-run to see the command without side effects:

gitversion-rs --config GitVersion.yml --dry-run

Or supply an ad-hoc prepare command (runs after the config's prepare):

gitversion-rs --exec 'docker build --build-arg VERSION=$CARGO_PKG_VERSION .'

Important caveat

cargo build derives CARGO_PKG_VERSION from Cargo.toml, so the inherited env var alone does not change the version the inner crate compiles with — you must stamp Cargo.toml first (the hook does this with sed). The exported env vars are most useful for scripts and external tools (Docker build-args, release uploaders, etc.).

If you want to override a crate's own version without modifying Cargo.toml, use the library / build.rs approach instead — see ../build_inject.rs, where cargo:rustc-env=CARGO_PKG_VERSION=... is applied by Cargo after it reads the manifest and therefore wins.

Files

  • GitVersion.yml — the exec hook configuration.
  • build.rs — sample consumer build.rs that reads CARGO_PKG_VERSION.