Skip to main content

Git Commit Hash

The Git Commit Hash recipe creates a short version of the current Git commit hash and provides it as an environment variable. In the default configuration, the value is available in subsequent build steps as %env.GIT_COMMIT_SHORT_HASH%.

Recipe IDjjideenschmiede/git-commit-hash
MarketplaceGit Commit Hash
Source codeGitHub

How It Works​

The recipe reads the VCS revision of the build from the environment variable BUILD_VCS_NUMBER, shortens it to the configured length and sets the result as a build parameter. Neither access to the repository nor an installed Git on the agent is required.

Installation​

The recipe requires TeamCity 2025.03 or later. How to add it to a build configuration is described under Using a Recipe.

Inputs​

InputRequiredDefaultDescription
Hash LengthNo7Number of characters for the short commit hash (between 4 and 40)
Environment Variable NameNoGIT_COMMIT_SHORT_HASHName of the environment variable that stores the short hash

Output​

ParameterDescriptionExample
env.GIT_COMMIT_SHORT_HASHShort commit hash; the name follows the input Environment Variable Namea1b2c3d

Example​

Usage in a subsequent command line step, for example as a tag for a Docker image:

docker build -t registry.example.com/app:%env.GIT_COMMIT_SHORT_HASH% .

Notes​

  • The recipe does not validate the range of Hash Length. If the value is larger than the length of the hash, the complete hash is used; a value that is not a number leads to an error.
  • The build needs a VCS root. Without a VCS revision, the recipe fails with an error.
  • For builds with several VCS roots, TeamCity provides the revision per root in separate variables. The recipe only uses BUILD_VCS_NUMBER.