Skip to main content

Extract version information from a Git repository and output as environment variables

Project description

git-version-utils

Extract version information from a Git repository and output as environment variables.

Installation

pip install git-version-utils

Usage

CLI

# Print all version info
git-version

# Output as environment variables (useful with `source` or `eval`)
git-version --property env

# Custom prefix for environment variables
git-version --prefix MYAPP --property env

# Custom tag pattern
git-version --tag-pattern "release-*" --property env

# Get a single property
git-version --property version
git-version --property version_major
git-version --property commit

# Specify repository path
git-version --repo /path/to/repo --property env

Python API

from git_version import GitVersion

gv = GitVersion(repo_path="/path/to/repo", tag_pattern="v[0-9]*")

print(gv.version)       # "1.2.3.42"
print(gv.version_major) # "1"
print(gv.version_minor) # "2"
print(gv.version_patch) # "3"
print(gv.build)         # "42"
print(gv.tag)           # "v1.2.3"
print(gv.branch)        # "main"
print(gv.short)         # "a1b2c3"
print(gv.full)          # "1.2.3.42-a1b2c3"
print(gv.commit)        # "a1b2c3d4e5f6..."

# Get all as environment variables
env_vars = gv.env(prefix="BUILD_VERSION")
for key, value in env_vars.items():
    print(f"{key}={value}")

Environment Variables Output

With default prefix BUILD_VERSION:

Variable Example Description
BUILD_VERSION 1.2.3.42 Full version: <semver>.<build>
BUILD_VERSION_MAJOR 1 Major version component
BUILD_VERSION_MINOR 2 Minor version component
BUILD_VERSION_PATCH 3 Patch version component
BUILD_VERSION_BUILD 42 Commits since last tag
BUILD_VERSION_TAG v1.2.3 Latest matching git tag
BUILD_VERSION_FULL 1.2.3.42-a1b2c3 Version with commit hash
BUILD_VERSION_EXTENDED 1.2.3.42-a1b2c3 Full if build>0, else version
BUILD_VERSION_SHORT a1b2c3 Short 6-char commit hash
BUILD_VERSION_COMMIT a1b2c3d4... Full 40-char commit hash
BUILD_VERSION_BRANCH main Current branch name
BUILD_VERSION_DEFAULT_BRANCH master Default branch from git config

CI/CD Integration

Shell (source)

source <(git-version --property env)
echo "$BUILD_VERSION"

CMake

execute_process(
    COMMAND git-version --property env
    OUTPUT_VARIABLE GIT_VERSION_ENV
    OUTPUT_STRIP_TRAILING_WHITESPACE
)

Docker

RUN pip install git-version-utils
RUN source <(git-version --property env) && echo "Building $BUILD_VERSION"

Docker CI Container

A pre-built Docker image with git-version-utils is available at ghcr.io/synacker/git-version-utils.

The image is based on python:3.13-slim and includes git + git-version-utils. It is designed to be used as the job container in CI pipelines.

Usage

# Run git-version inside the container
docker run --rm \
  -v $(pwd):/workspace -w /workspace \
  ghcr.io/synacker/git-version-utils:latest \
  git-version --safe-directory '*' --property env

GitHub Actions — Job Outputs

Use container: to run the job inside the image, then parse git-version --property env into $GITHUB_OUTPUT. Downstream jobs consume the values via needs.set-version.outputs.*.

name: CI with job outputs

on:
  push:
    branches: [main, "release/*"]

