bcr_postsubmit: use gcloud storage rsync with fixed mtimes (#3009)

Migrate `bcr_postsubmit.py` from `gsutil` to `gcloud storage rsync`
again (see #2588), with a fix for the problem that caused the revert in
#2589.

## Problem

A fresh git checkout gives every file a new mtime. `gcloud storage
rsync` keeps the mtime in the object metadata. When the mtime changes,
it patches the metadata of every object, also when the content is the
same.

## Changes

- Set a fixed mtime on all files in `modules/` before rsync. Then rsync
only touches objects with changed content. `--checksums-only` finds the
changed files.
- Upload `last_green.txt` with `Cache-Control: no-cache`.
`get_last_green()` reads this file over HTTP.

Each run is still a full rsync. Thus the latest run alone makes the
bucket the same as the latest commit.

## Testing

I tested on `gs://bazel-testing` with fresh copies of the files for each
run:

| Run | Copies | Metadata patches |
|---|---|---|
| Fixed mtime, no changes | 0 | 0 |
| No fixed mtime, no changes | 0 | all objects |
| Fixed mtime, 1 edit with the same file size + 1 new version + 1
`metadata.json` change | 3 | 0 |
| Fixed mtime, object with a different stored mtime | 0 | 1 |

Note: the first run on the production bucket patches the mtime of all
objects one time, because the objects have different stored mtimes now.
Later runs do not patch.
diff --git a/buildkite/bazel-central-registry/bcr_postsubmit.py b/buildkite/bazel-central-registry/bcr_postsubmit.py
index 654a412..5b2dd44 100755
--- a/buildkite/bazel-central-registry/bcr_postsubmit.py
+++ b/buildkite/bazel-central-registry/bcr_postsubmit.py
@@ -42,6 +42,9 @@
 # that passed through the post-submit pipeline successfully.
 LAST_GREEN_FILE = "last_green.txt"
 
+# Fixed mtime (2020-01-01T00:00:00Z) for all files in modules/ before rsync.
+FIXED_MTIME = 1577836800
+
 
 class AttestationError(Exception):
     """Raised when there is a problem wrt attestations."""
@@ -167,14 +170,39 @@
     raise AttestationError(f"Invalid basename of {url}.")
 
 
+def pin_mtimes(root):
+    # A fresh git checkout gives every file a new mtime. gcloud storage rsync
+    # stores the mtime in the object metadata and patches it on every object
+    # when it changes, even if the content is the same. Use a fixed mtime so
+    # that only objects with changed content are touched.
+    for dirpath, _, filenames in os.walk(root):
+        for name in filenames:
+            os.utime(
+                os.path.join(dirpath, name),
+                (FIXED_MTIME, FIXED_MTIME),
+                follow_symlinks=False,
+            )
+
+
 def sync_bcr_content():
     print_expanded_group(":gcloud: Sync BCR content")
     subprocess.check_output(
-        ["gsutil", "-h", "Cache-Control:no-cache", "cp", "./bazel_registry.json", BCR_BUCKET]
+        ["gcloud", "storage", "cp", "--cache-control=no-cache", "./bazel_registry.json", BCR_BUCKET]
     )
+    pin_mtimes("./modules")
     subprocess.check_output(
-        # -c Use checksum to compare files
-        ["gsutil", "-h", "Cache-Control:no-cache", "-m", "rsync", "-e", "-c", "-r", "./modules", BCR_BUCKET + "modules"]
+        [
+            "gcloud",
+            "storage",
+            "rsync",
+            "--recursive",
+            # Compare checksums only. The mtime is fixed, so it cannot show changes.
+            "--checksums-only",
+            "--ignore-symlinks",
+            "--cache-control=no-cache",
+            "./modules",
+            BCR_BUCKET + "modules",
+        ]
     )
 
 
@@ -184,7 +212,8 @@
         f.write(get_commit())
 
     dest = os.path.join(BCR_BUCKET, LAST_GREEN_FILE)
-    subprocess.check_output(["gsutil", "cp", path, dest])
+    # get_last_green() reads this file over HTTP, so do not let it be cached.
+    subprocess.check_output(["gcloud", "storage", "cp", "--cache-control=no-cache", path, dest])
 
 
 def main():