Skip to content

Instantly share code, notes, and snippets.

@tobrun
Last active February 14, 2023 18:35
Show Gist options
  • Save tobrun/439fdf06c51083e49966647736769597 to your computer and use it in GitHub Desktop.
Save tobrun/439fdf06c51083e49966647736769597 to your computer and use it in GitHub Desktop.
Script to automatically generate SNAPSHOT version if repository tags start with `v` naming
import subprocess
from datetime import datetime
# Get the ISO 8601 UTC datetime
now = datetime.utcnow()
iso_time = now.strftime('%Y%m%dT%H%M%SZ')
# Get the SHA of the latest commit
commit_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode('utf-8').strip()[:7]
# Get a list of all tags in the repository
tags_output = subprocess.check_output(['git', 'tag', '-l']).decode('utf-8')
tags = [tag.lstrip('v') for tag in tags_output.strip().split('\n')]
# Filter tags that don't follow the "major.minor.patch" format
valid_tags = [tag for tag in tags if len(tag.split('.')) == 3]
# Find the highest version number
highest_version = '0.0.0'
for tag in valid_tags:
if tag > highest_version:
highest_version = tag
# Increment the minor version by 1
major_version, minor_version, patch_version = highest_version.split('.')
new_minor_version = int(minor_version) + 1
new_version = f"{major_version}.{new_minor_version}.{patch_version}-SNAPSHOT.{iso_time}+{commit_hash}"
print(new_version)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment