diff --git a/build.gradle b/build.gradle index f788224..4156ac5 100644 --- a/build.gradle +++ b/build.gradle @@ -163,48 +163,61 @@ task resolveVersion { description 'Resolves version information from Git repository or project properties.' doFirst { + + project.ext.commit = System.env.GIT_COMMIT + project.ext.branch = System.env.GIT_BRANCH + try { def git = Grgit.open(dir: project.projectDir) - project.ext.commit = git.head().id - project.ext.branch = git.branch.current().name + project.ext.commit = commit ?: git.head().id + project.ext.branch = branch ?: git.branch.current().name if (project.version != 'unspecified') { // Leave version as-is - return - } - - def tag = version_findRelevantTag(git) - if (tag == null) { - - String suffix - if (project.hasProperty('buildId')) { - suffix = project.buildId; - } else { - suffix = java.time.ZonedDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern('yyyy_MM_dd')) - } - project.version = "999.0.0-$suffix" - - logger.warn 'Git repository does not contain an applicable tag, using dummy version {}\nSpecify version with -Pversion=1.2.3 or create a Git tag named v1.2.3', project.version - } else { - project.version = version_parseVersion(tag.name, tag.commit != git.head()) + // Resolve version from Git + def tag = version_findRelevantTag(git) + if (tag == null) { + String suffix + if (project.hasProperty('buildId')) { + suffix = project.buildId; + } else { + suffix = java.time.ZonedDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern('yyyy_MM_dd')) + } + project.version = "999.0.0-$suffix" + + logger.warn 'Git repository does not contain an applicable tag, using dummy version {}\nSpecify version with -Pversion=1.2.3 or create a Git tag named v1.2.3', project.version + + } else { + + project.version = version_parseVersion(tag.name, tag.commit != git.head()) + + } + } } catch (org.eclipse.jgit.errors.RepositoryNotFoundException e) { if (project.version == 'unspecified') project.version = 'dev' - project.ext.commit = '-' - project.ext.branch = '-' + project.ext.commit = commit ?: '-' + project.ext.branch = branch ?: '-' logger.warn 'No Git repository found in project root, using dummy version {}\nSpecify version with -Pversion=1.2.3 or create a Git tag named v1.2.3', project.version } + + if (branch.contains '/') { + // Strip remote - no one wants that + project.ext.branch = branch.takeAfter '/' + } + + if (!project.hasProperty('buildId')) { + project.ext.buildId = '-' + } } doLast { - if (!project.hasProperty('buildId')) { - project.ext.buildId = '-' - } + } } diff --git a/docs/building/BuildScriptReference.md b/docs/building/BuildScriptReference.md index 283d3b6..721638b 100644 --- a/docs/building/BuildScriptReference.md +++ b/docs/building/BuildScriptReference.md @@ -60,14 +60,21 @@ In all other cases, a fallback dummy value is used for version, appended with bu ### Git metadata -Git commit and Git branch are correspond to the state of the local Git repository, if any. In case Git metadata is -unavailable, `-` fallback is used for both fields. +Git commit is determined from `GIT_COMMIT` environment variable if it exists, or the state of the local Git +repository, if any, or `-`. + +Git branch is determined from `GIT_BRANCH` environment variable if it exists, or the state of the local Git +repository, if any, or `-`. + +The names of the environment variables are picked to assist Jenkins builds. ### Build ID -Build ID uniquely identifies artifacts produced by automated build systems. For example, builds executed by WindCorp -Jenkins suite have build IDs like `WJ3` or `WJ142`. Build ID must be provided explicitly; it is `-` unless specified -otherwise. +Build ID uniquely identifies artifacts produced by automated build systems. Build ID must be provided explicitly; it +is `-` unless specified otherwise. + +The proposed scheme for naming build IDs is ``. For example, builds +executed by WindCorp Jenkins suite have build IDs like `WJ3` or `WJ142`. Build ID may be set with `buildId` project property. This may be done in a variety of ways, for example with command line argument `-PbuildId=WJ3`