This is a language agnostic git versioning tool using tags.
There are plenty of tools available that can generate a version based on Git tags.
However, they are typically:
driven by commit messages, not configuration
dependent on programming languages
I didn't want to have to introduce a programming language into my pipeline, especially when it was a language that had
absolutely nothing to do with my pipeline, e.g. using an action implemented in JS in a Python project.
I also didn't feel like commit messages were the way to go, typos happen, and then someone needs to go and update it
manually.
The aim has been to have a very simple approach at versioning without introducing new dependencies, and for it to be
configuration driven.
The version.json file supports the following options:
Field
Description
Default
Example
default_branch
Main branch name
"master"
"main", "master"
major
Minimum major version
"0"
"1", "2"
minor
Minimum minor version
"0"
"0", "5"
tag_prefix
Prefix for git tags
""
"v", "release-"
include_v_prefix
Include v in output
false
true, false
On your default branch (e.g., main), the script produces clean semantic versions:
# No previous tags
./version.sh
# Output: v1.0.0# After tag v1.0.0, with 3 more commits
./version.sh
# Output: v1.0.3# With version constraints (major: "2", minor: "1")
./version.sh
# Output: v2.1.0 (enforces minimums)
On feature branches, the script produces pre-release versions:
# On branch "feature/user-auth"
./version.sh
# Output: v1.0.3-dev.feature-user-auth.abc1234# On branch "bugfix/login-issue"
./version.sh
# Output: v1.0.3-dev.bugfix-login-issue.def5678
When you have uncommitted changes:
./version.sh
# Main branch: v1.0.3-dirty# Feature branch: v1.0.3-dev.feature-auth.abc1234.dirty
#!/bin/bash
VERSION=$(./version.sh)echo"Building version: $VERSION"# Use in Docker build
docker build -t myapp:$VERSION.# Use in package.json
npm version $VERSION --no-git-tag-version
VERSION := $(shell ./version.sh).PHONY: version
version:
@echo $(VERSION).PHONY: build
build:
docker build -t myapp:$(VERSION)..PHONY: tag
tag:
git tag $(VERSION)
git push origin $(VERSION)
#!/bin/bashset -e
# Get version
VERSION=$(./version.sh)
CLEAN_VERSION=${VERSION#v}# Remove v prefix# Check if pre-releaseif [[ "$VERSION"=~-dev\. ]];thenecho"Pre-release version: $VERSION"echo"Deploying to staging..."elseecho"Release version: $VERSION"echo"Deploying to production..."fi