Problem Statement
Explain semantic versioning, automated versioning in CI/CD, and release management strategies including tags, changelogs, and release notes.
Explanation
Semantic versioning (semver) uses three-part version: MAJOR.MINOR.PATCH (e.g., 2.4.1). Increment MAJOR for incompatible API changes (breaking changes), MINOR for backwards-compatible functionality additions, PATCH for backwards-compatible bug fixes. Version 1.0.0 indicates public API. Versions < 1.0.0 are initial development (anything may change).
Examples: 1.0.0 → 1.0.1 (bug fix), 1.0.1 → 1.1.0 (new feature, backwards-compatible), 1.1.0 → 2.0.0 (breaking change). Pre-release versions: 2.0.0-alpha, 2.0.0-beta.1, 2.0.0-rc.1. Build metadata: 1.0.0+20230101.
Conventional commits enable automated versioning:
```
feat: add user authentication
fix: resolve memory leak in cache
docs: update API documentation
chore: upgrade dependencies
```
Commit types: feat (MINOR bump), fix (PATCH bump), BREAKING CHANGE (MAJOR bump), docs, style, refactor, test, chore (no version bump).
Automated versioning with semantic-release:
1. Install:
```bash
npm install --save-dev semantic-release
```
2. Configuration (.releaserc.json):
```json
{
"branches": ["main"],
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/changelog",
"@semantic-release/npm",
"@semantic-release/github",
"@semantic-release/git"
]
}
```
3. CI/CD integration:
```yaml
# GitHub Actions
release:
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- run: npm ci
- run: npx semantic-release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
```
semantic-release analyzes commits, determines version bump, generates changelog, creates Git tag, publishes package, creates GitHub release.
Manual versioning with npm version:
```bash
npm version patch # 1.0.0 → 1.0.1
npm version minor # 1.0.1 → 1.1.0
npm version major # 1.1.0 → 2.0.0
git push --follow-tags
```
Git tags for releases:
```bash
# Create annotated tag
git tag -a v1.0.0 -m "Release version 1.0.0"
git push origin v1.0.0
# List tags
git tag -l
# Delete tag
git tag -d v1.0.0
git push origin :refs/tags/v1.0.0
```
Trigger release pipeline on tags:
```yaml
# GitLab
deploy:
stage: deploy
script:
- ./deploy.sh
only:
- tags
```
```yaml
# GitHub Actions
on:
push:
tags:
- 'v*'
```
Changelog generation:
Automatic with conventional-changelog:
```bash
npx conventional-changelog -p angular -i CHANGELOG.md -s
```
CHANGELOG.md:
```markdown
# Changelog
## [2.0.0] - 2024-01-15
### Breaking Changes
- Changed authentication API
### Features
- Added user roles
- Implemented OAuth2
### Bug Fixes
- Fixed memory leak in cache
- Resolved race condition
## [1.5.0] - 2024-01-01
...
```
GitHub Release Notes:
```yaml
- name: Create Release
uses: actions/create-release@v1
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
body: |
## What's New
- Feature 1
- Feature 2
## Bug Fixes
- Fix 1
draft: false
prerelease: false
```
Release strategies:
1. Trunk-based: release from main branch, tag releases, hotfixes as patches
2. Release branches: create release/v1.x branch, cherry-pick fixes, main continues development
3. GitFlow: develop branch for development, release branches for stabilization, main for production
Version file management:
```yaml
# Update version in multiple files
- name: Bump version
run: |
VERSION=$(cat VERSION)
sed -i "s/version: .*/version: $VERSION/" helm/Chart.yaml
sed -i "s/\"version\": .*/\"version\": \"$VERSION\",/" package.json
```
Best practices: follow semantic versioning strictly, use conventional commits for automation, automate versioning and changelog, tag all releases, maintain CHANGELOG.md, create GitHub/GitLab releases, document breaking changes clearly, communicate version changes to users, use pre-release versions for testing (beta, rc). Understanding versioning and release management ensures clear communication of changes and predictable dependency management.