docker.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. import os
  10. from pathlib import Path
  11. from materialize import ci_util, git, mzbuild, ui
  12. from materialize.mz_version import MzVersion
  13. from materialize.rustc_flags import Sanitizer
  14. from materialize.version_list import get_all_mz_versions
  15. from materialize.xcompile import Arch
  16. def main() -> None:
  17. bazel = ui.env_is_truthy("CI_BAZEL_BUILD")
  18. bazel_remote_cache = os.getenv("CI_BAZEL_REMOTE_CACHE")
  19. bazel_lto = ui.env_is_truthy("CI_BAZEL_LTO")
  20. repos = [
  21. mzbuild.Repository(
  22. Path("."),
  23. Arch.X86_64,
  24. coverage=False,
  25. sanitizer=Sanitizer.none,
  26. bazel=bazel,
  27. bazel_remote_cache=bazel_remote_cache,
  28. bazel_lto=bazel_lto,
  29. ),
  30. mzbuild.Repository(
  31. Path("."),
  32. Arch.AARCH64,
  33. coverage=False,
  34. sanitizer=Sanitizer.none,
  35. bazel=bazel,
  36. bazel_remote_cache=bazel_remote_cache,
  37. bazel_lto=bazel_lto,
  38. ),
  39. ]
  40. buildkite_tag = os.environ["BUILDKITE_TAG"]
  41. def include_image(image: mzbuild.Image) -> bool:
  42. # Images must always be publishable to be tagged. Only mainline images
  43. # get tagged for releases, but even non-mainline images get `unstable`
  44. # tags.
  45. return image.publish and (not buildkite_tag or image.mainline)
  46. print("--- Tagging Docker images")
  47. deps = [
  48. repo.resolve_dependencies(image for image in repo if include_image(image))
  49. for repo in repos
  50. ]
  51. if buildkite_tag:
  52. # On tag builds, always tag the images as such.
  53. mzbuild.publish_multiarch_images(buildkite_tag, deps)
  54. # Also tag the images as `latest` if this is the latest version.
  55. version = MzVersion.parse_mz(buildkite_tag)
  56. latest_version = max(t for t in get_all_mz_versions() if t.prerelease is None)
  57. if version == latest_version:
  58. mzbuild.publish_multiarch_images("latest", deps)
  59. else:
  60. mz_version = ci_util.get_mz_version()
  61. mzbuild.publish_multiarch_images("unstable", deps)
  62. # Ideally we'd use SemVer metadata (e.g., `v1.0.0+metadata`), but `+`
  63. # is not a valid character in Docker tags, so we use `--` instead.
  64. mzbuild.publish_multiarch_images(
  65. f'v{mz_version}--main.g{git.rev_parse("HEAD")}', deps
  66. )
  67. # Sync image descriptions to Docker Hub. The image descriptions are the
  68. # same across architectures, so we arbitrarily choose the first
  69. # repository.
  70. for image in repos[0]:
  71. image.sync_description()
  72. if __name__ == "__main__":
  73. main()