Skip to content

Commit

Permalink
[6.4.0] Add formatted timestamp entries to volatile workspace status …
Browse files Browse the repository at this point in the history
…file. (#19499)

This change avoids unix timestamp parsing for Starlark rules which will
be writing BuildInfo files in the future.

PiperOrigin-RevId: 537258769
Change-Id: Ie68bac5d8e365f3251122c4b6367227e1dd0cec8

---------

Co-authored-by: Ian (Hee) Cha <heec@google.com>
  • Loading branch information
buildbreaker2021 and iancha1992 committed Sep 13, 2023
1 parent 2ebbfeb commit 2fe15f5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
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

0 comments on commit 2fe15f5

Please sign in to comment.