Skip to content

Commit 2b68367

Browse files
authored
Merge pull request #539 from grantfin/bitbucket-pipelines-support
Support Bitbucket Pipelines CI for build number and branch
2 parents 8e073ae + c4d2ca5 commit 2b68367

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* This file is part of git-commit-id-plugin by Konrad 'ktoso' Malawski <konrad.malawski@java.pl>
3+
*
4+
* git-commit-id-plugin is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* git-commit-id-plugin is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with git-commit-id-plugin. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package pl.project13.core.cibuild;
19+
20+
import pl.project13.core.GitCommitPropertyConstant;
21+
import pl.project13.core.log.LoggerBridge;
22+
23+
import java.util.Map;
24+
import java.util.Optional;
25+
import java.util.Properties;
26+
import javax.annotation.Nonnull;
27+
28+
public class BitbucketBuildServerData extends BuildServerDataProvider {
29+
30+
BitbucketBuildServerData(LoggerBridge log, @Nonnull Map<String, String> env) {
31+
super(log, env);
32+
}
33+
34+
/**
35+
* @param env The current system environment variables, obtained via System.getenv().
36+
* @return true, if the system environment variables contain the Bitbucket specific environment variable; false otherwise
37+
* @see <a href="https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/">Bitbucket Variables</a>
38+
*/
39+
public static boolean isActiveServer(Map<String, String> env) {
40+
return env.containsKey("BITBUCKET_BUILD_NUMBER");
41+
}
42+
43+
@Override
44+
void loadBuildNumber(@Nonnull Properties properties) {
45+
String buildNumber = Optional.ofNullable(env.get("BITBUCKET_BUILD_NUMBER")).orElse("");
46+
47+
maybePut(properties, GitCommitPropertyConstant.BUILD_NUMBER, () -> buildNumber);
48+
}
49+
50+
@Override
51+
public String getBuildBranch() {
52+
String environmentBasedKey = null;
53+
54+
String envKey = "BITBUCKET_BRANCH";
55+
String environmentBasedBranch = env.get(envKey);
56+
if (environmentBasedBranch != null) {
57+
environmentBasedKey = envKey;
58+
}
59+
log.info("Using environment variable based branch name. {} = {}", environmentBasedKey, environmentBasedBranch);
60+
return environmentBasedBranch;
61+
}
62+
}

core/src/main/java/pl/project13/core/cibuild/BuildServerDataProvider.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ public static BuildServerDataProvider getBuildServerProvider(@Nonnull Map<String
110110
if (AwsCodeBuildBuildServerData.isActiveServer(env)) {
111111
return new AwsCodeBuildBuildServerData(log, env);
112112
}
113+
if (BitbucketBuildServerData.isActiveServer(env)) {
114+
return new BitbucketBuildServerData(log, env);
115+
}
113116
return new UnknownBuildServerData(log, env);
114117
}
115118

maven/docs/using-the-plugin.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,6 @@ Refer to the table below to see which values are supported by which CIs.
914914

915915
| variable | description | supported CIs |
916916
| ------------------------- | ----------------------------------------|:---------------------------------------------------------:|
917-
|`git.build.number` | holds a project specific build number | Bamboo, Hudson, Jenkins, TeamCity, Travis, Gitlab CI (Gitlab >8.10 & Gitlab CI >0.5), Azure DevOps, AWS CodeBuild |
917+
|`git.build.number` | holds a project specific build number | Bamboo, Hudson, Jenkins, TeamCity, Travis, Gitlab CI (Gitlab >8.10 & Gitlab CI >0.5), Azure DevOps, AWS CodeBuild, Bitbucket Pipelines |
918918
|`git.build.number.unique` | holds a system wide unique build number | TeamCity, Travis, Gitlab CI (Gitlab >11.0), AWS CodeBuild |
919919

0 commit comments

Comments
 (0)