Bỏ qua đến nội dung chính
TMT ELECTRONICv2.26.5
DEVELOPMENT TOOLS

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 Đủ

📦 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

CODE
MAJOR.MINOR.PATCH
 1  .  2  .  3

Ví 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

CODE
1.2.3 → 1.2.4  ✅ Bug fix
1.2.3 → 1.2.5  ✅ Multiple patches

Ví 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ũ

CODE
1.2.3 → 1.3.0  ✅ New feature
1.2.3 → 1.4.0  ✅ Major feature addition

Ví 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

CODE
1.2.3 → 2.0.0  ✅ Breaking change
1.2.3 → 3.0.0  ✅ Major rewrite

Ví 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

CODE
X.Y.Z-prerelease[.build]

Ví dụ:

  • 1.0.0-alpha
  • 1.0.0-alpha.1
  • 1.0.0-beta.2
  • 1.0.0-rc.1

Thứ Tự Ưu Tiên Pre-release

Theo thứ tự từ thấp đến cao:

  1. alpha - Phát triển sớm, nhiều bugs
  2. beta - Feature complete, testing
  3. rc (release candidate) - Sẵn sàng release
CODE
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.0

So Sánh Versions

CODE
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

CODE
X.Y.Z-prerelease+build

Ví dụ:

  • 1.0.0+20240607
  • 1.0.0-beta+exp.sha.5114f85
  • 1.0.0+21AF26D3----117B344092BD

Lưu ý: Build metadata KHÔNG ảnh hưởng đến version precedence

CODE
1.0.0+build.1 = 1.0.0+build.2  (Same precedence)

🚀 Workflow Thực Tế

Development Cycle

CODE
# 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 development

Git Branching Strategy

BASH
# 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

JSON
{
 "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 (^)

CODE
^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 (~)

CODE
~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

JSON
{
 "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

BASH
npm install --save-dev semantic-release
JSON
// .releaserc.json
{
 "branches": ["main"],
 "plugins": [
   "@semantic-release/commit-analyzer",
   "@semantic-release/release-notes-generator",
   "@semantic-release/npm",
   "@semantic-release/github"
 ]
}

Standard Version

BASH
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 version

Conventional Commits

BASH
# 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

CODE
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 features

Node.js LTS Strategy

CODE
# 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 ✅

  1. Start with 0.1.0 cho projects mới
  2. Use 1.0.0 khi API stable
  3. Document breaking changes trong CHANGELOG
  4. Test pre-releases thoroughly
  5. Use conventional commits cho automation
  6. Keep dependencies updated within compatible ranges

Don'ts ❌

  1. Đừng skip versions (1.0.0 → 1.0.2)
  2. Đừng reuse versions sau khi publish
  3. Đừng thay đổi published versions
  4. Đừng break compatibility trong PATCH/MINOR
  5. Đừng dùng dates làm version (20240607)

Version 0.x.x Special Rules

CODE
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 release

Lưu ý: Version 0.x.x được coi là development phase, breaking changes có thể xảy ra ở MINOR version.

🔄 Migration & Deprecation

Deprecation Strategy

JAVASCRIPT
// 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

CODE
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

MARKDOWN
# 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 messages

Git Tags

BASH
# 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

JSON
{
 "name": "my-package",
 "version": "1.2.3",
 "engines": {
   "node": ">=16.0.0"
 }
}

Python/PyPI

PYTHON
# setup.py
version="1.2.3"

# __init__.py
__version__ = "1.2.3"

Maven/Java

XML
<groupId>com.company</groupId>
<artifactId>my-app</artifactId>
<version>1.2.3</version>

Docker Images

DOCKERFILE
# Multi-stage tagging
FROM node:16 as builder
# ...

FROM nginx:alpine
LABEL version="1.2.3"
BASH
# 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

JAVASCRIPT
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'));             // false

JavaScript Library

JAVASCRIPT
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

BASH
# 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.0

NPM Version Commands

BASH
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!

THẺ

  • #Semantic Versioning
  • #SemVer
  • #Version Control
  • #Software Release
  • #Alpha Beta RC
  • #NPM Versioning
  • #Git Tags
  • #Conventional Commits
  • #Semantic Release
  • #Version Management
  • #Software Development
  • #Release Process
  • #Breaking Changes
  • #Dependency Management
  • #CHANGELOG
Xem tất cả bài viết →
Hợp tác