Skip to content
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

[6.4.0] Add formatted timestamp entries to volatile workspace status file. #19499

Merged
merged 4 commits into from
Sep 13, 2023
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
2 changes: 2 additions & 0 deletions site/en/docs/user-manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,8 @@ The contract is:
Bazel always outputs the following volatile keys:
* `BUILD_TIMESTAMP`: time of the build in seconds since the Unix Epoch (the value
of `System.currentTimeMillis()` divided by a thousand)
* `FORMATTED_DATE`: time of the build Formatted as
`yyyy MMM d HH mm ss EEE`(for example 2023 Jun 2 01 44 29 Fri) in UTC.

On Linux/macOS you can pass `--workspace_status_command=/bin/true` to
disable retrieving workspace status, because `true` does nothing, successfully (exits
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.TreeMap;
import javax.annotation.Nullable;
Expand All @@ -80,6 +83,13 @@ static class BazelWorkspaceStatusAction extends WorkspaceStatusAction {
private final String username;
private final String hostname;

private static final DateTimeFormatter TIME_FORMAT =
DateTimeFormatter.ofPattern("yyyy MMM d HH mm ss EEE");

private static String format(long timestamp) {
return Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.UTC).format(TIME_FORMAT);
}

BazelWorkspaceStatusAction(
Artifact stableStatus, Artifact volatileStatus, String username, String hostname) {
super(
Expand Down Expand Up @@ -180,8 +190,9 @@ public ActionResult execute(ActionExecutionContext actionExecutionContext)
stableMap.put(BuildInfo.BUILD_EMBED_LABEL, options.embedLabel);
stableMap.put(BuildInfo.BUILD_HOST, hostname);
stableMap.put(BuildInfo.BUILD_USER, username);
volatileMap.put(
BuildInfo.BUILD_TIMESTAMP, Long.toString(getCurrentTimeMillis(clientEnv) / 1000));
long currentTimeMillis = getCurrentTimeMillis(clientEnv);
volatileMap.put(BuildInfo.BUILD_TIMESTAMP, Long.toString(currentTimeMillis / 1000));
volatileMap.put("FORMATTED_DATE", format(currentTimeMillis / 1000 * 1000));
try {
Map<String, String> statusMap =
parseWorkspaceStatus(getAdditionalWorkspaceStatus(options, actionExecutionContext));
Expand Down
22 changes: 22 additions & 0 deletions src/test/shell/bazel/bazel_workspace_status_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,28 @@ EOF
assert_contains "STABLE_NAME bob" bazel-genfiles/ao
assert_contains "NUMBER 3" bazel-genfiles/ao

# Test that generated timestamp is the same as its formatted version.
volatile_file="bazel-out/volatile-status.txt"
bazel build --stamp //:a || fail "build failed"
assert_contains "BUILD_TIMESTAMP" $volatile_file
assert_contains "FORMATTED_DATE" $volatile_file
# Read key value pairs.
timestamp_key_value=$(sed "1q;d" $volatile_file)
formatted_timestamp_key_value=$(sed "2q;d" $volatile_file)
# Extract values of the formatted date and timestamp.
timestamp=${timestamp_key_value#* }
formatted_date=${formatted_timestamp_key_value#* }
if [[ $(uname -s) == "Darwin" ]]
then
timestamp_formatted_date=$(date -u -r "$timestamp" +'%Y %b %d %H %M %S %a')
else
timestamp_formatted_date=$(date -u -d "@$timestamp" +'%Y %b %d %H %M %S %a')
fi

if [[ $timestamp_formatted_date != $formatted_date ]]
then
fail "Timestamp formatted date: $timestamp_formatted_date differs from workspace module provided formatted date: $formatted_date"
fi
}

function test_env_var_in_workspace_status() {
Expand Down