Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 3 | # Copyright 2018 The Bazel Authors. All rights reserved. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 17 | import argparse |
Jakob Buchgraber | d20ffeb | 2018-02-18 03:16:43 +0100 | [diff] [blame] | 18 | import base64 |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 19 | import codecs |
Jakob Buchgraber | d20ffeb | 2018-02-18 03:16:43 +0100 | [diff] [blame] | 20 | import hashlib |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 21 | import json |
Jakob Buchgraber | 6db0f26 | 2018-02-17 15:45:54 +0100 | [diff] [blame] | 22 | import multiprocessing |
Philipp Wollermann | 0a04cf3 | 2018-02-21 17:07:22 +0100 | [diff] [blame] | 23 | import os |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 24 | import os.path |
Jakob Buchgraber | 257693b | 2018-02-20 00:03:56 +0100 | [diff] [blame] | 25 | import random |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 26 | import re |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 27 | from shutil import copyfile |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 28 | import shutil |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 29 | import stat |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 30 | import subprocess |
| 31 | import sys |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 32 | import tempfile |
| 33 | import urllib.request |
Jakob Buchgraber | 540d47b | 2018-02-21 19:11:23 +0100 | [diff] [blame] | 34 | from github3 import login |
Philipp Wollermann | c030f2e | 2018-02-21 17:02:19 +0100 | [diff] [blame] | 35 | from urllib.request import url2pathname |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 36 | from urllib.parse import urlparse |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 37 | |
| 38 | # Initialize the random number generator. |
| 39 | random.seed() |
| 40 | |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 41 | |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 42 | class BuildkiteException(Exception): |
| 43 | """ |
| 44 | Raised whenever something goes wrong and we should exit with an error. |
| 45 | """ |
| 46 | pass |
| 47 | |
| 48 | |
| 49 | class BinaryUploadRaceException(Exception): |
| 50 | """ |
| 51 | Raised when try_publish_binaries wasn't able to publish a set of binaries, |
| 52 | because the generation of the current file didn't match the expected value. |
| 53 | """ |
| 54 | pass |
| 55 | |
| 56 | |
| 57 | class BazelTestFailedException(Exception): |
| 58 | """ |
| 59 | Raised when a Bazel test fails. |
| 60 | """ |
| 61 | pass |
| 62 | |
| 63 | |
Jakob Buchgraber | c8b6bbe | 2018-02-22 09:54:47 +0100 | [diff] [blame] | 64 | class BazelBuildFailedException(Exception): |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 65 | """ |
| 66 | Raised when a Bazel build fails. |
| 67 | """ |
| 68 | pass |
| 69 | |
Jakob Buchgraber | d26a7d9 | 2018-02-21 18:53:54 +0100 | [diff] [blame] | 70 | |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 71 | def eprint(*args, **kwargs): |
| 72 | """ |
| 73 | Print to stderr and flush (just in case). |
| 74 | """ |
| 75 | print(*args, flush=True, file=sys.stderr, **kwargs) |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 76 | |
| 77 | |
| 78 | def downstream_projects(): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 79 | return { |
Jakob Buchgraber | f462ff9 | 2018-02-20 17:34:09 +0100 | [diff] [blame] | 80 | "Bazel Remote Execution": { |
| 81 | "git_repository": "https://github.com/bazelbuild/bazel.git", |
| 82 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/bazel-remote-execution-postsubmit.json" |
| 83 | }, |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 84 | "BUILD_file_generator": { |
| 85 | "git_repository": "https://github.com/bazelbuild/BUILD_file_generator.git", |
| 86 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/BUILD_file_generator-postsubmit.json" |
| 87 | }, |
| 88 | "bazel-toolchains": { |
| 89 | "git_repository": "https://github.com/bazelbuild/bazel-toolchains.git", |
| 90 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/bazel-toolchains-postsubmit.json" |
| 91 | }, |
| 92 | "buildtools": { |
| 93 | "git_repository": "https://github.com/bazelbuild/buildtools.git", |
| 94 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/buildtools-postsubmit.json" |
| 95 | }, |
| 96 | "CLion Plugin": { |
| 97 | "git_repository": "https://github.com/bazelbuild/intellij.git", |
| 98 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/clion-postsubmit.json" |
| 99 | }, |
| 100 | "Eclipse Plugin": { |
| 101 | "git_repository": "https://github.com/bazelbuild/eclipse.git", |
| 102 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/eclipse-postsubmit.json" |
| 103 | }, |
| 104 | "Gerrit": { |
| 105 | "git_repository": "https://gerrit.googlesource.com/gerrit.git", |
| 106 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/gerrit-postsubmit.json" |
| 107 | }, |
| 108 | "Google Logging": { |
| 109 | "git_repository": "https://github.com/google/glog.git", |
| 110 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/glog-postsubmit.json" |
| 111 | }, |
| 112 | "IntelliJ Plugin": { |
| 113 | "git_repository": "https://github.com/bazelbuild/intellij.git", |
| 114 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/intellij-postsubmit.json" |
| 115 | }, |
| 116 | "migration-tooling": { |
| 117 | "git_repository": "https://github.com/bazelbuild/migration-tooling.git", |
| 118 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/migration-tooling-postsubmit.json" |
| 119 | }, |
| 120 | "protobuf": { |
| 121 | "git_repository": "https://github.com/google/protobuf.git", |
| 122 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/protobuf-postsubmit.json" |
| 123 | }, |
| 124 | "re2": { |
| 125 | "git_repository": "https://github.com/google/re2.git", |
| 126 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/re2-postsubmit.json" |
| 127 | }, |
| 128 | "rules_appengine": { |
| 129 | "git_repository": "https://github.com/bazelbuild/rules_appengine.git", |
| 130 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/rules_appengine-postsubmit.json" |
| 131 | }, |
| 132 | "rules_closure": { |
| 133 | "git_repository": "https://github.com/bazelbuild/rules_closure.git", |
| 134 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/rules_closure-postsubmit.json" |
| 135 | }, |
| 136 | "rules_d": { |
| 137 | "git_repository": "https://github.com/bazelbuild/rules_d.git", |
| 138 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/rules_d-postsubmit.json" |
| 139 | }, |
| 140 | "rules_go": { |
| 141 | "git_repository": "https://github.com/bazelbuild/rules_go.git", |
| 142 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/rules_go-postsubmit.json" |
| 143 | }, |
| 144 | "rules_groovy": { |
| 145 | "git_repository": "https://github.com/bazelbuild/rules_groovy.git", |
| 146 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/rules_groovy-postsubmit.json" |
| 147 | }, |
| 148 | "rules_gwt": { |
| 149 | "git_repository": "https://github.com/bazelbuild/rules_gwt.git", |
| 150 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/rules_gwt-postsubmit.json" |
| 151 | }, |
| 152 | "rules_jsonnet": { |
| 153 | "git_repository": "https://github.com/bazelbuild/rules_jsonnet.git", |
| 154 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/rules_jsonnet-postsubmit.json" |
| 155 | }, |
| 156 | "rules_k8s": { |
| 157 | "git_repository": "https://github.com/bazelbuild/rules_k8s.git", |
| 158 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/rules_k8s-postsubmit.json" |
| 159 | }, |
| 160 | "rules_nodejs": { |
| 161 | "git_repository": "https://github.com/bazelbuild/rules_nodejs.git", |
| 162 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/rules_nodejs-postsubmit.json" |
| 163 | }, |
| 164 | "rules_perl": { |
| 165 | "git_repository": "https://github.com/bazelbuild/rules_perl.git", |
| 166 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/rules_perl-postsubmit.json" |
| 167 | }, |
| 168 | "rules_python": { |
| 169 | "git_repository": "https://github.com/bazelbuild/rules_python.git", |
| 170 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/rules_python-postsubmit.json" |
| 171 | }, |
| 172 | "rules_rust": { |
| 173 | "git_repository": "https://github.com/bazelbuild/rules_rust.git", |
| 174 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/rules_rust-postsubmit.json" |
| 175 | }, |
| 176 | "rules_sass": { |
| 177 | "git_repository": "https://github.com/bazelbuild/rules_sass.git", |
| 178 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/rules_sass-postsubmit.json" |
| 179 | }, |
| 180 | "rules_scala": { |
| 181 | "git_repository": "https://github.com/bazelbuild/rules_scala.git", |
| 182 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/rules_scala-postsubmit.json" |
| 183 | }, |
| 184 | "rules_typescript": { |
| 185 | "git_repository": "https://github.com/bazelbuild/rules_typescript.git", |
| 186 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/rules_typescript-postsubmit.json" |
| 187 | }, |
| 188 | # Enable once is resolved: https://github.com/bazelbuild/continuous-integration/issues/191 |
| 189 | # "rules_webtesting": { |
| 190 | # "git_repository": "https://github.com/bazelbuild/rules_webtesting.git", |
| 191 | # "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/rules_webtesting-postsubmit.json" |
| 192 | # }, |
| 193 | "skydoc": { |
| 194 | "git_repository": "https://github.com/bazelbuild/skydoc.git", |
| 195 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/skydoc-postsubmit.json" |
| 196 | }, |
| 197 | "subpar": { |
| 198 | "git_repository": "https://github.com/google/subpar.git", |
| 199 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/subpar-postsubmit.json" |
| 200 | }, |
| 201 | "TensorFlow": { |
| 202 | "git_repository": "https://github.com/tensorflow/tensorflow.git", |
| 203 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/tensorflow-postsubmit.json" |
| 204 | }, |
| 205 | "TensorFlow Serving": { |
| 206 | "git_repository": "https://github.com/tensorflow/serving.git", |
| 207 | "http_config": "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/pipelines/tensorflow-serving-postsubmit.json" |
| 208 | } |
| 209 | } |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 210 | |
| 211 | |
Philipp Wollermann | bcedf9b | 2018-02-19 18:07:44 +0100 | [diff] [blame] | 212 | def python_binary(platform=None): |
| 213 | if platform == "windows": |
| 214 | return "python.exe" |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 215 | return "python3.6" |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 216 | |
| 217 | |
| 218 | def bazelcipy_url(): |
Philipp Wollermann | db02486 | 2018-02-19 17:16:56 +0100 | [diff] [blame] | 219 | """ |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 220 | URL to the latest version of this script. |
Philipp Wollermann | db02486 | 2018-02-19 17:16:56 +0100 | [diff] [blame] | 221 | """ |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 222 | return "https://raw.githubusercontent.com/bazelbuild/continuous-integration/master/buildkite/bazelci.py" |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 223 | |
| 224 | |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 225 | def platforms_info(): |
Philipp Wollermann | db02486 | 2018-02-19 17:16:56 +0100 | [diff] [blame] | 226 | """ |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 227 | Returns a map containing all supported platform names as keys, with the |
| 228 | values being the platform name in a human readable format, and a the |
| 229 | buildkite-agent's working directory. |
Philipp Wollermann | db02486 | 2018-02-19 17:16:56 +0100 | [diff] [blame] | 230 | """ |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 231 | return { |
| 232 | "ubuntu1404": { |
| 233 | "name": "Ubuntu 14.04", |
Jakob Buchgraber | 7d1d3bb | 2018-02-21 22:38:22 +0100 | [diff] [blame] | 234 | "emoji-name": ":ubuntu: 14.04", |
Philipp Wollermann | 1c03636 | 2018-02-19 17:16:31 +0100 | [diff] [blame] | 235 | "agent-directory": "/var/lib/buildkite-agent/builds/${BUILDKITE_AGENT_NAME}" |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 236 | }, |
| 237 | "ubuntu1604": { |
| 238 | "name": "Ubuntu 16.04", |
Jakob Buchgraber | 7d1d3bb | 2018-02-21 22:38:22 +0100 | [diff] [blame] | 239 | "emoji-name": ":ubuntu: 16.04", |
Philipp Wollermann | 1c03636 | 2018-02-19 17:16:31 +0100 | [diff] [blame] | 240 | "agent-directory": "/var/lib/buildkite-agent/builds/${BUILDKITE_AGENT_NAME}" |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 241 | }, |
| 242 | "macos": { |
| 243 | "name": "macOS", |
Jakob Buchgraber | 7d1d3bb | 2018-02-21 22:38:22 +0100 | [diff] [blame] | 244 | "emoji-name": ":darwin:", |
Philipp Wollermann | 1c03636 | 2018-02-19 17:16:31 +0100 | [diff] [blame] | 245 | "agent-directory": "/usr/local/var/buildkite-agent/builds/${BUILDKITE_AGENT_NAME}" |
| 246 | }, |
| 247 | "windows": { |
Jakob Buchgraber | 3a2c77d | 2018-02-21 22:39:53 +0100 | [diff] [blame] | 248 | "name": "Windows", |
Jakob Buchgraber | 4973b6f | 2018-02-21 22:40:57 +0100 | [diff] [blame] | 249 | "emoji-name": ":windows:", |
Philipp Wollermann | 1c03636 | 2018-02-19 17:16:31 +0100 | [diff] [blame] | 250 | "agent-directory": "d:/build/${BUILDKITE_AGENT_NAME}", |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 251 | } |
| 252 | } |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 253 | |
Jakob Buchgraber | 6db0f26 | 2018-02-17 15:45:54 +0100 | [diff] [blame] | 254 | |
Jakob Buchgraber | 257693b | 2018-02-20 00:03:56 +0100 | [diff] [blame] | 255 | def flaky_test_meme_url(): |
Jakob Buchgraber | f910746 | 2018-02-20 00:18:39 +0100 | [diff] [blame] | 256 | urls = ["https://storage.googleapis.com/bazel-buildkite-memes/flaky_tests_1.jpg", |
Jakob Buchgraber | ad6692e | 2018-02-20 11:12:00 +0100 | [diff] [blame] | 257 | "https://storage.googleapis.com/bazel-buildkite-memes/flaky_tests_2.jpg"] |
Jakob Buchgraber | 257693b | 2018-02-20 00:03:56 +0100 | [diff] [blame] | 258 | return random.choice(urls) |
| 259 | |
| 260 | |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 261 | def downstream_projects_root(platform): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 262 | downstream_projects_dir = os.path.expandvars( |
| 263 | "${BUILDKITE_ORGANIZATION_SLUG}-downstream-projects") |
| 264 | path = os.path.join(agent_directory(platform), downstream_projects_dir) |
| 265 | if not os.path.exists(path): |
| 266 | os.makedirs(path) |
| 267 | return path |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 268 | |
| 269 | |
| 270 | def agent_directory(platform): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 271 | return os.path.expandvars(platforms_info()[platform]["agent-directory"]) |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 272 | |
| 273 | |
| 274 | def supported_platforms(): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 275 | return set(platforms_info().keys()) |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 276 | |
| 277 | |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 278 | def fetch_configs(http_url): |
Philipp Wollermann | db02486 | 2018-02-19 17:16:56 +0100 | [diff] [blame] | 279 | """ |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 280 | If specified fetches the build configuration from http_url, else tries to |
| 281 | read it from .bazelci/config.json. |
| 282 | Returns the json configuration as a python data structure. |
Philipp Wollermann | db02486 | 2018-02-19 17:16:56 +0100 | [diff] [blame] | 283 | """ |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 284 | if http_url is None: |
| 285 | with open(".bazelci/config.json", "r") as fd: |
| 286 | return json.load(fd) |
| 287 | with urllib.request.urlopen(http_url) as resp: |
| 288 | reader = codecs.getreader("utf-8") |
| 289 | return json.load(reader(resp)) |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 290 | |
Jakob Buchgraber | 9c83de7 | 2018-02-18 15:32:44 +0100 | [diff] [blame] | 291 | |
Jakob Buchgraber | 3120f7a | 2018-02-18 13:28:02 +0100 | [diff] [blame] | 292 | def print_collapsed_group(name): |
Jakob Buchgraber | 5c9b13d | 2018-02-21 22:28:14 +0100 | [diff] [blame] | 293 | eprint("\n\n--- {0}\n\n".format(name)) |
Jakob Buchgraber | 3120f7a | 2018-02-18 13:28:02 +0100 | [diff] [blame] | 294 | |
Jakob Buchgraber | 9c83de7 | 2018-02-18 15:32:44 +0100 | [diff] [blame] | 295 | |
Jakob Buchgraber | 3120f7a | 2018-02-18 13:28:02 +0100 | [diff] [blame] | 296 | def print_expanded_group(name): |
Jakob Buchgraber | 5c9b13d | 2018-02-21 22:28:14 +0100 | [diff] [blame] | 297 | eprint("\n\n+++ {0}\n\n".format(name)) |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 298 | |
Jakob Buchgraber | 9c83de7 | 2018-02-18 15:32:44 +0100 | [diff] [blame] | 299 | |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 300 | def execute_commands(config, platform, git_repository, use_but, save_but, |
| 301 | build_only, test_only): |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 302 | fail_pipeline = False |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 303 | tmpdir = None |
| 304 | bazel_binary = "bazel" |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 305 | commit = os.getenv("BUILDKITE_COMMIT") |
Jakob Buchgraber | fb95a2f | 2018-02-22 11:46:25 +0100 | [diff] [blame^] | 306 | build_only = build_only or "test_targets" not in config |
| 307 | test_only = test_only or "build_targets" not in config |
| 308 | if build_only and test_only: |
| 309 | raise BuildkiteException("build_only and test_only cannot be true at the same time") |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 310 | try: |
| 311 | if git_repository: |
| 312 | clone_git_repository(git_repository, platform) |
Jakob Buchgraber | 2ab68f8 | 2018-02-21 19:43:08 +0100 | [diff] [blame] | 313 | else: |
| 314 | git_repository = os.getenv("BUILDKITE_REPO") |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 315 | if is_pull_request(): |
Jakob Buchgraber | 0109fa7 | 2018-02-21 22:13:10 +0100 | [diff] [blame] | 316 | update_pull_request_verification_status(git_repository, commit, state="success") |
Philipp Wollermann | ff39ef5 | 2018-02-21 14:18:52 +0100 | [diff] [blame] | 317 | cleanup() |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 318 | tmpdir = tempfile.mkdtemp() |
| 319 | if use_but: |
| 320 | print_collapsed_group("Downloading Bazel under test") |
| 321 | bazel_binary = download_bazel_binary(tmpdir, platform) |
| 322 | print_bazel_version_info(bazel_binary) |
| 323 | execute_shell_commands(config.get("shell_commands", None)) |
| 324 | execute_bazel_run(bazel_binary, config.get("run_targets", None)) |
| 325 | if not test_only: |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 326 | build_bep_file = os.path.join(tmpdir, "build_bep.json") |
| 327 | if is_pull_request(): |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 328 | update_pull_request_build_status( |
| 329 | platform, git_repository, commit, "pending", None) |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 330 | try: |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 331 | execute_bazel_build(bazel_binary, platform, config.get("build_flags", []), |
| 332 | config.get("build_targets", None), build_bep_file) |
| 333 | if is_pull_request(): |
| 334 | invocation_id = bes_invocation_id(build_bep_file) |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 335 | update_pull_request_build_status( |
| 336 | platform, git_repository, commit, "success", invocation_id) |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 337 | if save_but: |
| 338 | upload_bazel_binary() |
| 339 | except BazelBuildFailedException: |
Jakob Buchgraber | d26a7d9 | 2018-02-21 18:53:54 +0100 | [diff] [blame] | 340 | if is_pull_request(): |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 341 | invocation_id = bes_invocation_id(build_bep_file) |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 342 | update_pull_request_build_status( |
| 343 | platform, git_repository, commit, "failure", invocation_id) |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 344 | fail_pipeline = True |
Jakob Buchgraber | 9f40373 | 2018-02-22 10:18:29 +0100 | [diff] [blame] | 345 | if (not fail_pipeline) and (not build_only): |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 346 | test_bep_file = os.path.join(tmpdir, "test_bep.json") |
| 347 | try: |
| 348 | if is_pull_request(): |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 349 | update_pull_request_test_status( |
| 350 | platform, git_repository, commit, "pending", None) |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 351 | execute_bazel_test(bazel_binary, platform, config.get("test_flags", []), |
| 352 | config.get("test_targets", None), test_bep_file) |
Jakob Buchgraber | 5d6c714 | 2018-02-21 20:16:51 +0100 | [diff] [blame] | 353 | if has_flaky_tests(test_bep_file): |
| 354 | show_image(flaky_test_meme_url(), "Flaky Tests") |
| 355 | # We also treat flaky tests as test failures |
| 356 | raise BazelTestFailedException |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 357 | if is_pull_request(): |
| 358 | invocation_id = bes_invocation_id(test_bep_file) |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 359 | update_pull_request_test_status( |
| 360 | platform, git_repository, commit, "success", invocation_id) |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 361 | except BazelTestFailedException: |
| 362 | if is_pull_request(): |
| 363 | invocation_id = bes_invocation_id(test_bep_file) |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 364 | failed_tests = tests_with_status( |
| 365 | test_bep_file, status="FAILED") |
| 366 | timed_out_tests = tests_with_status( |
| 367 | test_bep_file, status="TIMEOUT") |
| 368 | flaky_tests = tests_with_status( |
| 369 | test_bep_file, status="FLAKY") |
Jakob Buchgraber | de682f5 | 2018-02-21 21:53:53 +0100 | [diff] [blame] | 370 | |
| 371 | update_pull_request_test_status(platform, git_repository, commit, "failure", invocation_id, |
Jakob Buchgraber | 5d6c714 | 2018-02-21 20:16:51 +0100 | [diff] [blame] | 372 | len(failed_tests), len(timed_out_tests), len(flaky_tests)) |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 373 | fail_pipeline = True |
| 374 | |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 375 | upload_test_logs(test_bep_file, tmpdir) |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 376 | finally: |
| 377 | if tmpdir: |
| 378 | shutil.rmtree(tmpdir) |
Philipp Wollermann | ff39ef5 | 2018-02-21 14:18:52 +0100 | [diff] [blame] | 379 | cleanup() |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 380 | |
| 381 | if fail_pipeline: |
| 382 | raise BuildkiteException("At least one test failed or was flaky.") |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 383 | |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 384 | |
Jakob Buchgraber | 5d6c714 | 2018-02-21 20:16:51 +0100 | [diff] [blame] | 385 | def tests_with_status(bep_file, status): |
| 386 | return set(label for label, _ in test_logs_for_status(bep_file, status=status)) |
Jakob Buchgraber | 257693b | 2018-02-20 00:03:56 +0100 | [diff] [blame] | 387 | |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 388 | |
Jakob Buchgraber | 27670f8 | 2018-02-21 22:09:56 +0100 | [diff] [blame] | 389 | __github_token__ = None |
Jakob Buchgraber | 8498ea6 | 2018-02-21 22:02:35 +0100 | [diff] [blame] | 390 | |
| 391 | |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 392 | def fetch_github_token(): |
Jakob Buchgraber | 27670f8 | 2018-02-21 22:09:56 +0100 | [diff] [blame] | 393 | global __github_token__ |
| 394 | if __github_token__: |
| 395 | return __github_token__ |
Jakob Buchgraber | aa2af38 | 2018-02-21 19:56:54 +0100 | [diff] [blame] | 396 | try: |
| 397 | execute_command( |
| 398 | ["gsutil", "cp", "gs://bazel-encrypted-secrets/github-token.enc", "github-token.enc"]) |
Jakob Buchgraber | 27670f8 | 2018-02-21 22:09:56 +0100 | [diff] [blame] | 399 | __github_token__ = subprocess.check_output(["gcloud", "kms", "decrypt", "--location", "global", "--keyring", "buildkite", |
Jakob Buchgraber | 0109fa7 | 2018-02-21 22:13:10 +0100 | [diff] [blame] | 400 | "--key", "github-token", "--ciphertext-file", "github-token.enc", |
| 401 | "--plaintext-file", "-"]).decode("utf-8").strip() |
Jakob Buchgraber | 27670f8 | 2018-02-21 22:09:56 +0100 | [diff] [blame] | 402 | return __github_token__ |
Jakob Buchgraber | aa2af38 | 2018-02-21 19:56:54 +0100 | [diff] [blame] | 403 | finally: |
Jakob Buchgraber | c1055ad | 2018-02-21 20:04:23 +0100 | [diff] [blame] | 404 | os.remove("github-token.enc") |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 405 | |
| 406 | |
| 407 | def owner_repository_from_url(git_repository): |
Jakob Buchgraber | 18a46c7 | 2018-02-21 19:18:51 +0100 | [diff] [blame] | 408 | m = re.search(r"/([^/]+)/([^/]+)\.git$", git_repository) |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 409 | owner = m.group(1) |
| 410 | repository = m.group(2) |
| 411 | return (owner, repository) |
| 412 | |
| 413 | |
Jakob Buchgraber | 564bcae | 2018-02-21 22:19:10 +0100 | [diff] [blame] | 414 | def results_view_url(invocation_id): |
Jakob Buchgraber | aa2af38 | 2018-02-21 19:56:54 +0100 | [diff] [blame] | 415 | results_url = None |
| 416 | if invocation_id: |
| 417 | results_url = "https://source.cloud.google.com/results/invocations/" + invocation_id |
Jakob Buchgraber | 84e523c | 2018-02-21 22:25:00 +0100 | [diff] [blame] | 418 | return results_url |
Jakob Buchgraber | 564bcae | 2018-02-21 22:19:10 +0100 | [diff] [blame] | 419 | |
| 420 | |
| 421 | def update_pull_request_status(git_repository, commit, state, details_url, description, context): |
| 422 | gh = login(token=fetch_github_token()) |
| 423 | owner, repo = owner_repository_from_url(git_repository) |
| 424 | repo = gh.repository(owner=owner, repository=repo) |
| 425 | repo.create_status(sha=commit, state=state, target_url=details_url, description=description, |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 426 | context=context) |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 427 | |
| 428 | |
Jakob Buchgraber | 0109fa7 | 2018-02-21 22:13:10 +0100 | [diff] [blame] | 429 | def update_pull_request_verification_status(git_repository, commit, state): |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 430 | description = "" |
| 431 | if state == "pending": |
Jakob Buchgraber | 0109fa7 | 2018-02-21 22:13:10 +0100 | [diff] [blame] | 432 | description = "A project member must verify this pull request." |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 433 | elif state == "success": |
Jakob Buchgraber | de682f5 | 2018-02-21 21:53:53 +0100 | [diff] [blame] | 434 | description = "Verified" |
Jakob Buchgraber | 564bcae | 2018-02-21 22:19:10 +0100 | [diff] [blame] | 435 | build_url = os.getenv("BUILDKITE_BUILD_URL") |
| 436 | update_pull_request_status(git_repository, commit, state, build_url, description, |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 437 | "Untrusted Code Verification") |
| 438 | |
| 439 | |
| 440 | def update_pull_request_build_status(platform, git_repository, commit, state, invocation_id): |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 441 | description = "" |
| 442 | if state == "pending": |
Jakob Buchgraber | de682f5 | 2018-02-21 21:53:53 +0100 | [diff] [blame] | 443 | description = "Building ..." |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 444 | elif state == "failure": |
Jakob Buchgraber | de682f5 | 2018-02-21 21:53:53 +0100 | [diff] [blame] | 445 | description = "Failure" |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 446 | elif state == "success": |
Jakob Buchgraber | de682f5 | 2018-02-21 21:53:53 +0100 | [diff] [blame] | 447 | description = "Success" |
Jakob Buchgraber | 564bcae | 2018-02-21 22:19:10 +0100 | [diff] [blame] | 448 | update_pull_request_status(git_repository, commit, state, results_view_url(invocation_id), description, |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 449 | "bazel build ({0})".format(platforms_info()[platform]["name"])) |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 450 | |
| 451 | |
Jakob Buchgraber | de682f5 | 2018-02-21 21:53:53 +0100 | [diff] [blame] | 452 | def update_pull_request_test_status(platform, git_repository, commit, state, invocation_id, num_failed=0, |
| 453 | num_timed_out=0, num_flaky=0): |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 454 | description = "" |
Jakob Buchgraber | aa2af38 | 2018-02-21 19:56:54 +0100 | [diff] [blame] | 455 | if state == "pending": |
Jakob Buchgraber | de682f5 | 2018-02-21 21:53:53 +0100 | [diff] [blame] | 456 | description = "Testing ..." |
Jakob Buchgraber | aa2af38 | 2018-02-21 19:56:54 +0100 | [diff] [blame] | 457 | elif state == "failure": |
Jakob Buchgraber | 7e9a4d2 | 2018-02-21 22:51:44 +0100 | [diff] [blame] | 458 | if num_failed > 0: |
Jakob Buchgraber | de682f5 | 2018-02-21 21:53:53 +0100 | [diff] [blame] | 459 | description = description + "{0} tests failed, ".format(num_failed) |
| 460 | if num_timed_out > 0: |
| 461 | description = description + "{0} tests timed out, ".format(num_timed_out) |
| 462 | if num_flaky > 0: |
| 463 | description = description + "{0} tests are flaky, ".format(num_flaky) |
| 464 | if len(description) > 0: |
| 465 | description = description[:-2] |
| 466 | else: |
| 467 | description = "Some tests didn't pass" |
Jakob Buchgraber | c1055ad | 2018-02-21 20:04:23 +0100 | [diff] [blame] | 468 | elif state == "success": |
Jakob Buchgraber | de682f5 | 2018-02-21 21:53:53 +0100 | [diff] [blame] | 469 | description = "All tests passed" |
Jakob Buchgraber | 564bcae | 2018-02-21 22:19:10 +0100 | [diff] [blame] | 470 | update_pull_request_status(git_repository, commit, state, results_view_url(invocation_id), description, |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 471 | "bazel test ({0})".format(platforms_info()[platform]["name"])) |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 472 | |
| 473 | |
| 474 | def is_pull_request(): |
Jakob Buchgraber | 67761d3 | 2018-02-21 19:00:21 +0100 | [diff] [blame] | 475 | third_party_repo = os.getenv("BUILDKITE_PULL_REQUEST_REPO", "") |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 476 | return len(third_party_repo) > 0 |
| 477 | |
| 478 | |
| 479 | def bes_invocation_id(bep_file): |
| 480 | targets = [] |
| 481 | raw_data = "" |
| 482 | with open(bep_file) as f: |
| 483 | raw_data = f.read() |
| 484 | decoder = json.JSONDecoder() |
| 485 | |
| 486 | pos = 0 |
| 487 | while pos < len(raw_data): |
| 488 | bep_obj, size = decoder.raw_decode(raw_data[pos:]) |
| 489 | if "started" in bep_obj: |
| 490 | return bep_obj["started"]["uuid"] |
| 491 | pos += size + 1 |
| 492 | return None |
| 493 | |
| 494 | |
Jakob Buchgraber | 257693b | 2018-02-20 00:03:56 +0100 | [diff] [blame] | 495 | def show_image(url, alt): |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 496 | eprint("\033]1338;url='\"{0}\"';alt='\"{1}\"'\a\n".format(url, alt)) |
Jakob Buchgraber | 257693b | 2018-02-20 00:03:56 +0100 | [diff] [blame] | 497 | |
| 498 | |
Jakob Buchgraber | 02e0722 | 2018-02-19 15:05:56 +0100 | [diff] [blame] | 499 | def has_flaky_tests(bep_file): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 500 | return len(test_logs_for_status(bep_file, status="FLAKY")) > 0 |
Jakob Buchgraber | 02e0722 | 2018-02-19 15:05:56 +0100 | [diff] [blame] | 501 | |
| 502 | |
Jakob Buchgraber | 7e690a7 | 2018-02-18 13:22:15 +0100 | [diff] [blame] | 503 | def print_bazel_version_info(bazel_binary): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 504 | print_collapsed_group("Bazel Info") |
Philipp Wollermann | 3e1a771 | 2018-02-19 17:34:24 +0100 | [diff] [blame] | 505 | execute_command([bazel_binary, "version"]) |
| 506 | execute_command([bazel_binary, "info"]) |
Jakob Buchgraber | 7e690a7 | 2018-02-18 13:22:15 +0100 | [diff] [blame] | 507 | |
| 508 | |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 509 | def upload_bazel_binary(): |
Jakob Buchgraber | 7d1d3bb | 2018-02-21 22:38:22 +0100 | [diff] [blame] | 510 | print_collapsed_group(":gcloud: Uploading Bazel Under Test") |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 511 | execute_command(["buildkite-agent", "artifact", |
| 512 | "upload", "bazel-bin/src/bazel"]) |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 513 | |
| 514 | |
Jakob Buchgraber | d20ffeb | 2018-02-18 03:16:43 +0100 | [diff] [blame] | 515 | def download_bazel_binary(dest_dir, platform): |
Philipp Wollermann | f6be466 | 2018-02-21 14:48:28 +0100 | [diff] [blame] | 516 | source_step = create_label(platform, "Bazel", build_only=True) |
Philipp Wollermann | 3e1a771 | 2018-02-19 17:34:24 +0100 | [diff] [blame] | 517 | execute_command(["buildkite-agent", "artifact", "download", |
| 518 | "bazel-bin/src/bazel", dest_dir, "--step", source_step]) |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 519 | bazel_binary_path = os.path.join(dest_dir, "bazel-bin/src/bazel") |
| 520 | st = os.stat(bazel_binary_path) |
| 521 | os.chmod(bazel_binary_path, st.st_mode | stat.S_IEXEC) |
| 522 | return bazel_binary_path |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 523 | |
| 524 | |
| 525 | def clone_git_repository(git_repository, platform): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 526 | root = downstream_projects_root(platform) |
Philipp Wollermann | ff39ef5 | 2018-02-21 14:18:52 +0100 | [diff] [blame] | 527 | project_name = re.search(r"/([^/]+)\.git$", git_repository).group(1) |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 528 | clone_path = os.path.join(root, project_name) |
| 529 | print_collapsed_group("Fetching " + project_name + " sources") |
| 530 | if os.path.exists(clone_path): |
| 531 | os.chdir(clone_path) |
Philipp Wollermann | 3e1a771 | 2018-02-19 17:34:24 +0100 | [diff] [blame] | 532 | execute_command(["git", "remote", "set-url", "origin", git_repository]) |
| 533 | execute_command(["git", "clean", "-fdqx"]) |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 534 | execute_command(["git", "submodule", "foreach", |
| 535 | "--recursive", "git", "clean", "-fdqx"]) |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 536 | # sync to the latest commit of HEAD. Unlikely git pull this also works after |
| 537 | # a force push. |
Philipp Wollermann | 3e1a771 | 2018-02-19 17:34:24 +0100 | [diff] [blame] | 538 | execute_command(["git", "fetch", "origin"]) |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 539 | remote_head = subprocess.check_output( |
| 540 | ["git", "symbolic-ref", "refs/remotes/origin/HEAD"]) |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 541 | remote_head = remote_head.decode("utf-8") |
| 542 | remote_head = remote_head.rstrip() |
Philipp Wollermann | 3e1a771 | 2018-02-19 17:34:24 +0100 | [diff] [blame] | 543 | execute_command(["git", "reset", remote_head, "--hard"]) |
| 544 | execute_command(["git", "submodule", "sync", "--recursive"]) |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 545 | execute_command(["git", "submodule", "update", |
| 546 | "--init", "--recursive", "--force"]) |
| 547 | execute_command(["git", "submodule", "foreach", |
| 548 | "--recursive", "git", "reset", "--hard"]) |
Philipp Wollermann | 3e1a771 | 2018-02-19 17:34:24 +0100 | [diff] [blame] | 549 | execute_command(["git", "clean", "-fdqx"]) |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 550 | execute_command(["git", "submodule", "foreach", |
| 551 | "--recursive", "git", "clean", "-fdqx"]) |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 552 | else: |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 553 | execute_command( |
| 554 | ["git", "clone", "--recurse-submodules", git_repository, clone_path]) |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 555 | os.chdir(clone_path) |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 556 | |
| 557 | |
Philipp Wollermann | ff39ef5 | 2018-02-21 14:18:52 +0100 | [diff] [blame] | 558 | def cleanup(): |
Jakob Buchgraber | c03052a | 2018-02-22 10:59:58 +0100 | [diff] [blame] | 559 | print_collapsed_group(":wastebasket: Cleanup") |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 560 | if os.path.exists("WORKSPACE"): |
Jakob Buchgraber | 9d578fd | 2018-02-22 11:32:15 +0100 | [diff] [blame] | 561 | execute_command(["bazel", "clean", "--expunge"]) |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 562 | |
Jakob Buchgraber | fd1eaca | 2018-02-17 17:27:14 +0100 | [diff] [blame] | 563 | |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 564 | def execute_shell_commands(commands): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 565 | if not commands: |
| 566 | return |
Jakob Buchgraber | 94d5c21 | 2018-02-22 09:57:08 +0100 | [diff] [blame] | 567 | print_collapsed_group(":bash: Setup (Shell Commands)") |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 568 | shell_command = "\n".join(commands) |
Philipp Wollermann | 3e1a771 | 2018-02-19 17:34:24 +0100 | [diff] [blame] | 569 | execute_command([shell_command], shell=True) |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 570 | |
| 571 | |
| 572 | def execute_bazel_run(bazel_binary, targets): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 573 | if not targets: |
| 574 | return |
| 575 | print_collapsed_group("Setup (Run Targets)") |
| 576 | for target in targets: |
Philipp Wollermann | 3e1a771 | 2018-02-19 17:34:24 +0100 | [diff] [blame] | 577 | execute_command([bazel_binary, "run", "--curses=yes", |
Jakob Buchgraber | 9965953 | 2018-02-19 17:38:50 +0100 | [diff] [blame] | 578 | "--color=yes", "--verbose_failures", target]) |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 579 | |
| 580 | |
Jakob Buchgraber | 4f1d271 | 2018-02-20 10:22:47 +0100 | [diff] [blame] | 581 | def remote_caching_flags(platform): |
Jakob Buchgraber | 27c5329 | 2018-02-22 10:48:53 +0100 | [diff] [blame] | 582 | if is_pull_request(): |
| 583 | common_flags = ["--bes_backend=buildeventservice.googleapis.com", "--bes_best_effort=false", |
| 584 | "--bes_timeout=10s", "--tls_enabled", "--project_id=bazel-public", |
| 585 | "--remote_instance_name=projects/bazel-public", |
| 586 | "--experimental_remote_spawn_cache", |
| 587 | "--remote_timeout=10", "--remote_cache=remotebuildexecution.googleapis.com", |
| 588 | "--experimental_remote_platform_override=properties:{name:\"platform\" value:\"" + platform + "\"}"] |
| 589 | else: |
| 590 | common_flags = ["--remote_timeout=10", "--experimental_remote_spawn_cache", |
| 591 | "--experimental_remote_platform_override=properties:{name:\"platform\" value:\"" + platform + "\"}", |
| 592 | "--remote_http_cache=https://storage.googleapis.com/bazel-buildkite-cache"] |
Jakob Buchgraber | c38a719 | 2018-02-20 12:56:52 +0100 | [diff] [blame] | 593 | if platform in ["ubuntu1404", "ubuntu1604"]: |
| 594 | return common_flags + ["--google_default_credentials"] |
| 595 | elif platform == "macos": |
| 596 | return common_flags + ["--google_credentials=/Users/ci/GoogleDrive/bazel-public-e29b1f995cb1.json"] |
Jakob Buchgraber | 4f1d271 | 2018-02-20 10:22:47 +0100 | [diff] [blame] | 597 | return [] |
| 598 | |
| 599 | |
Jakob Buchgraber | b4342cd | 2018-02-20 16:35:07 +0100 | [diff] [blame] | 600 | def remote_enabled(flags): |
| 601 | # Detect if the project configuration enabled its own remote caching / execution. |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 602 | remote_flags = ["--remote_executor", |
| 603 | "--remote_cache", "--remote_http_cache"] |
Jakob Buchgraber | b4342cd | 2018-02-20 16:35:07 +0100 | [diff] [blame] | 604 | for flag in flags: |
| 605 | for remote_flag in remote_flags: |
| 606 | if flag.startswith(remote_flag): |
| 607 | return True |
| 608 | return False |
| 609 | |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 610 | |
| 611 | def execute_bazel_build(bazel_binary, platform, flags, targets, bep_file): |
Jakob Buchgraber | 7d1d3bb | 2018-02-21 22:38:22 +0100 | [diff] [blame] | 612 | print_expanded_group(":bazel: Build") |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 613 | num_jobs = str(multiprocessing.cpu_count()) |
Jakob Buchgraber | ad6692e | 2018-02-20 11:12:00 +0100 | [diff] [blame] | 614 | common_flags = ["--show_progress_rate_limit=5", "--curses=yes", "--color=yes", "--keep_going", |
Jakob Buchgraber | d26a7d9 | 2018-02-21 18:53:54 +0100 | [diff] [blame] | 615 | "--jobs=" + num_jobs, "--build_event_json_file=" + bep_file, |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 616 | "--experimental_build_event_json_file_path_conversion=false"] |
Jakob Buchgraber | b4342cd | 2018-02-20 16:35:07 +0100 | [diff] [blame] | 617 | caching_flags = [] |
| 618 | if not remote_enabled(flags): |
| 619 | caching_flags = remote_caching_flags(platform) |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 620 | try: |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 621 | execute_command([bazel_binary, "build"] + |
| 622 | common_flags + caching_flags + flags + targets) |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 623 | except subprocess.CalledProcessError as e: |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 624 | raise BazelBuildFailedException( |
| 625 | "bazel build failed with exit code {}".format(e.returncode)) |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 626 | |
| 627 | |
Jakob Buchgraber | d220429 | 2018-02-20 10:25:40 +0100 | [diff] [blame] | 628 | def execute_bazel_test(bazel_binary, platform, flags, targets, bep_file): |
Jakob Buchgraber | 7d1d3bb | 2018-02-21 22:38:22 +0100 | [diff] [blame] | 629 | print_expanded_group(":bazel: Test") |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 630 | num_jobs = str(multiprocessing.cpu_count()) |
Jakob Buchgraber | 77175c7 | 2018-02-20 15:32:07 +0100 | [diff] [blame] | 631 | common_flags = ["--show_progress_rate_limit=5", "--curses=yes", "--color=yes", "--keep_going", |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 632 | "--flaky_test_attempts=3", "--build_tests_only", |
| 633 | "--jobs=" + num_jobs, "--local_test_jobs=" + num_jobs, |
Jakob Buchgraber | 77595f1 | 2018-02-21 11:44:18 +0100 | [diff] [blame] | 634 | "--build_event_json_file=" + bep_file, |
| 635 | "--experimental_build_event_json_file_path_conversion=false"] |
Jakob Buchgraber | b4342cd | 2018-02-20 16:35:07 +0100 | [diff] [blame] | 636 | caching_flags = [] |
| 637 | if not remote_enabled(flags): |
| 638 | caching_flags = remote_caching_flags(platform) |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 639 | try: |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 640 | execute_command([bazel_binary, "test"] + |
| 641 | common_flags + caching_flags + flags + targets) |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 642 | except subprocess.CalledProcessError as e: |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 643 | raise BazelTestFailedException( |
| 644 | "bazel test failed with exit code {}".format(e.returncode)) |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 645 | |
| 646 | |
Jakob Buchgraber | 02e0722 | 2018-02-19 15:05:56 +0100 | [diff] [blame] | 647 | def upload_test_logs(bep_file, tmpdir): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 648 | if not os.path.exists(bep_file): |
| 649 | return |
| 650 | test_logs = test_logs_to_upload(bep_file, tmpdir) |
| 651 | if test_logs: |
| 652 | cwd = os.getcwd() |
| 653 | try: |
| 654 | os.chdir(tmpdir) |
Jakob Buchgraber | 7d1d3bb | 2018-02-21 22:38:22 +0100 | [diff] [blame] | 655 | print_collapsed_group(":gcloud: Uploading Test Logs") |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 656 | execute_command( |
| 657 | ["buildkite-agent", "artifact", "upload", "*/**/*.log"]) |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 658 | finally: |
| 659 | os.chdir(cwd) |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 660 | |
| 661 | |
Jakob Buchgraber | 02e0722 | 2018-02-19 15:05:56 +0100 | [diff] [blame] | 662 | def test_logs_to_upload(bep_file, tmpdir): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 663 | failed = test_logs_for_status(bep_file, status="FAILED") |
| 664 | timed_out = test_logs_for_status(bep_file, status="TIMEOUT") |
| 665 | flaky = test_logs_for_status(bep_file, status="FLAKY") |
| 666 | # Rename the test.log files to the target that created them |
| 667 | # so that it's easy to associate test.log and target. |
| 668 | new_paths = [] |
Philipp Wollermann | ff39ef5 | 2018-02-21 14:18:52 +0100 | [diff] [blame] | 669 | for label, test_logs in failed + timed_out + flaky: |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 670 | attempt = 0 |
| 671 | if len(test_logs) > 1: |
| 672 | attempt = 1 |
| 673 | for test_log in test_logs: |
Jakob Buchgraber | 070ef5f | 2018-02-20 17:57:14 +0100 | [diff] [blame] | 674 | try: |
| 675 | new_path = test_label_to_path(tmpdir, label, attempt) |
Jakob Buchgraber | 070ef5f | 2018-02-20 17:57:14 +0100 | [diff] [blame] | 676 | os.makedirs(os.path.dirname(new_path), exist_ok=True) |
Jakob Buchgraber | 070ef5f | 2018-02-20 17:57:14 +0100 | [diff] [blame] | 677 | copyfile(test_log, new_path) |
Jakob Buchgraber | 070ef5f | 2018-02-20 17:57:14 +0100 | [diff] [blame] | 678 | new_paths.append(new_path) |
Philipp Wollermann | c030f2e | 2018-02-21 17:02:19 +0100 | [diff] [blame] | 679 | attempt += 1 |
Jakob Buchgraber | 070ef5f | 2018-02-20 17:57:14 +0100 | [diff] [blame] | 680 | except IOError as err: |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 681 | # Log error and ignore. |
| 682 | eprint(err) |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 683 | return new_paths |
Jakob Buchgraber | 699aece | 2018-02-19 12:49:30 +0100 | [diff] [blame] | 684 | |
| 685 | |
Jakob Buchgraber | 02e0722 | 2018-02-19 15:05:56 +0100 | [diff] [blame] | 686 | def test_label_to_path(tmpdir, label, attempt): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 687 | # remove leading // |
| 688 | path = label[2:] |
Philipp Wollermann | c030f2e | 2018-02-21 17:02:19 +0100 | [diff] [blame] | 689 | path = path.replace("/", os.sep) |
| 690 | path = path.replace(":", os.sep) |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 691 | if attempt == 0: |
| 692 | path = os.path.join(path, "test.log") |
| 693 | else: |
| 694 | path = os.path.join(path, "attempt_" + str(attempt) + ".log") |
| 695 | return os.path.join(tmpdir, path) |
Jakob Buchgraber | 699aece | 2018-02-19 12:49:30 +0100 | [diff] [blame] | 696 | |
| 697 | |
Jakob Buchgraber | 02e0722 | 2018-02-19 15:05:56 +0100 | [diff] [blame] | 698 | def test_logs_for_status(bep_file, status): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 699 | targets = [] |
| 700 | raw_data = "" |
Jakob Buchgraber | 94d5c21 | 2018-02-22 09:57:08 +0100 | [diff] [blame] | 701 | with open(bep_file, encoding="utf-8") as f: |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 702 | raw_data = f.read() |
| 703 | decoder = json.JSONDecoder() |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 704 | |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 705 | pos = 0 |
| 706 | while pos < len(raw_data): |
| 707 | bep_obj, size = decoder.raw_decode(raw_data[pos:]) |
Jakob Buchgraber | 45e3863 | 2018-02-19 17:27:08 +0100 | [diff] [blame] | 708 | if "testSummary" in bep_obj: |
| 709 | test_target = bep_obj["id"]["testSummary"]["label"] |
| 710 | test_status = bep_obj["testSummary"]["overallStatus"] |
| 711 | if test_status == status: |
| 712 | outputs = bep_obj["testSummary"]["failed"] |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 713 | test_logs = [] |
| 714 | for output in outputs: |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 715 | test_logs.append(url2pathname( |
| 716 | urlparse(output["uri"]).path)) |
Jakob Buchgraber | 45e3863 | 2018-02-19 17:27:08 +0100 | [diff] [blame] | 717 | targets.append((test_target, test_logs)) |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 718 | pos += size + 1 |
| 719 | return targets |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 720 | |
| 721 | |
Philipp Wollermann | 3e1a771 | 2018-02-19 17:34:24 +0100 | [diff] [blame] | 722 | def execute_command(args, shell=False, fail_if_nonzero=True): |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 723 | eprint(" ".join(args)) |
| 724 | return subprocess.run(args, shell=shell, check=fail_if_nonzero).returncode |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 725 | |
| 726 | |
Jakob Buchgraber | 22890f1 | 2018-02-21 20:33:25 +0100 | [diff] [blame] | 727 | def untrusted_code_verification_step(): |
Jakob Buchgraber | 1abb420 | 2018-02-21 20:30:28 +0100 | [diff] [blame] | 728 | return """ |
| 729 | - block: \"Untrusted Code Verification\" |
| 730 | prompt: \"Did you review this pull request for malicious code?\"""" |
| 731 | |
Jakob Buchgraber | 16b29a6 | 2018-02-21 20:46:40 +0100 | [diff] [blame] | 732 | |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 733 | def print_project_pipeline(platform_configs, project_name, http_config, |
| 734 | git_repository, use_but): |
Jakob Buchgraber | aa2af38 | 2018-02-21 19:56:54 +0100 | [diff] [blame] | 735 | pipeline_steps = [] |
Jakob Buchgraber | 1abb420 | 2018-02-21 20:30:28 +0100 | [diff] [blame] | 736 | if is_pull_request(): |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 737 | trusted_git_repository = git_repository |
| 738 | if not trusted_git_repository: |
| 739 | trusted_git_repository = os.getenv("BUILDKITE_REPO") |
Jakob Buchgraber | c16e422 | 2018-02-21 21:18:24 +0100 | [diff] [blame] | 740 | commit = os.getenv("BUILDKITE_COMMIT") |
Jakob Buchgraber | 0109fa7 | 2018-02-21 22:13:10 +0100 | [diff] [blame] | 741 | update_pull_request_verification_status(trusted_git_repository, commit, state="pending") |
Jakob Buchgraber | f465695 | 2018-02-21 20:34:35 +0100 | [diff] [blame] | 742 | pipeline_steps.append(untrusted_code_verification_step()) |
Philipp Wollermann | ff39ef5 | 2018-02-21 14:18:52 +0100 | [diff] [blame] | 743 | for platform, _ in platform_configs.items(): |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 744 | step = runner_step(platform, project_name, |
| 745 | http_config, git_repository, use_but) |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 746 | pipeline_steps.append(step) |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 747 | |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 748 | print_pipeline(pipeline_steps) |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 749 | |
| 750 | |
Jakob Buchgraber | aa2af38 | 2018-02-21 19:56:54 +0100 | [diff] [blame] | 751 | def runner_step(platform, project_name=None, http_config=None, |
Philipp Wollermann | f6be466 | 2018-02-21 14:48:28 +0100 | [diff] [blame] | 752 | git_repository=None, use_but=False): |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 753 | command = python_binary(platform) + \ |
| 754 | " bazelci.py runner --platform=" + platform |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 755 | if http_config: |
Philipp Wollermann | f6be466 | 2018-02-21 14:48:28 +0100 | [diff] [blame] | 756 | command += " --http_config=" + http_config |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 757 | if git_repository: |
Philipp Wollermann | f6be466 | 2018-02-21 14:48:28 +0100 | [diff] [blame] | 758 | command += " --git_repository=" + git_repository |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 759 | if use_but: |
Philipp Wollermann | f6be466 | 2018-02-21 14:48:28 +0100 | [diff] [blame] | 760 | command += " --use_but" |
| 761 | label = create_label(platform, project_name) |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 762 | return """ |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 763 | - label: \"{0}\" |
| 764 | command: \"{1}\\n{2}\" |
| 765 | agents: |
| 766 | - \"os={3}\"""".format(label, fetch_bazelcipy_command(), command, platform) |
| 767 | |
| 768 | |
| 769 | def print_pipeline(steps): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 770 | print("steps:") |
| 771 | for step in steps: |
| 772 | print(step) |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 773 | |
| 774 | |
| 775 | def wait_step(): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 776 | return """ |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 777 | - wait""" |
| 778 | |
| 779 | |
| 780 | def http_config_flag(http_config): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 781 | if http_config is not None: |
| 782 | return "--http_config=" + http_config |
| 783 | return "" |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 784 | |
| 785 | |
| 786 | def fetch_bazelcipy_command(): |
Jakob Buchgraber | 9cd9d75 | 2018-02-21 19:22:03 +0100 | [diff] [blame] | 787 | return "curl -s {0} -o bazelci.py".format(bazelcipy_url()) |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 788 | |
| 789 | |
| 790 | def upload_project_pipeline_step(project_name, git_repository, http_config): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 791 | pipeline_command = ("{0} bazelci.py project_pipeline --project_name=\\\"{1}\\\" " + |
| 792 | "--use_but --git_repository={2}").format(python_binary(), project_name, |
| 793 | git_repository) |
| 794 | if http_config: |
Philipp Wollermann | f6be466 | 2018-02-21 14:48:28 +0100 | [diff] [blame] | 795 | pipeline_command += " --http_config=" + http_config |
| 796 | pipeline_command += " | buildkite-agent pipeline upload" |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 797 | |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 798 | return """ |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 799 | - label: \"Setup {0}\" |
| 800 | command: \"{1}\\n{2}\" |
| 801 | agents: |
| 802 | - \"pipeline=true\"""".format(project_name, fetch_bazelcipy_command(), |
| 803 | pipeline_command) |
| 804 | |
| 805 | |
Philipp Wollermann | f6be466 | 2018-02-21 14:48:28 +0100 | [diff] [blame] | 806 | def create_label(platform, project_name, build_only=False, test_only=False): |
| 807 | if build_only and test_only: |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 808 | raise BuildkiteException( |
| 809 | "build_only and test_only cannot be true at the same time") |
Jakob Buchgraber | 7d1d3bb | 2018-02-21 22:38:22 +0100 | [diff] [blame] | 810 | platform_name = platforms_info()[platform]["emoji-name"] |
Philipp Wollermann | f6be466 | 2018-02-21 14:48:28 +0100 | [diff] [blame] | 811 | |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 812 | if build_only: |
| 813 | label = "Build " |
Philipp Wollermann | f6be466 | 2018-02-21 14:48:28 +0100 | [diff] [blame] | 814 | elif test_only: |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 815 | label = "Test " |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 816 | else: |
Philipp Wollermann | f6be466 | 2018-02-21 14:48:28 +0100 | [diff] [blame] | 817 | label = "" |
| 818 | |
| 819 | if project_name: |
| 820 | label += "{0} ({1})".format(project_name, platform_name) |
| 821 | else: |
| 822 | label += platform_name |
| 823 | |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 824 | return label |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 825 | |
| 826 | |
Philipp Wollermann | f6be466 | 2018-02-21 14:48:28 +0100 | [diff] [blame] | 827 | def bazel_build_step(platform, project_name, http_config=None, build_only=False, test_only=False): |
Philipp Wollermann | bcedf9b | 2018-02-19 18:07:44 +0100 | [diff] [blame] | 828 | pipeline_command = python_binary(platform) + " bazelci.py runner" |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 829 | if build_only: |
Philipp Wollermann | f6be466 | 2018-02-21 14:48:28 +0100 | [diff] [blame] | 830 | pipeline_command += " --build_only --save_but" |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 831 | if test_only: |
Philipp Wollermann | f6be466 | 2018-02-21 14:48:28 +0100 | [diff] [blame] | 832 | pipeline_command += " --test_only" |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 833 | if http_config: |
Philipp Wollermann | f6be466 | 2018-02-21 14:48:28 +0100 | [diff] [blame] | 834 | pipeline_command += " --http_config=" + http_config |
| 835 | label = create_label(platform, project_name, build_only, test_only) |
| 836 | pipeline_command += " --platform=" + platform |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 837 | |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 838 | return """ |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 839 | - label: \"{0}\" |
| 840 | command: \"{1}\\n{2}\" |
| 841 | agents: |
| 842 | - \"os={3}\"""".format(label, fetch_bazelcipy_command(), |
| 843 | pipeline_command, platform) |
| 844 | |
| 845 | |
Jakob Buchgraber | 76381e0 | 2018-02-19 16:19:56 +0100 | [diff] [blame] | 846 | def publish_bazel_binaries_step(): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 847 | command = python_binary() + " bazelci.py publish_binaries" |
| 848 | return """ |
Jakob Buchgraber | 76381e0 | 2018-02-19 16:19:56 +0100 | [diff] [blame] | 849 | - label: \"Publish Bazel Binaries\" |
| 850 | command: \"{0}\\n{1}\" |
Jakob Buchgraber | d20ffeb | 2018-02-18 03:16:43 +0100 | [diff] [blame] | 851 | agents: |
Jakob Buchgraber | 76381e0 | 2018-02-19 16:19:56 +0100 | [diff] [blame] | 852 | - \"pipeline=true\"""".format(fetch_bazelcipy_command(), command) |
Jakob Buchgraber | d20ffeb | 2018-02-18 03:16:43 +0100 | [diff] [blame] | 853 | |
| 854 | |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 855 | def print_bazel_postsubmit_pipeline(configs, http_config): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 856 | if not configs: |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 857 | raise BuildkiteException( |
| 858 | "Bazel postsubmit pipeline configuration is empty.") |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 859 | if set(configs.keys()) != set(supported_platforms()): |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 860 | raise BuildkiteException( |
| 861 | "Bazel postsubmit pipeline needs to build Bazel on all supported platforms.") |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 862 | |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 863 | pipeline_steps = [] |
| 864 | for platform, config in configs.items(): |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 865 | pipeline_steps.append(bazel_build_step( |
| 866 | platform, "Bazel", http_config, build_only=True)) |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 867 | pipeline_steps.append(wait_step()) |
Jakob Buchgraber | d20ffeb | 2018-02-18 03:16:43 +0100 | [diff] [blame] | 868 | |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 869 | # todo move this to the end with a wait step. |
| 870 | pipeline_steps.append(publish_bazel_binaries_step()) |
Jakob Buchgraber | d20ffeb | 2018-02-18 03:16:43 +0100 | [diff] [blame] | 871 | |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 872 | for platform, config in configs.items(): |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 873 | pipeline_steps.append(bazel_build_step( |
| 874 | platform, "Bazel", http_config, test_only=True)) |
Philipp Wollermann | f6be466 | 2018-02-21 14:48:28 +0100 | [diff] [blame] | 875 | |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 876 | for project, config in downstream_projects().items(): |
| 877 | git_repository = config["git_repository"] |
| 878 | http_config = config.get("http_config", None) |
| 879 | pipeline_steps.append(upload_project_pipeline_step(project, |
| 880 | git_repository, http_config)) |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 881 | |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 882 | print_pipeline(pipeline_steps) |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 883 | |
| 884 | |
Jakob Buchgraber | d20ffeb | 2018-02-18 03:16:43 +0100 | [diff] [blame] | 885 | def bazelci_builds_download_url(platform, build_number): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 886 | return "https://storage.googleapis.com/bazel-builds/artifacts/{0}/{1}/bazel".format(platform, build_number) |
Jakob Buchgraber | d20ffeb | 2018-02-18 03:16:43 +0100 | [diff] [blame] | 887 | |
| 888 | |
| 889 | def bazelci_builds_upload_url(platform, build_number): |
Jakob Buchgraber | 2a2304f | 2018-02-19 21:29:44 +0100 | [diff] [blame] | 890 | return "gs://bazel-builds/artifacts/{0}/{1}/bazel".format(platform, build_number) |
Jakob Buchgraber | d20ffeb | 2018-02-18 03:16:43 +0100 | [diff] [blame] | 891 | |
| 892 | |
Jakob Buchgraber | 76381e0 | 2018-02-19 16:19:56 +0100 | [diff] [blame] | 893 | def bazelci_builds_metadata_url(): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 894 | return "gs://bazel-builds/metadata/latest_fully_tested.json" |
Jakob Buchgraber | d20ffeb | 2018-02-18 03:16:43 +0100 | [diff] [blame] | 895 | |
| 896 | |
Jakob Buchgraber | 76381e0 | 2018-02-19 16:19:56 +0100 | [diff] [blame] | 897 | def latest_generation_and_build_number(): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 898 | output = None |
| 899 | attempt = 0 |
| 900 | while attempt < 5: |
| 901 | output = subprocess.check_output( |
| 902 | ["gsutil", "stat", bazelci_builds_metadata_url()]) |
| 903 | match = re.search("Generation:[ ]*([0-9]+)", output.decode("utf-8")) |
| 904 | if not match: |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 905 | raise BuildkiteException( |
| 906 | "Couldn't parse generation. gsutil output format changed?") |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 907 | generation = match.group(1) |
Jakob Buchgraber | d20ffeb | 2018-02-18 03:16:43 +0100 | [diff] [blame] | 908 | |
Philipp Wollermann | ff39ef5 | 2018-02-21 14:18:52 +0100 | [diff] [blame] | 909 | match = re.search(r"Hash \(md5\):[ ]*([^\s]+)", output.decode("utf-8")) |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 910 | if not match: |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 911 | raise BuildkiteException( |
| 912 | "Couldn't parse md5 hash. gsutil output format changed?") |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 913 | expected_md5hash = base64.b64decode(match.group(1)) |
Jakob Buchgraber | d20ffeb | 2018-02-18 03:16:43 +0100 | [diff] [blame] | 914 | |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 915 | output = subprocess.check_output( |
| 916 | ["gsutil", "cat", bazelci_builds_metadata_url()]) |
| 917 | hasher = hashlib.md5() |
| 918 | hasher.update(output) |
| 919 | actual_md5hash = hasher.digest() |
Jakob Buchgraber | d20ffeb | 2018-02-18 03:16:43 +0100 | [diff] [blame] | 920 | |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 921 | if expected_md5hash == actual_md5hash: |
| 922 | break |
Philipp Wollermann | f6be466 | 2018-02-21 14:48:28 +0100 | [diff] [blame] | 923 | attempt += 1 |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 924 | info = json.loads(output.decode("utf-8")) |
| 925 | return (generation, info["build_number"]) |
Jakob Buchgraber | d20ffeb | 2018-02-18 03:16:43 +0100 | [diff] [blame] | 926 | |
Jakob Buchgraber | 699aece | 2018-02-19 12:49:30 +0100 | [diff] [blame] | 927 | |
Jakob Buchgraber | 88083fd | 2018-02-18 17:23:35 +0100 | [diff] [blame] | 928 | def sha256_hexdigest(filename): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 929 | sha256 = hashlib.sha256() |
| 930 | with open(filename, 'rb') as f: |
| 931 | for block in iter(lambda: f.read(65536), b''): |
| 932 | sha256.update(block) |
| 933 | return sha256.hexdigest() |
Jakob Buchgraber | 699aece | 2018-02-19 12:49:30 +0100 | [diff] [blame] | 934 | |
Jakob Buchgraber | d20ffeb | 2018-02-18 03:16:43 +0100 | [diff] [blame] | 935 | |
Jakob Buchgraber | 76381e0 | 2018-02-19 16:19:56 +0100 | [diff] [blame] | 936 | def try_publish_binaries(build_number, expected_generation): |
Philipp Wollermann | 3e1a771 | 2018-02-19 17:34:24 +0100 | [diff] [blame] | 937 | tmpdir = tempfile.mkdtemp() |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 938 | try: |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 939 | info = { |
| 940 | "build_number": build_number, |
| 941 | "git_commit": os.environ["BUILDKITE_COMMIT"], |
| 942 | "platforms": {} |
| 943 | } |
| 944 | for platform in supported_platforms(): |
| 945 | bazel_binary_path = download_bazel_binary(tmpdir, platform) |
Philipp Wollermann | 3e1a771 | 2018-02-19 17:34:24 +0100 | [diff] [blame] | 946 | execute_command(["gsutil", "cp", "-a", "public-read", bazel_binary_path, |
| 947 | bazelci_builds_upload_url(platform, build_number)]) |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 948 | info["platforms"][platform] = { |
| 949 | "url": bazelci_builds_download_url(platform, build_number), |
| 950 | "sha256": sha256_hexdigest(bazel_binary_path), |
| 951 | } |
Jakob Buchgraber | 76381e0 | 2018-02-19 16:19:56 +0100 | [diff] [blame] | 952 | |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 953 | info_file = os.path.join(tmpdir, "info.json") |
| 954 | with open(info_file, mode="w", encoding="utf-8") as fp: |
| 955 | json.dump(info, fp) |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 956 | |
| 957 | try: |
| 958 | execute_command([ |
| 959 | "gsutil", |
| 960 | "-h", "x-goog-if-generation-match:" + expected_generation, |
| 961 | "-h", "Content-Type:application/json", |
| 962 | "cp", "-a", "public-read", |
| 963 | info_file, bazelci_builds_metadata_url()]) |
| 964 | except subprocess.CalledProcessError: |
| 965 | raise BinaryUploadRaceException() |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 966 | finally: |
Philipp Wollermann | 3e1a771 | 2018-02-19 17:34:24 +0100 | [diff] [blame] | 967 | shutil.rmtree(tmpdir) |
Jakob Buchgraber | d20ffeb | 2018-02-18 03:16:43 +0100 | [diff] [blame] | 968 | |
| 969 | |
Jakob Buchgraber | 76381e0 | 2018-02-19 16:19:56 +0100 | [diff] [blame] | 970 | def publish_binaries(): |
Philipp Wollermann | db02486 | 2018-02-19 17:16:56 +0100 | [diff] [blame] | 971 | """ |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 972 | Publish Bazel binaries to GCS. |
Philipp Wollermann | db02486 | 2018-02-19 17:16:56 +0100 | [diff] [blame] | 973 | """ |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 974 | current_build_number = os.environ.get("BUILDKITE_BUILD_NUMBER", None) |
| 975 | if not current_build_number: |
| 976 | raise BuildkiteException("Not running inside Buildkite") |
| 977 | current_build_number = int(current_build_number) |
| 978 | |
| 979 | for _ in range(5): |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 980 | latest_generation, latest_build_number = latest_generation_and_build_number() |
Jakob Buchgraber | d20ffeb | 2018-02-18 03:16:43 +0100 | [diff] [blame] | 981 | |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 982 | if current_build_number <= latest_build_number: |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 983 | eprint(("Current build '{0}' is not newer than latest published '{1}'. " + |
| 984 | "Skipping publishing of binaries.").format(current_build_number, |
| 985 | latest_build_number)) |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 986 | break |
Jakob Buchgraber | d20ffeb | 2018-02-18 03:16:43 +0100 | [diff] [blame] | 987 | |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 988 | try: |
| 989 | try_publish_binaries(current_build_number, latest_generation) |
| 990 | except BinaryUploadRaceException: |
| 991 | # Retry. |
| 992 | continue |
| 993 | |
| 994 | eprint("Successfully updated '{0}' to binaries from build {1}." |
| 995 | .format(bazelci_builds_metadata_url(), current_build_number)) |
| 996 | break |
| 997 | else: |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 998 | raise BuildkiteException( |
| 999 | "Could not publish binaries, ran out of attempts.") |
Jakob Buchgraber | d20ffeb | 2018-02-18 03:16:43 +0100 | [diff] [blame] | 1000 | |
| 1001 | |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 1002 | def main(argv=None): |
| 1003 | if argv is None: |
| 1004 | argv = sys.argv |
| 1005 | |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 1006 | parser = argparse.ArgumentParser( |
| 1007 | description='Bazel Continuous Integration Script') |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 1008 | |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 1009 | subparsers = parser.add_subparsers(dest="subparsers_name") |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 1010 | |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 1011 | bazel_postsubmit_pipeline = subparsers.add_parser( |
| 1012 | "bazel_postsubmit_pipeline") |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 1013 | bazel_postsubmit_pipeline.add_argument("--http_config", type=str) |
| 1014 | bazel_postsubmit_pipeline.add_argument("--git_repository", type=str) |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 1015 | |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 1016 | project_pipeline = subparsers.add_parser("project_pipeline") |
| 1017 | project_pipeline.add_argument("--project_name", type=str) |
| 1018 | project_pipeline.add_argument("--http_config", type=str) |
| 1019 | project_pipeline.add_argument("--git_repository", type=str) |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 1020 | project_pipeline.add_argument( |
| 1021 | "--use_but", type=bool, nargs="?", const=True) |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 1022 | |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 1023 | runner = subparsers.add_parser("runner") |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 1024 | runner.add_argument("--platform", action="store", |
| 1025 | choices=list(supported_platforms())) |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 1026 | runner.add_argument("--http_config", type=str) |
| 1027 | runner.add_argument("--git_repository", type=str) |
| 1028 | runner.add_argument("--use_but", type=bool, nargs="?", const=True) |
| 1029 | runner.add_argument("--save_but", type=bool, nargs="?", const=True) |
| 1030 | runner.add_argument("--build_only", type=bool, nargs="?", const=True) |
| 1031 | runner.add_argument("--test_only", type=bool, nargs="?", const=True) |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 1032 | |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 1033 | runner = subparsers.add_parser("publish_binaries") |
Jakob Buchgraber | d20ffeb | 2018-02-18 03:16:43 +0100 | [diff] [blame] | 1034 | |
Philipp Wollermann | 598b4a4 | 2018-02-19 17:03:36 +0100 | [diff] [blame] | 1035 | args = parser.parse_args() |
Jakob Buchgraber | 0b6a39d | 2018-02-17 00:45:14 +0100 | [diff] [blame] | 1036 | |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 1037 | try: |
| 1038 | if args.subparsers_name == "bazel_postsubmit_pipeline": |
| 1039 | configs = fetch_configs(args.http_config) |
Jakob Buchgraber | 6104a43 | 2018-02-21 21:16:53 +0100 | [diff] [blame] | 1040 | print_bazel_postsubmit_pipeline( |
| 1041 | configs.get("platforms", None), args.http_config) |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 1042 | elif args.subparsers_name == "project_pipeline": |
| 1043 | configs = fetch_configs(args.http_config) |
| 1044 | print_project_pipeline(configs.get("platforms", None), args.project_name, |
| 1045 | args.http_config, args.git_repository, args.use_but) |
| 1046 | elif args.subparsers_name == "runner": |
| 1047 | configs = fetch_configs(args.http_config) |
| 1048 | execute_commands(configs.get("platforms", None)[args.platform], |
| 1049 | args.platform, args.git_repository, args.use_but, args.save_but, |
| 1050 | args.build_only, args.test_only) |
| 1051 | elif args.subparsers_name == "publish_binaries": |
| 1052 | publish_binaries() |
| 1053 | else: |
| 1054 | parser.print_help() |
| 1055 | return 2 |
| 1056 | except BuildkiteException as e: |
| 1057 | eprint(str(e)) |
| 1058 | return 1 |
| 1059 | return 0 |
| 1060 | |
Jakob Buchgraber | 95e3d57 | 2018-02-21 18:48:49 +0100 | [diff] [blame] | 1061 | |
Philipp Wollermann | dcaddd9 | 2018-02-21 14:13:43 +0100 | [diff] [blame] | 1062 | if __name__ == "__main__": |
| 1063 | sys.exit(main()) |