Skip to content

Commit

Permalink
build: Customize the logic to determine the version
Browse files Browse the repository at this point in the history
The previous implementation had the problem that pre-releases always
got the revision appended. Also, the logic to remove the pre-release
identifier did not work with versioned pre-releases like "RC1".

Fix this by completely customizing the logic to determine the version:

- Releases only contain the semantic version, like "1.2.3".
- Pre-releases additionally contain the pre-release version, like
  "1.2.3-RC1".
- Commits ahead of the last release tag get the commit count and
  revision appended, like "1.2.3-RC1.001.sha.0123456".

The implementation is more verbose than before but makes it easier to
understand how the version is constructed.

Signed-off-by: Martin Nonnenmacher <[email protected]>
  • Loading branch information
mnonnenmacher committed Oct 9, 2024
1 parent aa9ff24 commit e7631fd
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,26 @@ semver {
// Do not create an empty release commit when running the "releaseVersion" task.
createReleaseCommit = false

// Use "RC" instead of "SNAPSHOT" as the default pre-release identifier.
defaultPreRelease = "RC"

// Do not let untracked files bump the version or add a "-SNAPSHOT" suffix.
noDirtyCheck = true
}

// Only override a default version (which usually is "unspecified"), but not a custom version.
if (version == Project.DEFAULT_VERSION) {
version = semver.semVersion.takeIf { it.isPreRelease }
// To get rid of a build part's "+" prefix because Docker tags do not support it, use only the original "build"
// part as the "pre-release" part.
?.toString()?.replace("${semver.defaultPreRelease}+", "")
// Fall back to a plain version without pre-release or build parts.
?: semver.version
val semVersion = semver.semVersion

// Set the version based on the following rules:
// - If the current commit is tagged as a release (e.g., 1.2.3) or pre-release (e.g., 1.2.3-RC1), the version equals
// the tag.
// - If the current commit is ahead of the last release or pre-release tag, the version is the tag plus the
// pre-release identifier, the commit count, and the SHA. For example, "1.2.3-RC1.001.sha.0123456". Note that
// semantic versions would use a "+" instead of a "." in front of the commit count, but this version is also used
// as a Docker tag and Docker does not allow "+" in tags.
val shaLength = if (semVersion.commitCount > 0) 7 else 0
version = semVersion.toInfoVersionString(shaLength = shaLength).replace('+', '.')
}

logger.lifecycle("Building ORT Server version $version.")
Expand Down

0 comments on commit e7631fd

Please sign in to comment.