Semantic Versioning - Hướng Dẫn Đầy Đủ
18 PHÚT ĐỌCTMT ELECTRONIC TEAM
Hướng dẫn chi tiết về cách đặt và quản lý version cho phần mềm theo chuẩn Semantic Versioning (SemVer), bao gồm alpha, beta, rc và best practices.

📦 Semantic Versioning - Hướng Dẫn Đầy Đủ
Hướng dẫn chi tiết về cách đặt và quản lý version cho phần mềm theo chuẩn Semantic Versioning (SemVer).
🔢 Semantic Versioning Là Gì?
Semantic Versioning (SemVer) là một chuẩn đặt tên version cho phần mềm, giúp developers và users hiểu được mức độ thay đổi giữa các phiên bản.
Cấu Trúc Cơ Bản: X.Y.Z
MAJOR.MINOR.PATCH
1 . 2 . 3Ví dụ: 2.1.4
- MAJOR (2): Breaking changes
- MINOR (1): New features (backward compatible)
- PATCH (4): Bug fixes (backward compatible)
📈 Quy Tắc Nâng Version
1. PATCH Version (Z)
Khi nào: Bug fixes, security patches
Tăng khi: Sửa lỗi mà không thay đổi API
1.2.3 → 1.2.4 ✅ Bug fix
1.2.3 → 1.2.5 ✅ Multiple patchesVí dụ:
- Fix null pointer exception
- Sửa typo trong documentation
- Performance improvements không thay đổi API
- Security vulnerability patches
2. MINOR Version (Y)
Khi nào: Thêm features mới, backward compatible
Tăng khi: Thêm tính năng mà không phá vỡ API cũ
1.2.3 → 1.3.0 ✅ New feature
1.2.3 → 1.4.0 ✅ Major feature additionVí dụ:
- Thêm method mới vào class
- Thêm optional parameters
- Thêm new endpoints trong API
- Deprecate old features (chưa xóa)
Lưu ý: PATCH reset về 0 khi MINOR tăng
3. MAJOR Version (X)
Khi nào: Breaking changes
Tăng khi: Thay đổi không tương thích ngược
1.2.3 → 2.0.0 ✅ Breaking change
1.2.3 → 3.0.0 ✅ Major rewriteVí dụ:
- Xóa method/class đã deprecated
- Thay đổi signature của function
- Thay đổi cấu trúc response API
- Thay đổi database schema
- Require version mới của dependencies
Lưu ý: MINOR và PATCH reset về 0 khi MAJOR tăng
🏷️ Pre-release Versions
Cấu Trúc Pre-release
X.Y.Z-prerelease[.build]Ví dụ:
1.0.0-alpha1.0.0-alpha.11.0.0-beta.21.0.0-rc.1
Thứ Tự Ưu Tiên Pre-release
Theo thứ tự từ thấp đến cao:
- alpha - Phát triển sớm, nhiều bugs
- beta - Feature complete, testing
- rc (release candidate) - Sẵn sàng release
1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < 1.0.0-beta
< 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0So Sánh Versions
1.0.0-alpha.1 < 1.0.0-alpha.2
1.0.0-alpha.2 < 1.0.0-alpha.beta
1.0.0-alpha.beta < 1.0.0-beta
1.0.0-beta < 1.0.0-beta.1
1.0.0-beta.1 < 1.0.0-beta.2
1.0.0-beta.2 < 1.0.0-rc.1
1.0.0-rc.1 < 1.0.0-rc.2
1.0.0-rc.2 < 1.0.0
1.0.0 < 1.0.1
1.0.1 < 1.1.0
1.1.0 < 2.0.0🔖 Build Metadata
Cấu Trúc với Build Info
X.Y.Z-prerelease+buildVí dụ:
1.0.0+202406071.0.0-beta+exp.sha.5114f851.0.0+21AF26D3----117B344092BD
Lưu ý: Build metadata KHÔNG ảnh hưởng đến version precedence
1.0.0+build.1 = 1.0.0+build.2 (Same precedence)🚀 Workflow Thực Tế
Development Cycle
# Start new major version
1.0.0-alpha.1 # Initial development
1.0.0-alpha.2 # Add core features
1.0.0-alpha.3 # More features
1.0.0-beta.1 # Feature freeze, testing
1.0.0-beta.2 # Bug fixes
1.0.0-beta.3 # More bug fixes
1.0.0-rc.1 # Release candidate
1.0.0-rc.2 # Final fixes
1.0.0 # Stable release
# Bug fixes
1.0.1 # Patch release
1.0.2 # More patches
# New features
1.1.0 # Minor release
1.1.1 # Patch
# Breaking changes
2.0.0-alpha.1 # Next major developmentGit Branching Strategy
# Main development
git checkout main
# Version 2.0.0-alpha.1
# Feature branch
git checkout -b feature/new-api
# Still 2.0.0-alpha.1
# When feature complete
git checkout main
git merge feature/new-api
# Bump to 2.0.0-alpha.2
# Beta testing
git checkout -b release/2.0.0-beta
# Version 2.0.0-beta.1
# Release candidate
git checkout -b release/2.0.0-rc
# Version 2.0.0-rc.1
# Final release
git checkout main
git merge release/2.0.0-rc
# Version 2.0.0📋 Version Ranges & Dependencies
NPM-style Ranges
{
"dependencies": {
"express": "^4.18.0", // >=4.18.0 <5.0.0
"lodash": "~4.17.21", // >=4.17.21 <4.18.0
"react": ">=16.8.0", // >=16.8.0
"vue": "3.2.x", // >=3.2.0 <3.3.0
"typescript": "*" // Latest
}
}Caret Range (^)
^1.2.3 := >=1.2.3 <2.0.0 (Compatible MINOR/PATCH)
^0.2.3 := >=0.2.3 <0.3.0 (0.x.x special case)
^0.0.3 := >=0.0.3 <0.0.4 (0.0.x special case)Tilde Range (~)
~1.2.3 := >=1.2.3 <1.3.0 (Compatible PATCH)
~1.2 := >=1.2.0 <1.3.0 (Same as ~1.2.0)
~1 := >=1.0.0 <2.0.0 (Same as ^1.0.0)🛠️ Công Cụ & Automation
Package.json Scripts
{
"scripts": {
"version:patch": "npm version patch",
"version:minor": "npm version minor",
"version:major": "npm version major",
"version:prerelease": "npm version prerelease",
"version:beta": "npm version prerelease --preid=beta",
"version:alpha": "npm version prerelease --preid=alpha",
"version:rc": "npm version prerelease --preid=rc"
}
}Automatic Versioning Tools
Semantic Release
npm install --save-dev semantic-release// .releaserc.json
{
"branches": ["main"],
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/npm",
"@semantic-release/github"
]
}Standard Version
npm install --save-dev standard-version
# Usage
npm run release # Auto-detect version bump
npm run release:minor # Force minor version
npm run release:major # Force major versionConventional Commits
# Format: type(scope): description
feat(api): add user authentication endpoint # MINOR bump
fix(auth): resolve token expiration bug # PATCH bump
feat!: change API response format # MAJOR bump
BREAKING CHANGE: remove deprecated endpoints # MAJOR bump
# Examples
feat: add shopping cart functionality
fix: resolve memory leak in image processing
docs: update API documentation
style: fix code formatting
refactor: extract common utilities
test: add unit tests for user service
chore: update dependencies📊 Version Lifecycle Examples
React.js Version History
15.6.2 → 16.0.0-alpha.1 # Start v16 development
16.0.0-alpha.1 → 16.0.0-alpha.13 # Alpha releases
16.0.0-alpha.13 → 16.0.0-beta.1 # Beta testing
16.0.0-beta.1 → 16.0.0-beta.5 # Beta fixes
16.0.0-beta.5 → 16.0.0-rc.1 # Release candidate
16.0.0-rc.1 → 16.0.0 # Stable release
16.0.0 → 16.0.1 # Patch fixes
16.0.1 → 16.1.0 # New featuresNode.js LTS Strategy
# Even numbers = LTS versions
12.0.0 # Current → Active LTS → Maintenance
13.0.0 # Current → EOL (Odd, no LTS)
14.0.0 # Current → Active LTS → Maintenance
15.0.0 # Current → EOL (Odd, no LTS)
16.0.0 # Current → Active LTS → Maintenance🎯 Best Practices
Do's ✅
- Start with 0.1.0 cho projects mới
- Use 1.0.0 khi API stable
- Document breaking changes trong CHANGELOG
- Test pre-releases thoroughly
- Use conventional commits cho automation
- Keep dependencies updated within compatible ranges
Don'ts ❌
- Đừng skip versions (1.0.0 → 1.0.2)
- Đừng reuse versions sau khi publish
- Đừng thay đổi published versions
- Đừng break compatibility trong PATCH/MINOR
- Đừng dùng dates làm version (20240607)
Version 0.x.x Special Rules
0.1.0 → 0.1.1 # Bug fixes
0.1.0 → 0.2.0 # Breaking changes allowed
0.1.0 → 1.0.0 # First stable releaseLưu ý: Version 0.x.x được coi là development phase, breaking changes có thể xảy ra ở MINOR version.
🔄 Migration & Deprecation
Deprecation Strategy
// Version 1.5.0 - Deprecate old method
function oldMethod() {
console.warn('oldMethod is deprecated, use newMethod instead');
return newMethod();
}
function newMethod() {
// New implementation
}Breaking Change Timeline
1.5.0 # Introduce new API, deprecate old
1.6.0 # Continue supporting both
1.7.0 # Final version with old API
2.0.0 # Remove old API (breaking change)📝 Version Documentation
CHANGELOG.md Format
# Changelog
## [2.0.0] - 2024-06-07
### Added
- New authentication system
- Support for multiple databases
### Changed
- API response format (BREAKING CHANGE)
- Improved performance
### Deprecated
- Old login method (use authenticate instead)
### Removed
- Legacy payment gateway
### Fixed
- Memory leak in file processing
- Session timeout issues
### Security
- Fixed XSS vulnerability in comments
## [1.2.3] - 2024-05-15
### Fixed
- Bug in user registration
- Typo in error messagesGit Tags
# Create version tag
git tag -a v1.2.3 -m "Release version 1.2.3"
git push origin v1.2.3
# List tags
git tag -l
# Checkout specific version
git checkout v1.2.3🌍 Platform-Specific Considerations
NPM/Node.js
{
"name": "my-package",
"version": "1.2.3",
"engines": {
"node": ">=16.0.0"
}
}Python/PyPI
# setup.py
version="1.2.3"
# __init__.py
__version__ = "1.2.3"Maven/Java
<groupId>com.company</groupId>
<artifactId>my-app</artifactId>
<version>1.2.3</version>Docker Images
# Multi-stage tagging
FROM node:16 as builder
# ...
FROM nginx:alpine
LABEL version="1.2.3"# Docker tag strategy
docker build -t myapp:1.2.3 .
docker tag myapp:1.2.3 myapp:1.2
docker tag myapp:1.2.3 myapp:1
docker tag myapp:1.2.3 myapp:latest🔍 Version Validation & Parsing
RegEx Pattern
const semverRegex = /^(0|[1-9]d*).(0|[1-9]d*).(0|[1-9]d*)(?:-((?:0|[1-9]d*|d*[a-zA-Z-][0-9a-zA-Z-]*)(?:.(?:0|[1-9]d*|d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:+([0-9a-zA-Z-]+(?:.[0-9a-zA-Z-]+)*))?$/;
// Test versions
console.log(semverRegex.test('1.2.3')); // true
console.log(semverRegex.test('1.2.3-alpha.1')); // true
console.log(semverRegex.test('1.2.3+build.1')); // true
console.log(semverRegex.test('1.2')); // falseJavaScript Library
const semver = require('semver');
// Version comparison
semver.gt('1.2.3', '1.2.2'); // true
semver.lt('1.2.3-alpha', '1.2.3'); // true
semver.satisfies('1.2.3', '^1.2.0'); // true
// Version parsing
const version = semver.parse('1.2.3-alpha.1+build.123');
console.log(version.major); // 1
console.log(version.minor); // 2
console.log(version.patch); // 3
console.log(version.prerelease); // ['alpha', 1]
console.log(version.build); // ['build', '123']⚡ Quick Reference
Version Bump Cheat Sheet
# Bug fix
1.2.3 → 1.2.4
# New feature (backward compatible)
1.2.3 → 1.3.0
# Breaking change
1.2.3 → 2.0.0
# Pre-releases
1.2.3 → 1.3.0-alpha.1
1.3.0-alpha.1 → 1.3.0-alpha.2
1.3.0-alpha.2 → 1.3.0-beta.1
1.3.0-beta.1 → 1.3.0-rc.1
1.3.0-rc.1 → 1.3.0NPM Version Commands
npm version patch # 1.2.3 → 1.2.4
npm version minor # 1.2.3 → 1.3.0
npm version major # 1.2.3 → 2.0.0
npm version prerelease --preid=alpha # 1.2.3 → 1.2.4-alpha.0
npm version prerelease --preid=beta # 1.2.3-alpha.0 → 1.2.4-beta.0
npm version prerelease --preid=rc # 1.2.3-beta.0 → 1.2.4-rc.0
npm version prerelease # 1.2.3-rc.0 → 1.2.3-rc.1🎯 Kết Luận
Semantic Versioning giúp:
- Communicate changes clearly to users
- Automate dependency management
- Enable safe updates with confidence
- Standardize release process
Quy tắc vàng:
- PATCH: Backward compatible bug fixes
- MINOR: Backward compatible new features
- MAJOR: Breaking changes
- Pre-release: alpha → beta → rc → stable
- Document everything in CHANGELOG
Tools cần thiết:
- Semantic Release cho automation
- Conventional Commits cho consistency
- Version validation libraries
- Proper branching strategy
Semantic Versioning không chỉ là về numbers - nó là về communication và trust giữa developers và users!

