|
| 1 | +import org.apache.tools.ant.taskdefs.condition.Os |
| 2 | + |
| 3 | +def versionFromFile = file("VERSION").text.trim() |
| 4 | + |
| 5 | +static String getGithubVar(String name) { |
| 6 | + return System.getenv("GITHUB_${name}") |
| 7 | +} |
| 8 | + |
| 9 | +static boolean isWindows() { |
| 10 | + return Os.isFamily(Os.FAMILY_WINDOWS) |
| 11 | +} |
| 12 | + |
| 13 | +File getGitExecutable() { |
| 14 | + String gitExe = isWindows() ? "git.exe" : "git" |
| 15 | + return System.getenv("PATH")?.split(File.pathSeparator)?.collect {file("${it}/${gitExe}") }?.find {it.canExecute() } |
| 16 | +} |
| 17 | + |
| 18 | +String getBranchName() { |
| 19 | + File gitExe = getGitExecutable() |
| 20 | + |
| 21 | + if (gitExe == null) { |
| 22 | + logger.warn("Git not found on path") |
| 23 | + return "unknown" |
| 24 | + } |
| 25 | + |
| 26 | + return new ByteArrayOutputStream().withStream { out -> |
| 27 | + String githubActionsRef = getGithubVar("REF") |
| 28 | + if (githubActionsRef != null) { |
| 29 | + return githubActionsRef.trim().substring(11).replace('/', '_') |
| 30 | + } |
| 31 | + |
| 32 | + exec { |
| 33 | + executable gitExe |
| 34 | + workingDir rootDir |
| 35 | + args "rev-parse", "--abbrev-ref", "HEAD" |
| 36 | + standardOutput out |
| 37 | + } |
| 38 | + |
| 39 | + return out.toString("UTF-8").trim() |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +String getCommit() { |
| 44 | + File gitExe = getGitExecutable() |
| 45 | + |
| 46 | + if (gitExe == null) { |
| 47 | + logger.warn("Git not found on path") |
| 48 | + return "unknown" |
| 49 | + } |
| 50 | + |
| 51 | + return new ByteArrayOutputStream().withStream { out -> |
| 52 | + exec { |
| 53 | + executable gitExe |
| 54 | + workingDir rootDir |
| 55 | + args "rev-parse", "--short", "HEAD" |
| 56 | + standardOutput out |
| 57 | + } |
| 58 | + |
| 59 | + return out.toString("UTF-8").trim() |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +String branchName = getBranchName() |
| 65 | +switch(branchName) { |
| 66 | + case "master": |
| 67 | + version = versionFromFile |
| 68 | + break |
| 69 | + |
| 70 | + case "develop": |
| 71 | + version = "${versionFromFile}-SNAPSHOT" |
| 72 | + break |
| 73 | + |
| 74 | + default: |
| 75 | + version = "${versionFromFile}-${getCommit()}-UNSTABLE" |
| 76 | + break |
| 77 | +} |
0 commit comments