Skip to content

Added support for build number in Github Actions #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/main/java/pl/project13/core/GitCommitPropertyConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,15 @@ public class GitCommitPropertyConstant {
*
* Currently supported CIs:
* <ul>
* <li>AWS CodeBuild</li>
* <li>Azure DevOps</li>
* <li>Bamboo</li>
* <li>Bitbucket Pipelines</li>
* <li>GitHub Actions</li>
* <li>Gitlab CI (Gitlab &gt;8.10 &amp; Gitlab CI &gt;0.5)</li>
* <li>Hudson/Jenkins</li>
* <li>TeamCity</li>
* <li>Travis</li>
* <li>Gitlab CI (Gitlab &gt;8.10 &amp; Gitlab CI &gt;0.5)</li>
* <li>Azure DevOps</li>
* <li>AWS CodeBuild</li>
* <li>Bitbucket Pipelines</li>
* </ul>
*/
public static final String BUILD_NUMBER = "build.number";
Expand All @@ -127,10 +128,11 @@ public class GitCommitPropertyConstant {
*
* Currently supported CIs:
* <ul>
* <li>TeamCity</li>
* <li>Travis</li>
* <li>Gitlab CI (Gitlab &gt;11.0)</li>
* <li>AWS CodeBuild</li>
* <li>AWS CodeBuild</li>
* <li>Gitlab CI (Gitlab &gt;11.0)</li>
* <li>GitHub Actions</li>
* <li>TeamCity</li>
* <li>Travis</li>
* </ul>
*/
public static final String BUILD_NUMBER_UNIQUE = "build.number.unique";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package pl.project13.core.cibuild;

import pl.project13.core.GitCommitPropertyConstant;
import pl.project13.core.log.LogInterface;

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

@Override
void loadBuildNumber(@Nonnull Properties properties) {
// This information is not reliably available on GitHub Actions
String runId = env.getOrDefault("GITHUB_RUN_ID", "0");
String runNumber = env.getOrDefault("GITHUB_RUN_NUMBER", "0");
String runAttempt = env.getOrDefault("GITHUB_RUN_ATTEMPT", "0");

maybePut(properties, GitCommitPropertyConstant.BUILD_NUMBER, () -> String.join(".", runNumber, runAttempt));
maybePut(properties, GitCommitPropertyConstant.BUILD_NUMBER_UNIQUE, () -> String.join(".", runId, runNumber, runAttempt));
}

@Override
Expand Down
32 changes: 2 additions & 30 deletions src/test/java/pl/project13/core/GitCommitIdTestCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package pl.project13.core;

import pl.project13.core.git.GitDescribeConfig;
import pl.project13.core.log.DummyLogInterface;
import pl.project13.core.log.LogInterface;
import pl.project13.core.util.BuildFileChangeListener;

Expand All @@ -34,7 +35,7 @@
public class GitCommitIdTestCallback {
private Map<String, String> systemEnv = System.getenv();
private String projectVersion = "dummy-version";
private LogInterface logInterface = createDummyLogInterface();
private LogInterface logInterface = new DummyLogInterface();
private String dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ";
private String dateFormatTimeZone = TimeZone.getDefault().getID();
private String prefixDot = "git.";
Expand Down Expand Up @@ -362,33 +363,4 @@ private GitDescribeConfig createGitDescribeConfig(boolean forceLongFormat, int a
gitDescribeConfig.setAbbrev(abbrev);
return gitDescribeConfig;
}

private LogInterface createDummyLogInterface() {
return new LogInterface() {
@Override
public void debug(String msg) {
// ignore
}

@Override
public void info(String msg) {
// ignore
}

@Override
public void warn(String msg) {
// ignore
}

@Override
public void error(String msg) {
// ignore
}

@Override
public void error(String msg, Throwable t) {
// ignore
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* This file is part of git-commit-id-plugin-core by Konrad 'ktoso' Malawski <[email protected]>
*
* git-commit-id-plugin-core is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* git-commit-id-plugin-core is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with git-commit-id-plugin-core. If not, see <http://www.gnu.org/licenses/>.
*/

package pl.project13.core.cibuild;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import pl.project13.core.GitCommitPropertyConstant;
import pl.project13.core.log.DummyLogInterface;

import java.util.Map;
import java.util.Properties;

import static org.assertj.core.api.Assertions.assertThat;

class BuildServerDataProviderTest {
@Test
void shouldSelectGithubAsDataProvider() {
Map<String, String> environment = Map.of("GITHUB_ACTIONS", "true");

BuildServerDataProvider provider = BuildServerDataProvider.getBuildServerProvider(environment, new DummyLogInterface());

assertThat(provider).isInstanceOf(GitHubBuildServerData.class);
}

@Nested
class GithubProviderTests {
@Test
void shouldVerifyOnGithubEnvironment() {
Map<String, String> environment = Map.of("GITHUB_ACTIONS", "true");

assertThat(GitHubBuildServerData.isActiveServer(environment)).isTrue();
}

@Test
void shouldLoadBuildNumber() {
Properties properties = new Properties();
Map<String, String> environment = Map.of(
"GITHUB_RUN_ID", "1658821493",
"GITHUB_RUN_NUMBER", "123",
"GITHUB_RUN_ATTEMPT", "1");
GitHubBuildServerData provider = new GitHubBuildServerData(new DummyLogInterface(), environment);

provider.loadBuildNumber(properties);

assertThat(properties).containsEntry(GitCommitPropertyConstant.BUILD_NUMBER, "123.1");
assertThat(properties).containsEntry(GitCommitPropertyConstant.BUILD_NUMBER_UNIQUE, "1658821493.123.1");
}

@Test
void shouldLoadBuildNumberAsZerosIfNotAvailable() {
Properties properties = new Properties();
Map<String, String> environment = Map.of();
GitHubBuildServerData provider = new GitHubBuildServerData(new DummyLogInterface(), environment);

provider.loadBuildNumber(properties);

assertThat(properties).containsEntry(GitCommitPropertyConstant.BUILD_NUMBER, "0.0");
assertThat(properties).containsEntry(GitCommitPropertyConstant.BUILD_NUMBER_UNIQUE, "0.0.0");
}

@Test
void shouldLoadBranchNameForPullRequestBuild() {
Map<String, String> environment = Map.of("GITHUB_REF", "refs/pull/feature_branch",
"GITHUB_HEAD_REF", "feature_branch");
GitHubBuildServerData provider = new GitHubBuildServerData(new DummyLogInterface(), environment);

assertThat(provider.getBuildBranch()).isEqualTo("feature_branch");
}

@Test
void shouldLoadBranchNameForBranchBuild() {
Map<String, String> environment = Map.of("GITHUB_REF", "refs/heads/feature_branch");
GitHubBuildServerData provider = new GitHubBuildServerData(new DummyLogInterface(), environment);

assertThat(provider.getBuildBranch()).isEqualTo("feature_branch");
}

@Test
void shouldLoadBranchNameAsEmptyIfNotAvailable() {
Map<String, String> environment = Map.of();
GitHubBuildServerData provider = new GitHubBuildServerData(new DummyLogInterface(), environment);

assertThat(provider.getBuildBranch()).isEmpty();
}
}
}
45 changes: 45 additions & 0 deletions src/test/java/pl/project13/core/log/DummyLogInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* This file is part of git-commit-id-plugin-core by Konrad 'ktoso' Malawski <[email protected]>
*
* git-commit-id-plugin-core is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* git-commit-id-plugin-core is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with git-commit-id-plugin-core. If not, see <http://www.gnu.org/licenses/>.
*/

package pl.project13.core.log;

public class DummyLogInterface implements LogInterface {
@Override
public void debug(String msg) {
// ignore
}

@Override
public void info(String msg) {
// ignore
}

@Override
public void warn(String msg) {
// ignore
}

@Override
public void error(String msg) {
// ignore
}

@Override
public void error(String msg, Throwable t) {
// ignore
}
}
Loading