Update a project's last green commit when its pipeline succeed at HEAD (#388)

Change-Id: I7865d07a2bfc48a96f68fdcff8e5a0e9efe77840
diff --git a/buildkite/bazelci.py b/buildkite/bazelci.py
index d6e5136..9222d84 100644
--- a/buildkite/bazelci.py
+++ b/buildkite/bazelci.py
@@ -1072,8 +1072,25 @@
                            http_config, file_config, git_repository, monitor_flaky_tests, use_but)
         pipeline_steps.append(step)
 
-    print(yaml.dump({"steps": pipeline_steps}))
+    if not is_pull_request and not use_but and os.getenv("BUILDKITE_BRANCH") == "master":
+        pipeline_steps.append("wait")
 
+        last_green_commit = os.getenv("BUILDKITE_COMMIT")
+        if not git_repository:
+            git_repository = os.getenv("BUILDKITE_REPO")
+
+        # If all builds succeed, update the last green commit of this project
+        pipeline_steps.append({
+            "label": "Update Last Green Commmit",
+            "command": [
+                "echo %s | %s cp -I %s" % (last_green_commit, gsutil_command(), bazelci_last_green_commit_url(git_repository))
+            ],
+            "agents": {
+                "kind": "pipeline"
+            }
+        })
+
+    print(yaml.dump({"steps": pipeline_steps}))
 
 def runner_step(platform, project_name=None, http_config=None,
                 file_config=None, git_repository=None, monitor_flaky_tests=False, use_but=False):
@@ -1260,6 +1277,10 @@
     return "gs://bazel-builds/metadata/latest.json"
 
 
+def bazelci_last_green_commit_url(git_repository):
+    return "gs://bazel-builds/last_green_commit/" + git_repository[len("https://"):]
+
+
 def latest_generation_and_build_number():
     output = None
     attempt = 0