Skip to content

Commit 819e54c

Browse files
authored
Merge pull request #228 from rand0m86/master
Added support for build number in Github Actions
2 parents 76d12c0 + 8856e4e commit 819e54c

File tree

5 files changed

+165
-39
lines changed

5 files changed

+165
-39
lines changed

src/main/java/pl/project13/core/GitCommitPropertyConstant.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,15 @@ public class GitCommitPropertyConstant {
108108
*
109109
* Currently supported CIs:
110110
* <ul>
111+
* <li>AWS CodeBuild</li>
112+
* <li>Azure DevOps</li>
111113
* <li>Bamboo</li>
114+
* <li>Bitbucket Pipelines</li>
115+
* <li>GitHub Actions</li>
116+
* <li>Gitlab CI (Gitlab &gt;8.10 &amp; Gitlab CI &gt;0.5)</li>
112117
* <li>Hudson/Jenkins</li>
113118
* <li>TeamCity</li>
114119
* <li>Travis</li>
115-
* <li>Gitlab CI (Gitlab &gt;8.10 &amp; Gitlab CI &gt;0.5)</li>
116-
* <li>Azure DevOps</li>
117-
* <li>AWS CodeBuild</li>
118-
* <li>Bitbucket Pipelines</li>
119120
* </ul>
120121
*/
121122
public static final String BUILD_NUMBER = "build.number";
@@ -127,10 +128,11 @@ public class GitCommitPropertyConstant {
127128
*
128129
* Currently supported CIs:
129130
* <ul>
130-
* <li>TeamCity</li>
131-
* <li>Travis</li>
132-
* <li>Gitlab CI (Gitlab &gt;11.0)</li>
133-
* <li>AWS CodeBuild</li>
131+
* <li>AWS CodeBuild</li>
132+
* <li>Gitlab CI (Gitlab &gt;11.0)</li>
133+
* <li>GitHub Actions</li>
134+
* <li>TeamCity</li>
135+
* <li>Travis</li>
134136
* </ul>
135137
*/
136138
public static final String BUILD_NUMBER_UNIQUE = "build.number.unique";

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package pl.project13.core.cibuild;
1919

20+
import pl.project13.core.GitCommitPropertyConstant;
2021
import pl.project13.core.log.LogInterface;
2122

2223
import javax.annotation.Nonnull;
@@ -39,7 +40,12 @@ public static boolean isActiveServer(Map<String, String> env) {
3940

4041
@Override
4142
void loadBuildNumber(@Nonnull Properties properties) {
42-
// This information is not reliably available on GitHub Actions
43+
String runId = env.getOrDefault("GITHUB_RUN_ID", "0");
44+
String runNumber = env.getOrDefault("GITHUB_RUN_NUMBER", "0");
45+
String runAttempt = env.getOrDefault("GITHUB_RUN_ATTEMPT", "0");
46+
47+
maybePut(properties, GitCommitPropertyConstant.BUILD_NUMBER, () -> String.join(".", runNumber, runAttempt));
48+
maybePut(properties, GitCommitPropertyConstant.BUILD_NUMBER_UNIQUE, () -> String.join(".", runId, runNumber, runAttempt));
4349
}
4450

4551
@Override

src/test/java/pl/project13/core/GitCommitIdTestCallback.java

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package pl.project13.core;
1919

2020
import pl.project13.core.git.GitDescribeConfig;
21+
import pl.project13.core.log.DummyLogInterface;
2122
import pl.project13.core.log.LogInterface;
2223
import pl.project13.core.util.BuildFileChangeListener;
2324

@@ -34,7 +35,7 @@
3435
public class GitCommitIdTestCallback {
3536
private Map<String, String> systemEnv = System.getenv();
3637
private String projectVersion = "dummy-version";
37-
private LogInterface logInterface = createDummyLogInterface();
38+
private LogInterface logInterface = new DummyLogInterface();
3839
private String dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ";
3940
private String dateFormatTimeZone = TimeZone.getDefault().getID();
4041
private String prefixDot = "git.";
@@ -362,33 +363,4 @@ private GitDescribeConfig createGitDescribeConfig(boolean forceLongFormat, int a
362363
gitDescribeConfig.setAbbrev(abbrev);
363364
return gitDescribeConfig;
364365
}
365-
366-
private LogInterface createDummyLogInterface() {
367-
return new LogInterface() {
368-
@Override
369-
public void debug(String msg) {
370-
// ignore
371-
}
372-
373-
@Override
374-
public void info(String msg) {
375-
// ignore
376-
}
377-
378-
@Override
379-
public void warn(String msg) {
380-
// ignore
381-
}
382-
383-
@Override
384-
public void error(String msg) {
385-
// ignore
386-
}
387-
388-
@Override
389-
public void error(String msg, Throwable t) {
390-
// ignore
391-
}
392-
};
393-
}
394366
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* This file is part of git-commit-id-plugin-core by Konrad 'ktoso' Malawski <konrad.malawski@java.pl>
3+
*
4+
* git-commit-id-plugin-core 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-core 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-core. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package pl.project13.core.cibuild;
19+
20+
import org.junit.jupiter.api.Nested;
21+
import org.junit.jupiter.api.Test;
22+
import pl.project13.core.GitCommitPropertyConstant;
23+
import pl.project13.core.log.DummyLogInterface;
24+
25+
import java.util.Map;
26+
import java.util.Properties;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
class BuildServerDataProviderTest {
31+
@Test
32+
void shouldSelectGithubAsDataProvider() {
33+
Map<String, String> environment = Map.of("GITHUB_ACTIONS", "true");
34+
35+
BuildServerDataProvider provider = BuildServerDataProvider.getBuildServerProvider(environment, new DummyLogInterface());
36+
37+
assertThat(provider).isInstanceOf(GitHubBuildServerData.class);
38+
}
39+
40+
@Nested
41+
class GithubProviderTests {
42+
@Test
43+
void shouldVerifyOnGithubEnvironment() {
44+
Map<String, String> environment = Map.of("GITHUB_ACTIONS", "true");
45+
46+
assertThat(GitHubBuildServerData.isActiveServer(environment)).isTrue();
47+
}
48+
49+
@Test
50+
void shouldLoadBuildNumber() {
51+
Properties properties = new Properties();
52+
Map<String, String> environment = Map.of(
53+
"GITHUB_RUN_ID", "1658821493",
54+
"GITHUB_RUN_NUMBER", "123",
55+
"GITHUB_RUN_ATTEMPT", "1");
56+
GitHubBuildServerData provider = new GitHubBuildServerData(new DummyLogInterface(), environment);
57+
58+
provider.loadBuildNumber(properties);
59+
60+
assertThat(properties).containsEntry(GitCommitPropertyConstant.BUILD_NUMBER, "123.1");
61+
assertThat(properties).containsEntry(GitCommitPropertyConstant.BUILD_NUMBER_UNIQUE, "1658821493.123.1");
62+
}
63+
64+
@Test
65+
void shouldLoadBuildNumberAsZerosIfNotAvailable() {
66+
Properties properties = new Properties();
67+
Map<String, String> environment = Map.of();
68+
GitHubBuildServerData provider = new GitHubBuildServerData(new DummyLogInterface(), environment);
69+
70+
provider.loadBuildNumber(properties);
71+
72+
assertThat(properties).containsEntry(GitCommitPropertyConstant.BUILD_NUMBER, "0.0");
73+
assertThat(properties).containsEntry(GitCommitPropertyConstant.BUILD_NUMBER_UNIQUE, "0.0.0");
74+
}
75+
76+
@Test
77+
void shouldLoadBranchNameForPullRequestBuild() {
78+
Map<String, String> environment = Map.of("GITHUB_REF", "refs/pull/feature_branch",
79+
"GITHUB_HEAD_REF", "feature_branch");
80+
GitHubBuildServerData provider = new GitHubBuildServerData(new DummyLogInterface(), environment);
81+
82+
assertThat(provider.getBuildBranch()).isEqualTo("feature_branch");
83+
}
84+
85+
@Test
86+
void shouldLoadBranchNameForBranchBuild() {
87+
Map<String, String> environment = Map.of("GITHUB_REF", "refs/heads/feature_branch");
88+
GitHubBuildServerData provider = new GitHubBuildServerData(new DummyLogInterface(), environment);
89+
90+
assertThat(provider.getBuildBranch()).isEqualTo("feature_branch");
91+
}
92+
93+
@Test
94+
void shouldLoadBranchNameAsEmptyIfNotAvailable() {
95+
Map<String, String> environment = Map.of();
96+
GitHubBuildServerData provider = new GitHubBuildServerData(new DummyLogInterface(), environment);
97+
98+
assertThat(provider.getBuildBranch()).isEmpty();
99+
}
100+
}
101+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* This file is part of git-commit-id-plugin-core by Konrad 'ktoso' Malawski <konrad.malawski@java.pl>
3+
*
4+
* git-commit-id-plugin-core 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-core 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-core. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package pl.project13.core.log;
19+
20+
public class DummyLogInterface implements LogInterface {
21+
@Override
22+
public void debug(String msg) {
23+
// ignore
24+
}
25+
26+
@Override
27+
public void info(String msg) {
28+
// ignore
29+
}
30+
31+
@Override
32+
public void warn(String msg) {
33+
// ignore
34+
}
35+
36+
@Override
37+
public void error(String msg) {
38+
// ignore
39+
}
40+
41+
@Override
42+
public void error(String msg, Throwable t) {
43+
// ignore
44+
}
45+
}

0 commit comments

Comments
 (0)