goversion is a tool and library for managing semantic version bumps in your Go projects. It bumps a version.go file while creating a semantic version commit and tag.
It is intended for use with go tools that are consumed from source.
go generatecan generate go code from git commits, but it's too late to capture the current git tag into src during the generate step.- Build flags are useful for inserting version tags from git into binaries, however
go toolconsumes source code and not binaries. - By bumping
version.gobefore creating the version commit, tools consumed bygo toolare able to introspect version data using a simple and clean workflow
- Semantic Version Bumping: Support for bumping versions using keywords (major, minor, patch, premajor, preminor, prepatch, prerelease, and from-git) or setting an explicit version.
- Git Integration: Automatically stages updated files, commits changes with the new version as the commit message, and tags the commit with the new version.
- CLI and Library: Offers both a command-line interface for quick version updates and a library for integrating version management into your applications.
- Flexible Configuration: Specify the path to your version file and include additional files for Git staging.
- go.mod and package self reference updates Save yourself the hassle of updating the go.mod and pacakge self references.
- Generic Version Bumping: Bump versions in any text file (package.json, Cargo.toml, etc.) by finding and replacing the first semantic version.
- Post-bump Hooks: Run custom scripts after version bumping but before committing, with access to old and new version via environment variables.
Install via Go modules:
# Install go version as a tool
go get -tool github.com/bcomnes/goversion/v2The goversion CLI defaults to using ./version.go as the version file, but you can override this with the -version-file flag. Use the -file flag to specify additional files to be staged.
go tool github.com/bcomnes/goversion/v2 [flags] <version-bump>
-version-file: Path to the Go file containing the version declaration. (Default:./version.go)-file: Additional file to include in the commit. This flag can be used multiple times.-bump-file: Additional file to scan for the first semantic version and bump it. This flag can be used multiple times. Only valid semver strings are matched (no "v" prefix).-post-bump: Script to execute after version bump but before git commit. ReceivesGOVERSION_OLD_VERSIONandGOVERSION_NEW_VERSIONenvironment variables. Files created or modified by the script must be specified with-fileto be included in the commit.-version: Show the version of thegoversionCLI tool and exit.-help: Show usage instructions.
The <version-bump> argument can be:
-
Keywords for semantic bumps:
major– 1.2.3 → 2.0.0minor– 1.2.3 → 1.3.0patch– 1.2.3 → 1.2.4premajor– 1.2.3 → 2.0.0-0preminor– 1.2.3 → 1.3.0-0prepatch– 1.2.3 → 1.2.4-0prerelease– 1.2.3 → 1.2.4-0 (or bumps prerelease: 1.2.4-0 → 1.2.4-1)
-
Special source:
from-git– use the latest Git tag (e.g.v1.2.3) as the version.
-
Explicit version strings (must be valid semver):
1.2.3– set exact version2.0.0-alpha.1– set prerelease versiondev– special non-semver string that initializes the version file (used for bootstrapping)
The -bump-file flag allows you to bump versions in any text file by finding and replacing the first valid semantic version:
- Only matches strict semver format (no "v" prefix)
- Replaces only the first occurrence
- Works with any file format (JSON, TOML, YAML, etc.)
- Common use cases: package.json, Cargo.toml, pyproject.toml, extension manifests
The -post-bump flag runs a script after version bumping but before committing:
- Script receives environment variables:
GOVERSION_OLD_VERSION- the version before bumpingGOVERSION_NEW_VERSION- the new version after bumping
- Script output is displayed to the user
- If the script fails, the entire operation is aborted
- Files created/modified by the script must be explicitly included with
-file - Common use cases: generating docs, updating changelogs, building artifacts
# Bump patch version (1.2.3 → 1.2.4)
goversion patch
# Bump minor version (1.2.3 → 1.3.0)
goversion minor
# Bump pre-release version (1.2.4-0 → 1.2.4-1)
goversion prerelease
# Set an explicit version
goversion 2.0.0
# Set a prerelease version
goversion 2.1.0-beta.1
# Use version from Git tag
goversion from-git
# Include README.md in the commit
goversion -file=README.md patch
# Use a custom version file path
goversion -version-file=internal/version.go minor
# Bump version in package.json and Cargo.toml
goversion -bump-file=package.json -bump-file=Cargo.toml patch
# Run a post-bump script that generates docs
# Note: Files created by the script must be included with -file
goversion -post-bump=./scripts/update-docs.sh -file=docs/version.md minor
# Combine multiple features
goversion -version-file=./version.go -bump-file=package.json -post-bump=./update.sh -file=CHANGELOG.md patchThis command will:
- Bump the version in the given file.
- Stage the updated version file (plus any
-fileflags). - Commit with the new version as the commit message (no
vprefix). - Tag the commit with the new version (with
vprefix). - For major version bumps ≥ v2, update go.mod module path and rewrite self-imports.
Note: The working directory must be clean (no unstaged/uncommitted changes outside the listed files) or the command will fail to prevent accidental commits.
You can also integrate goversion into your Go programs. For example:
package main
import (
"fmt"
"log"
"github.com/bcomnes/goversion/v2/pkg"
)
func main() {
// Basic version bump
meta, err := goversion.Run("./version.go", "minor", []string{"./version.go"}, []string{}, "")
if err != nil {
log.Fatalf("version bump failed: %v", err)
}
fmt.Printf("Bumped from %s to %s\n", meta.OldVersion, meta.NewVersion)
// With additional files to bump
bumpFiles := []string{"package.json", "Cargo.toml"}
meta, err = goversion.Run("./version.go", "patch", []string{"./version.go"}, bumpFiles, "")
if err != nil {
log.Fatalf("version bump failed: %v", err)
}
// Dry run to see what would change
meta, err = goversion.DryRun("./version.go", "major", []string{"package.json"})
if err != nil {
log.Fatalf("dry run failed: %v", err)
}
fmt.Printf("Would update files: %v\n", meta.UpdatedFiles)
}For detailed API documentation, visit PkgGoDev.
This project is licensed under the MIT License.