jobs:
  set-version:
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/synacker/git-version-utils:latest
      options: --workdir /__w/${{ github.event.repository.name }}/${{ github.event.repository.name }}
    outputs:
      BUILD_VERSION: ${{ steps.git_version.outputs.BUILD_VERSION }}
      BUILD_VERSION_MAJOR: ${{ steps.git_version.outputs.BUILD_VERSION_MAJOR }}
      BUILD_VERSION_MINOR: ${{ steps.git_version.outputs.BUILD_VERSION_MINOR }}
      BUILD_VERSION_PATCH: ${{ steps.git_version.outputs.BUILD_VERSION_PATCH }}
      BUILD_VERSION_BUILD: ${{ steps.git_version.outputs.BUILD_VERSION_BUILD }}
      BUILD_VERSION_TAG: ${{ steps.git_version.outputs.BUILD_VERSION_TAG }}
      BUILD_VERSION_FULL: ${{ steps.git_version.outputs.BUILD_VERSION_FULL }}
      BUILD_VERSION_EXTENDED: ${{ steps.git_version.outputs.BUILD_VERSION_EXTENDED }}
      BUILD_VERSION_SHORT: ${{ steps.git_version.outputs.BUILD_VERSION_SHORT }}
      BUILD_VERSION_COMMIT: ${{ steps.git_version.outputs.BUILD_VERSION_COMMIT }}
      BUILD_VERSION_BRANCH: ${{ steps.git_version.outputs.BUILD_VERSION_BRANCH }}
      BUILD_VERSION_DEFAULT_BRANCH: ${{ steps.git_version.outputs.BUILD_VERSION_DEFAULT_BRANCH }}
      BUILD_VERSION_RELEASE_BRANCHES: ${{ steps.git_version.outputs.BUILD_VERSION_RELEASE_BRANCHES }}

    steps:
      - uses: actions/checkout@v6.0.3
        with:
          fetch-depth: 0

      - name: Extract version info
        id: git_version
        run: |
          while IFS='=' read -r key value; do
            echo "$key=$value" >> "$GITHUB_OUTPUT"
          done < <(git-version --property env)

  build:
    runs-on: ubuntu-latest
    needs: set-version
    steps:
      - uses: actions/checkout@v6.0.3

      - name: Use version info
        run: |
          echo "Building version: ${{ needs.set-version.outputs.BUILD_VERSION }}"
          echo "Tag: ${{ needs.set-version.outputs.BUILD_VERSION_TAG }}"

GitHub Actions — Env File Artifact

A simpler approach: write the version info to a file and share it as an artifact.

name: CI with env file

on:
  push:
    branches: [main, "release/*"]

jobs:
  set-version:
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/synacker/git-version-utils:latest
      options: --workdir /__w/${{ github.event.repository.name }}/${{ github.event.repository.name }}
    steps:
      - uses: actions/checkout@v6.0.3
        with:
          fetch-depth: 0

      - name: Generate version.env
        run: git-version --property env > version.env

      - name: Upload version env file
        uses: actions/upload-artifact@v4
        with:
          name: version-env
          path: version.env

  build:
    runs-on: ubuntu-latest
    needs: set-version
    steps:
      - uses: actions/checkout@v6.0.3

      - name: Download version env file
        uses: actions/download-artifact@v4
        with:
          name: version-env

      - name: Load version and use it
        run: |
          source version.env
          echo "Building version: $BUILD_VERSION"
          echo "Tag: $BUILD_VERSION_TAG"

GitLab CI

Use the image directly and share version info via dotenv artifacts.

stages:
  - set-version
  - build

set-version:
  stage: set-version
  image: ghcr.io/synacker/git-version-utils:latest
  variables:
    GIT_DEPTH: 0
  script:
    - git-version --property env > version.env
  artifacts:
    reports:
      dotenv: version.env

build:
  stage: build
  image: python:3.13-slim
  needs:
    - job: set-version
      artifacts: true
  script:
    - echo "Building version: $BUILD_VERSION"
    - echo "Tag: $BUILD_VERSION_TAG"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

git_version_utils-0.2.1.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

git_version_utils-0.2.1.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file git_version_utils-0.2.1.0.tar.gz.

File metadata

  • Download URL: git_version_utils-0.2.1.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for git_version_utils-0.2.1.0.tar.gz
Algorithm Hash digest
SHA256 72a965bac50d4d1143131d56127602a9341a5743b0c7151548559f8fc428c102
MD5 8eecac92419d07c5aee5db867b843f8f
BLAKE2b-256 015e208806ea65444fac8cf1d7cad6e23e19576fe544784eccba6fe990709ad2

See more details on using hashes here.

File details

Details for the file git_version_utils-0.2.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for git_version_utils-0.2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 82f3cc018d605299f26ac9759ab50f8bbb72fe135bec4aa5fdc48616122995d2
MD5 97055f71e94a6da89e3fdc32bb54a154
BLAKE2b-256 000226d118369bbcbdd90f444a95bde2ebf12b4f5a628d64eb588b524fac6017

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page