In this lesson, you will learn how to implement Semantic Versioning (SemVer) and automate Git repository tagging to manage project releases programmatically.
The Logic of Semantic Versioning
Versioning is more than just numbering; it is a contract with your users. Semantic Versioning uses a MAJOR.MINOR.PATCH format:
- MAJOR: Breaking changes that break backward compatibility.
- MINOR: New features that are backward-compatible.
- PATCH: Backward-compatible bug fixes.
In an automated pipeline, manual version bumping is prone to error. You can automate this by integrating a versioning tool within your CI/CD workflow (such as GitHub Actions) that parses your commit history (using Conventional Commits) to determine the next version number.
Automating the Tagging Process
Once your code is tested and ready, you want to mark that point in history with a Git tag. If you are using a tool like poetry to manage your project, you can synchronize your version metadata with the repository state using Python to execute Git commands safely.
Here is a conceptual example of how you might automate the tagging process after a successful build:
import subprocess
def create_git_tag(version):
"""Safely tag the current commit with the provided version."""
try:
tag_name = f"v{version}"
# Execute shell commands to create and push the tag
subprocess.run(["git", "tag", "-a", tag_name, "-m", f"Release {version}"], check=True)
subprocess.run(["git", "push", "origin", tag_name], check=True)
print(f"Successfully tagged and pushed: {tag_name}")
except subprocess.CalledProcessError as e:
print(f"Failed to create tag: {e}")
# Usage: after verifying tests pass, trigger the tag
create_git_tag("1.2.4")
Why This Matters
By automating this, you eliminate the "human factor" where a developer might forget to tag a release or accidentally skip a version. This creates a clean, deterministic lineage for your software. When combined with your knowledge of GitHub Actions (Lesson 24), you can configure your automation to run this script automatically whenever a pull request is merged into the main branch, ensuring that your package metadata, lockfiles, and git tags are always perfectly aligned.
Key Takeaway: Automated tagging enforces a strict, reliable development lifecycle by transforming your versioning strategy from a manual chore into a systematic, reproducible part of your build pipeline.