@sharedutils/glsemver
v0.1.5
Published
Semantic versioning tool based on GitLab commit history and Conventional Commits
Maintainers
Readme
glsemver
Semantic versioning tool based on GitLab commit history and Conventional Commits.
Features
- 🚀 Calculate semantic versions from commit history
- 🔄 Follows Conventional Commits specification
- 🌿 Supports multi-branch workflows with prerelease versions
- 🌐 Pure API-based: all data fetched from GitLab API
- 🦊 Supports self-hosted GitLab instances
- 📦 Works perfectly with shallow clones in CI/CD
- 🎯 Zero configuration required
Installation
Global Installation
npm install -g @sharedutils/glsemverUse with npx (Recommended)
npx @sharedutils/glsemver current
npx @sharedutils/glsemver nextUsage
Get Current Version
Get the latest semantic version tag from your repository:
glsemver current
# Output: 1.0.0Calculate Next Version
Calculate the next version based on commit history:
glsemver next
# Output: 1.1.0With Verbose Logging
Use --log flag to see detailed information:
glsemver next --log
# Output:
# Repository: mygroup/myproject
# Host: https://gitlab.com
# Current branch: main
# Main branch: main
# Base version: 1.0.0
# HEAD SHA: abc1234
# Tag SHA: def5678
# Analyzing 3 commit(s)
# Release type: minor
# 1.1.0Command Options
current command
Get the current version (latest semantic version tag).
glsemver current [options]Options:
-r, --repo <repo>- Repository path (owner/repo or group/subgroup/repo)-H, --host <host>- GitLab host URL (default: from git remote or gitlab.com)-b, --branch <branch>- Current branch name-m, --main-branch <mainBranch>- Main branch name (default: from API)--sha <sha>- Current commit SHA--log- Enable verbose logging
next command
Calculate the next version based on commits.
glsemver next [options]Options:
-r, --repo <repo>- Repository path (owner/repo or group/subgroup/repo)-H, --host <host>- GitLab host URL (default: from git remote or gitlab.com)-b, --branch <branch>- Current branch name-m, --main-branch <mainBranch>- Main branch name (default: from API)--sha <sha>- Current commit SHA-s, --suffix <suffix>- Prerelease suffix for non-main branches--log- Enable verbose logging
Data Source Strategy
This tool uses a pure API approach:
| Data | Source |
|------|--------|
| Repository info | CLI --repo > local git remote |
| GitLab host | CLI --host > local git remote > GITLAB_HOST env > gitlab.com |
| Current branch | CLI --branch > CI_COMMIT_BRANCH env > local git |
| Current SHA | CLI --sha > CI_COMMIT_SHA env > local git > API |
| Main branch | CLI --main-branch > GitLab API |
| Tags & Commits | GitLab API (always) |
This approach is:
- 📦 Shallow clone friendly: Works perfectly with
git clone --depth 1 - 🎯 Consistent: Always reflects the remote repository state
- 🔧 Flexible: Can run without a git repository if all options provided
Version Calculation Rules
This tool follows Conventional Commits specification:
Major Version (X.0.0)
Incremented when commits contain breaking changes:
BREAKING CHANGE:orBREAKING-CHANGE:in commit message body!after commit type:feat!:,fix!:
feat!: redesign authentication system
BREAKING CHANGE: removed old authentication APIResult: 1.0.0 → 2.0.0
Minor Version (0.X.0)
Incremented for new features:
- Commit type:
featorfeature
feat: add user export functionalityResult: 1.0.0 → 1.1.0
Patch Version (0.0.X)
Incremented for bug fixes and performance improvements:
- Commit type:
fix - Commit type:
perf
fix: resolve login page styling issue
perf: optimize database queriesResult: 1.0.0 → 1.0.1
Prerelease Versions
On non-main branches, versions include a prerelease identifier:
Format: {version}-{suffix}.{number}
# On develop branch
glsemver next
# Output: 1.1.0-develop.1
# On feature/awesome branch
glsemver next
# Output: 1.1.0-feature-awesome.1
# Custom suffix
glsemver next --suffix beta
# Output: 1.1.0-beta.1No Version Bump
When commits don't match Conventional Commits patterns or only contain non-versioning types:
chore:,docs:,style:,refactor:,test:,build:,ci:
glsemver next
# Output: (empty string)Environment Variables
GITLAB_TOKEN
Set GITLAB_TOKEN to authenticate with GitLab API:
export GITLAB_TOKEN=your_gitlab_token_hereGet a token at: https://gitlab.com/-/profile/personal_access_tokens
In GitLab CI/CD, CI_JOB_TOKEN is automatically available and will be used if GITLAB_TOKEN is not set.
GITLAB_HOST
For self-hosted GitLab instances, set the host URL:
export GITLAB_HOST=https://gitlab.example.comIn GitLab CI/CD, CI_SERVER_URL is automatically used if GITLAB_HOST is not set.
GitLab CI/CD Environment Variables
The tool automatically uses these CI variables when available:
CI_COMMIT_BRANCH/CI_COMMIT_REF_NAME- Current branchCI_COMMIT_SHA- Current commit SHACI_JOB_TOKEN- API authenticationCI_SERVER_URL- GitLab instance URL
Examples
Basic Usage
# Get current version
glsemver current
# Output: 1.0.0
# Calculate next version
glsemver next
# Output: 1.1.0Explicit Repository (No Git Required)
# Specify repository explicitly
glsemver next --repo mygroup/myproject --branch main --sha abc1234
# With self-hosted GitLab
glsemver next --repo mygroup/myproject --host https://gitlab.example.com --branch mainIn GitLab CI/CD
# .gitlab-ci.yml
stages:
- version
- release
calculate-version:
stage: version
image: node:20
script:
- npm install -g @sharedutils/glsemver
- VERSION=$(glsemver next --log)
- |
if [ -z "$VERSION" ]; then
echo "No version bump needed"
exit 0
fi
- echo "VERSION=$VERSION" >> version.env
artifacts:
reports:
dotenv: version.env
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
create-release:
stage: release
image: node:20
needs:
- job: calculate-version
artifacts: true
script:
- |
if [ -z "$VERSION" ]; then
echo "No version to release"
exit 0
fi
- git config user.email "[email protected]"
- git config user.name "GitLab CI"
- git tag "v$VERSION"
- git push origin "v$VERSION"
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCHIn Package Scripts
{
"scripts": {
"version:current": "glsemver current",
"version:next": "glsemver next",
"version:check": "glsemver next || echo 'No version bump needed'"
}
}Version Bump Script
#!/bin/bash
VERSION=$(glsemver next)
if [ -z "$VERSION" ]; then
echo "No version bump required"
exit 0
fi
echo "Bumping version to $VERSION"
npm version $VERSION --no-git-tag-version
git add package.json
git commit -m "chore: bump version to $VERSION"
git tag "v$VERSION"
git push --follow-tagsTag Format
- ✅ Valid:
v1.0.0,v2.3.4,v10.20.30,v1.0.0-beta.1 - ❌ Invalid:
1.0.0,v1.0,release-v1.0.0
The tool only recognizes tags starting with v followed by semantic version (X.Y.Z).
Output versions do not include the v prefix.
Requirements
- Node.js >= 18.0.0
- GitLab API access (token required for private repositories)
- Git (optional, only needed if not providing --repo option)
License
ISC
Contributing
Issues and merge requests are welcome!
Credits
Inspired by semantic-release but designed to be:
- Lighter weight
- Zero configuration
- Focused on version calculation only
- Pure API-based for better CI/CD compatibility
