- Generate a version from git tags and commits
- Version is cached in a file to avoid expensive git operations
- Schema is based on semver
- Compatible with gitflow
- Portable, works on Linux and Windows
Version are generated from git tags and commits. Format is "[major].{minor}.[patch]-[commit amout since last tag].[commit id]+[timestamp]".
Timestamp is only added if the version is not cached. Timestamp is added to avoid integrity issues when executing the script multiple times.
Linux:
# Add this repo as a submodule, from the root of your repo
❯ git submodule add -b master https://github.com/clemlesne/gitops-version cicd/versionIn GitHub, Dependabot can automatically create you pull requests for new versions:
# .github/dependabot.yml version: 2 updates: - package-ecosystem: gitsubmodule schedule: interval: daily directory: cicd/version
❯ sh version.sh -g .
0.2.11-44.630dcd2❯ .\version.ps1 -g . 0.2.11-44.630dcd2
Examples:
# Get short version from current commit ❯ sh version.sh -g . 0.2.11-44.630dcd2 # Get long version from current commit ❯ sh version.sh -g . -m 0.2.11-44.630dcd2+20230327090732 # Get the long cached version from commit ❯ sh version.sh -g . -m -c 0.2.11-44.630dcd2+20230327090732 # Get the version from a repo stored in another folder ❯ sh version.sh -g my_folder/ 0.7.2
In your Makefile:
version: @bash cicd/version/version.sh -g . -c version-full: @bash cicd/version/version.sh -g . -c -m
And then, in your CI:
❯ make version-full 0.7.3-21.00736a8+20230327092242
Gradle (Java, Kotlin, Groovy, Scala, C/C++, JavaScript)
In your build.gradle:
if (project.hasProperty('projVersion')) { project.version = project.projVersion } else { project.version = 'unknown' }
And then, in your CI:
# Build a Gradle project with the version ❯ gradle -PprojVersion=$(sh version.sh -g . -m -c) build