Fixed 'HEAD' in detected Git branch on Jenkins

This commit is contained in:
OLEGSHA 2022-01-10 23:48:10 +03:00
parent 57a86b544e
commit e6e55a6c40
Signed by: OLEGSHA
GPG Key ID: E57A4B08D64AFF7A
2 changed files with 49 additions and 29 deletions

View File

@ -163,17 +163,21 @@ task resolveVersion {
description 'Resolves version information from Git repository or project properties.' description 'Resolves version information from Git repository or project properties.'
doFirst { doFirst {
project.ext.commit = System.env.GIT_COMMIT
project.ext.branch = System.env.GIT_BRANCH
try { try {
def git = Grgit.open(dir: project.projectDir) def git = Grgit.open(dir: project.projectDir)
project.ext.commit = git.head().id project.ext.commit = commit ?: git.head().id
project.ext.branch = git.branch.current().name project.ext.branch = branch ?: git.branch.current().name
if (project.version != 'unspecified') { if (project.version != 'unspecified') {
// Leave version as-is // Leave version as-is
return } else {
}
// Resolve version from Git
def tag = version_findRelevantTag(git) def tag = version_findRelevantTag(git)
if (tag == null) { if (tag == null) {
@ -192,19 +196,28 @@ task resolveVersion {
project.version = version_parseVersion(tag.name, tag.commit != git.head()) project.version = version_parseVersion(tag.name, tag.commit != git.head())
} }
}
} catch (org.eclipse.jgit.errors.RepositoryNotFoundException e) { } catch (org.eclipse.jgit.errors.RepositoryNotFoundException e) {
if (project.version == 'unspecified') project.version = 'dev' if (project.version == 'unspecified') project.version = 'dev'
project.ext.commit = '-' project.ext.commit = commit ?: '-'
project.ext.branch = '-' 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 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 { doLast {
if (!project.hasProperty('buildId')) {
project.ext.buildId = '-'
}
} }
} }

View File

@ -60,14 +60,21 @@ In all other cases, a fallback dummy value is used for version, appended with bu
### Git metadata ### Git metadata
Git commit and Git branch are correspond to the state of the local Git repository, if any. In case Git metadata is Git commit is determined from `GIT_COMMIT` environment variable if it exists, or the state of the local Git
unavailable, `-` fallback is used for both fields. 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
Build ID uniquely identifies artifacts produced by automated build systems. For example, builds executed by WindCorp Build ID uniquely identifies artifacts produced by automated build systems. Build ID must be provided explicitly; it
Jenkins suite have build IDs like `WJ3` or `WJ142`. Build ID must be provided explicitly; it is `-` unless specified is `-` unless specified otherwise.
otherwise.
The proposed scheme for naming build IDs is `<builder><build-system><build attempt no.>`. 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 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` line argument `-PbuildId=WJ3`