Skip to content

Commit eee4901

Browse files
committed
Stamp a version when we do releases
This change populates OSS version when releasing in GitHub Change-Id: Ife1b2b5ccc0f240eb83a9a44e7656e272ef94e7b
1 parent 149dfc1 commit eee4901

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

.github/workflows/release.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ jobs:
2727
distribution: 'adopt'
2828
java-version: '21'
2929

30-
- name: Build with Bazel
31-
run: bazel build java/com/google/copybara/copybara_deploy.jar
32-
3330
- name: Get current date
3431
id: date
3532
run: |
3633
echo "date=$(date +%Y%m%d)" >> $GITHUB_ENV
3734
35+
- name: Build with Bazel
36+
run: bazel build java/com/google/copybara/copybara_deploy.jar --stamp --embed_label v${{ env.date }}
37+
3838
- name: Calculate SHA256 checksum
3939
id: checksum
4040
run: sha256sum bazel-bin/java/com/google/copybara/copybara_deploy.jar | awk '{print $1}' > copybara_deploy.jar.sha256

java/com/google/copybara/Main.java

+28-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import com.google.copybara.util.console.NoPromptConsole;
5858
import java.io.ByteArrayInputStream;
5959
import java.io.IOException;
60+
import java.io.InputStream;
6061
import java.nio.charset.StandardCharsets;
6162
import java.nio.file.FileSystem;
6263
import java.nio.file.FileSystems;
@@ -67,6 +68,7 @@
6768
import java.util.Arrays;
6869
import java.util.Map;
6970
import java.util.Optional;
71+
import java.util.Properties;
7072
import java.util.function.Consumer;
7173
import java.util.logging.Level;
7274
import java.util.logging.LogManager;
@@ -83,6 +85,8 @@ public class Main {
8385
private static final String COPYBARA_NAMESPACE = "com.google.copybara";
8486

8587
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
88+
private static final String BUILD_DATA_PROPERTIES = "/build-data.properties";
89+
public static final String BUILD_LABEL = "Build label";
8690
/**
8791
* Represents the environment, typically {@code System.getEnv()}. Injected to make easier tests.
8892
*
@@ -300,15 +304,37 @@ public ImmutableSet<CopybaraCmd> getCommands(ModuleSet moduleSet,
300304
* Returns a short String representing the version of the binary
301305
*/
302306
protected String getVersion() {
303-
return "Unknown version";
307+
String buildLabel = getBuildInfo().get(BUILD_LABEL);
308+
return buildLabel == null ? "Unknown version" : buildLabel;
304309
}
305310

311+
private static ImmutableMap<String, String> getBuildInfo() {
312+
try (InputStream in = Main.class.getResourceAsStream(BUILD_DATA_PROPERTIES)) {
313+
if (in == null) {
314+
return ImmutableMap.of();
315+
}
316+
Properties props = new Properties();
317+
props.load(in);
318+
ImmutableMap.Builder<String, String> buildData = ImmutableMap.builder();
319+
for (Object key : props.keySet()) {
320+
String stringKey = key.toString();
321+
if (stringKey.startsWith("build.")) {
322+
// build.label -> Build label, build.timestamp.as.int -> Build timestamp as int
323+
String buildDataKey = "B" + stringKey.substring(1).replace('.', ' ');
324+
buildData.put(buildDataKey, props.getProperty(stringKey, ""));
325+
}
326+
}
327+
return buildData.buildOrThrow();
328+
} catch (IOException ignored) {
329+
return ImmutableMap.of();
330+
}
331+
}
306332
/**
307333
* Returns a String (can be multiline) representing all the information about who and when the
308334
* Copybara was built.
309335
*/
310336
protected String getBinaryInfo() {
311-
return "Unknown version";
337+
return Joiner.on("\n").withKeyValueSeparator(": ").join(getBuildInfo());
312338
}
313339

314340
protected Consumer<Migration> getMigrationRanConsumer() {

0 commit comments

Comments
 (0)