Skip to content

Commit 88c876a

Browse files
author
TheSnoozer
authored
Merge pull request #369 from secustor/implement_build_number
add build number support (git.build.number & git.build.number.unique)
2 parents 99fab40 + cc53594 commit 88c876a

11 files changed

+413
-76
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,8 @@ git.build.user.email=${git.build.user.email}
559559
git.build.time=${git.build.time}
560560
git.build.host=${git.build.host}
561561
git.build.version=${git.build.version}
562+
git.build.number=${git.build.number}
563+
git.build.number.unique=${git.build.number.unique}
562564
```
563565

564566
The `git` prefix may be configured in the plugin declaration above.
@@ -598,6 +600,8 @@ Start out with with adding the above steps to your project, next paste this **gi
598600
<property name="buildTime" value="${git.build.time}"/>
599601
<property name="buildHost" value="${git.build.host}"/>
600602
<property name="buildVersion" value="${git.build.version}"/>
603+
<property name="buildNumber" value="${git.build.number}"/>
604+
<property name="buildNumberUnique" value="${git.build.number.unique}"/>
601605
</bean>
602606
</beans>
603607
```
@@ -637,7 +641,9 @@ public class GitRepositoryState {
637641
String buildUserEmail; // =${git.build.user.email}
638642
String buildTime; // =${git.build.time}
639643
String buildHost; // =${git.build.host}
640-
String buildVersion // =${git.build.version}
644+
String buildVersion; // =${git.build.version}
645+
String buildNumber; // =${git.build.number}
646+
String buildNumberUnique; // =${git.build.number.unique}
641647

642648
public GitRepositoryState() {
643649
}
@@ -763,6 +769,8 @@ public GitRepositoryState(Properties properties)
763769
this.buildTime = String.valueOf(properties.get("git.build.time"));
764770
this.buildHost = String.valueOf(properties.get("git.build.host"));
765771
this.buildVersion = String.valueOf(properties.get("git.build.version"));
772+
this.buildNumber = String.valueOf(properties.get("git.build.number"));
773+
this.buildNumberUnique = String.valueOf(properties.get("git.build.number.unique"));
766774
}
767775
```
768776

@@ -780,6 +788,17 @@ If you need the actual *build time* then simply use the a filtered properties fi
780788
git.build.time=${git.build.time}
781789
```
782790

791+
Note on git.build.number variables
792+
------------------------------------
793+
The `git.build.number` variables are available on some hosted CIs and can be used to identify the "number" of the build.
794+
Refer to the table below to see which values are supported by which CIs.
795+
796+
797+
| variable | description | supported CIs |
798+
| ------------------------- | ----------------------------------------|:---------------------------------------------------------:|
799+
|`git.build.number` | holds a project specific build number | Bamboo, Hudson, Jenkins, TeamCity, Travis, Gitlab CI (Gitlab >8.10 & Gitlab CI >0.5)|
800+
|`git.build.number.unique` | holds a system wide unique build number | TeamCity, Travis, Gitlab CI (Gitlab >11.0) |
801+
783802
Yet another way to use the plugin
784803
=================================
785804

src/main/java/pl/project13/maven/git/GitCommitIdMojo.java

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,13 @@
2525
import java.io.InputStreamReader;
2626
import java.io.OutputStreamWriter;
2727
import java.io.Writer;
28-
import java.net.InetAddress;
29-
import java.net.UnknownHostException;
3028
import java.nio.charset.Charset;
3129
import java.nio.charset.StandardCharsets;
3230
import java.text.SimpleDateFormat;
33-
import java.util.Date;
3431
import java.util.HashMap;
3532
import java.util.List;
3633
import java.util.Map;
3734
import java.util.Properties;
38-
import java.util.TimeZone;
3935
import java.util.regex.Pattern;
4036

4137
import org.apache.maven.execution.MavenSession;
@@ -55,6 +51,7 @@
5551
import com.google.common.io.Files;
5652
import java.io.OutputStream;
5753

