Fixed 'HEAD' in detected Git branch on Jenkins
This commit is contained in:
parent
57a86b544e
commit
e6e55a6c40
61
build.gradle
61
build.gradle
@ -163,48 +163,61 @@ 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
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
} 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) {
|
} 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 = '-'
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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`
|
||||||
|
Reference in New Issue
Block a user