54+
import pl.project13.maven.git.build.BuildServerDataProvider;
5855
import pl.project13.maven.git.log.LoggerBridge;
5956
import pl.project13.maven.git.log.MavenLoggerBridge;
6057
import pl.project13.maven.git.util.PropertyManager;
@@ -308,12 +305,12 @@ public class GitCommitIdMojo extends AbstractMojo {
308305
/**
309306
* Allow to tell the plugin what commit should be used as reference to generate the properties from.
310307
* By default this property is simply set to <p>HEAD</p> which should reference to the latest commit in your repository.
311-
*
308+
*
312309
* In general this property can be set to something generic like <p>HEAD^1</p> or point to a branch or tag-name.
313310
* To support any kind or use-case this configuration can also be set to an entire commit-hash or it's abbreviated version.
314-
*
311+
*
315312
* A use-case for this feature can be found in https://github.com/ktoso/maven-git-commit-id-plugin/issues/338.
316-
*
313+
*
317314
* Please note that for security purposes not all references might be allowed as configuration.
318315
* If you have a specific use-case that is currently not white listed feel free to file an issue.
319316
* @since 2.2.4
@@ -406,8 +403,7 @@ public void execute() throws MojoExecutionException {
406403
prefixDot = trimmedPrefix.equals("") ? "" : trimmedPrefix + ".";
407404

408405
loadGitData(properties);
409-
loadBuildVersionAndTimeData(properties);
410-
loadBuildHostData(properties);
406+
loadBuildData(properties);
411407
loadShortDescribe(properties);
412408
propertiesReplacer.performReplacement(properties, replacementProperties);
413409
propertiesFilterer.filter(properties, includeOnlyProperties, this.prefixDot);
@@ -430,6 +426,17 @@ public void execute() throws MojoExecutionException {
430426
}
431427
}
432428

429+
private void loadBuildData(Properties properties) {
430+
BuildServerDataProvider buildServerDataProvider = BuildServerDataProvider.getBuildServerProvider(System.getenv(),log);
431+
buildServerDataProvider
432+
.setDateFormat(dateFormat)
433+
.setDateFormatTimeZone(dateFormatTimeZone)
434+
.setProject(project)
435+
.setPrefixDot(prefixDot);
436+
437+
buildServerDataProvider.loadBuildData(properties);
438+
}
439+
433440
private void publishPropertiesInto(MavenProject target) {
434441
target.getProperties().putAll(properties);
435442
}
@@ -476,26 +483,6 @@ private void logProperties() {
476483
}
477484
}
478485

479-
void loadBuildVersionAndTimeData(@NotNull Properties properties) {
480-
Date buildDate = new Date();
481-
SimpleDateFormat smf = new SimpleDateFormat(dateFormat);
482-
if (dateFormatTimeZone != null) {
483-
smf.setTimeZone(TimeZone.getTimeZone(dateFormatTimeZone));
484-
}
485-
put(properties, GitCommitPropertyConstant.BUILD_TIME, smf.format(buildDate));
486-
put(properties, GitCommitPropertyConstant.BUILD_VERSION, project.getVersion());
487-
}
488-
489-
void loadBuildHostData(@NotNull Properties properties) {
490-
String buildHost = null;
491-
try {
492-
buildHost = InetAddress.getLocalHost().getHostName();
493-
} catch (UnknownHostException e) {
494-
log.info("Unable to get build host, skipping property {}. Error message: {}", GitCommitPropertyConstant.BUILD_HOST, e.getMessage());
495-
}
496-
put(properties, GitCommitPropertyConstant.BUILD_HOST, buildHost);
497-
}
498-
499486
void loadShortDescribe(@NotNull Properties properties) {
500487
//removes git hash part from describe
501488
String commitDescribe = properties.getProperty(prefixDot + GitCommitPropertyConstant.COMMIT_DESCRIBE);

src/main/java/pl/project13/maven/git/GitCommitPropertyConstant.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class GitCommitPropertyConstant {
1515
public static final String BUILD_TIME = "build.time";
1616
public static final String BUILD_VERSION = "build.version";
1717
public static final String BUILD_HOST = "build.host";
18+
public static final String BUILD_NUMBER = "build.number";
19+
public static final String BUILD_NUMBER_UNIQUE = "build.number.unique";
1820
public static final String COMMIT_AUTHOR_NAME = "commit.user.name";
1921
public static final String COMMIT_AUTHOR_EMAIL = "commit.user.email";
2022
public static final String COMMIT_MESSAGE_FULL = "commit.message.full";

src/main/java/pl/project13/maven/git/GitDataProvider.java

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import org.apache.http.client.utils.URIBuilder;
2121
import org.jetbrains.annotations.NotNull;
22+
import pl.project13.maven.git.build.BuildServerDataProvider;
23+
import pl.project13.maven.git.build.UnknownBuildServerData;
2224
import pl.project13.maven.git.log.LoggerBridge;
2325
import pl.project13.maven.git.util.PropertyManager;
2426

@@ -167,57 +169,20 @@ void validateAbbrevLength(int abbrevLength) throws GitCommitIdExecutionException
167169
* @param env environment settings
168170
* @return results of getBranchName() or, if in Jenkins/Hudson, value of GIT_BRANCH
169171
*/
170-
protected String determineBranchName(Map<String, String> env) throws GitCommitIdExecutionException {
171-
if (runningOnBuildServer(env)) {
172-
return determineBranchNameOnBuildServer(env);
172+
protected String determineBranchName(@NotNull Map<String, String> env) throws GitCommitIdExecutionException {
173+
BuildServerDataProvider buildServerDataProvider = BuildServerDataProvider.getBuildServerProvider(env,log);
174+
if (!(buildServerDataProvider instanceof UnknownBuildServerData)) {
175+
String branchName = buildServerDataProvider.getBuildBranch();
176+
if (isNullOrEmpty(branchName)) {
177+
log.info("Detected that running on CI environment, but using repository branch, no GIT_BRANCH detected.");
178+
return getBranchName();
179+
}
180+
return branchName;
173181
} else {
174182
return getBranchName();
175183
}
176184
}
177185

178-
/**
179-
* Detects if we're running on Jenkins or Hudson, based on expected env variables.
180-
*
181-
* TODO: How can we detect Bamboo, TeamCity etc? Pull requests welcome.
182-
*
183-
* @param env environment settings
184-
* @return true if running
185-
* @see <a href="https://wiki.jenkins-ci.org/display/JENKINS/Building+a+software+project#Buildingasoftwareproject-JenkinsSetEnvironmentVariables">JenkinsSetEnvironmentVariables</a>
186-
* @see <a href="https://docs.gitlab.com/ce/ci/variables/#predefined-variables-environment-variables">GitlabCIVariables</a>
187-
*/
188-
private boolean runningOnBuildServer(Map<String, String> env) {
189-
return env.containsKey("HUDSON_URL") || env.containsKey("JENKINS_URL") || env.containsKey("CI") ||
190-
env.containsKey("HUDSON_HOME") || env.containsKey("JENKINS_HOME");
191-
}
192-
193-
/**
194-
* Is "Jenkins aware", and prefers {@code GIT_LOCAL_BRANCH} over {@code GIT_BRANCH} to getting the branch via git if that environment variables are set.
195-
* The {@code GIT_LOCAL_BRANCH} and {@code GIT_BRANCH} variables are set by Jenkins/Hudson when put in detached HEAD state, but it still knows which branch was cloned.
196-
* If the above fails the environment will be checked for a Gitlab CI environment (>0.4 Runner && >9.0 Gitlab). Is this the case the {@code CI_COMMIT_REF_NAME} will be read out which holds the branch name or tag name if a tag is built.
197-
*/
198-
protected String determineBranchNameOnBuildServer(Map<String, String> env) throws GitCommitIdExecutionException {
199-
String environmentBasedLocalBranch = env.get("GIT_LOCAL_BRANCH");
200-
if (!isNullOrEmpty(environmentBasedLocalBranch)) {
201-
log.info("Using environment variable based branch name. GIT_LOCAL_BRANCH = {}", environmentBasedLocalBranch);
202-
return environmentBasedLocalBranch;
203-
}
204-
205-
String environmentBasedBranch = env.get("GIT_BRANCH");
206-
if (!isNullOrEmpty(environmentBasedBranch)) {
207-
log.info("Using environment variable based branch name. GIT_BRANCH = {}", environmentBasedBranch);
208-
return environmentBasedBranch;
209-
}
210-
211-
environmentBasedBranch = env.get("CI_COMMIT_REF_NAME");
212-
if (!isNullOrEmpty(environmentBasedBranch)) {
213-
log.info("Using environment variable based branch name. CI_COMMIT_REF_NAME = {}", environmentBasedBranch);
214-
return environmentBasedBranch;
215-
}
216-
217-
log.info("Detected that running on CI environment, but using repository branch, no GIT_BRANCH detected.");
218-
return getBranchName();
219-
}
220-
221186
protected SimpleDateFormat getSimpleDateFormatWithTimeZone() {
222187
SimpleDateFormat smf = new SimpleDateFormat(dateFormat);
223188
if (dateFormatTimeZone != null) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package pl.project13.maven.git.build;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import pl.project13.maven.git.GitCommitPropertyConstant;
5+
import pl.project13.maven.git.log.LoggerBridge;
6+
7+
import java.util.Map;
8+
import java.util.Properties;
9+
10+
public class BambooBuildServerData extends BuildServerDataProvider {
11+
12+
BambooBuildServerData(LoggerBridge log, @NotNull Map<String, String> env) {
13+
super(log, env);
14+
}
15+
16+
public static boolean isActiveServer(Map<String, String> env) {
17+
return env.containsKey("BAMBOO_BUILDKEY");
18+
}
19+
20+
@Override
21+
void loadBuildNumber(@NotNull Properties properties) {
22+
String buildNumber = env.get("BAMBOO_BUILDNUMBER");
23+
24+
put(properties, GitCommitPropertyConstant.BUILD_NUMBER, buildNumber == null ? "" : buildNumber);
25+
}
26+
27+
@Override
28+
public String getBuildBranch() {
29+
String environmentBasedBranch = env.get("BAMBOO_PLANREPOSITORY_BRANCH");
30+
log.info("Using environment variable based branch name. BAMBOO_PLANREPOSITORY_BRANCH = {}", environmentBasedBranch);
31+
return environmentBasedBranch;
32+
}
33+
}

0 commit comments

Comments
 (0)