Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bazel loves Go

Yuki Ito
December 06, 2019

Bazel loves Go

Yuki Ito

December 06, 2019
Tweet

More Decks by Yuki Ito

Other Decks in Technology

Transcript

  1. What is Bazel 7 Bazel is an open-source build and

    test tool similar to Make, Maven, and Gradle. https://docs.bazel.build/versions/master/bazel-overview.html
  2. What is Bazel 9 High-level build language Starlark (dialect of

    Python) Starlark is a language intended for use as a configuration language. It was designed for the Bazel build system, but may be useful for other projects as well. https://github.com/bazelbuild/starlark
  3. What is Bazel 10 Starlark (dialect of Python) def fizz_buzz(n):

    """Print Fizz Buzz numbers from 1 to n.""" for i in range(1, n + 1): s = "" if i % 3 == 0: s += "Fizz" if i % 5 == 0: s += "Buzz" print(s if s else i) fizz_buzz(20)
  4. WORKSPACE & BUILD 13 ᵓ── BUILD.bazel ᵓ── WORKSPACE ᵓ── cmd

    │ ᵓ── BUILD.bazel │ ᵓ── apply.go │ └── ... ᵓ── main.go ᵓ── pkg │ └── spanner │ ᵓ── BUILD.bazel │ ᵓ── client.go │ └── ... └── ...
  5. WORKSPACE & BUILD 14 ᵓ── BUILD.bazel ᵓ── WORKSPACE ᵓ── cmd

    │ ᵓ── BUILD.bazel │ ᵓ── apply.go │ └── ... ᵓ── main.go ᵓ── pkg │ └── spanner │ ᵓ── BUILD.bazel │ ᵓ── client.go │ └── ... └── ... WORKSPACE
  6. WORKSPACE & BUILD 15 The WORKSPACE file, which identifies the

    directory and its contents as a Bazel workspace and lives at the root of the project’s directory structure. WORKSPACE
  7. 16 http_archive( name = "io_bazel_rules_go", urls = ["..."], ) load(

    "@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies", ) go_rules_dependencies() go_register_toolchains( go_version = "1.13.4", nogo = "@//:nogo", )
  8. 17 http_archive( name = "io_bazel_rules_go", urls = ["..."], ) load(

    "@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies", ) go_rules_dependencies() go_register_toolchains( go_version = "1.13.4", nogo = "@//:nogo", )
  9. 18 go_repository( name = "co_honnef_go_tools", importpath = "honnef.co/go/tools", sum =

    "h1:/hemPrYIhOhy8zYrNj+069zDB68us2sMGsfkFJO0iZs=", version = "v0.0.0-20190523083050-ea95bdfd59fc", ) go_repository( name = "com_github_burntsushi_toml", importpath = "github.com/BurntSushi/toml", sum = "h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=", version = "v0.3.1", ) go_repository( name = "com_github_burntsushi_xgb", importpath = "github.com/BurntSushi/xgb", sum = "h1:1BDTz0u9nC3//pOCMdNH+CiXJVYJh5UQNCOBG7jbELc=", version = "v0.0.0-20160522181843-27f122750802", ) ...
  10. WORKSPACE & BUILD 20 ᵓ── BUILD.bazel ᵓ── WORKSPACE ᵓ── cmd

    │ ᵓ── BUILD.bazel │ ᵓ── apply.go │ └── ... ᵓ── main.go ᵓ── pkg │ └── spanner │ ᵓ── BUILD.bazel │ ᵓ── client.go │ └── ... └── ... BUILD
  11. WORKSPACE & BUILD 21 One or more BUILD files, which

    tell Bazel how to build different parts of the project. BUILD
  12. 22 go_library( name = "go_default_library", srcs = ["main.go"], importpath =

    "github.com/mercari/wrench", visibility = ["//visibility:private"], deps = [ "//cmd:go_default_library", "//pkg/spanner:go_default_library", ], ) go_binary( name = "wrench", embed = [":go_default_library"], visibility = ["//visibility:public"], )
  13. 23 go_library( name = "go_default_library", srcs = [ "apply.go", ...

    ], importpath = "github.com/mercari/wrench/cmd", visibility = ["//visibility:public"], deps = [ "//pkg/spanner:go_default_library", "@com_github_spf13_cobra//:go_default_library", ], ) go_test( name = "go_default_test", srcs = [ "export_test.go", "migrate_test.go", ], data = glob(["testdata/**"]), embed = [":go_default_library"], )
  14. WORKSPACE & BUILD 24 ᵓ── BUILD.bazel ᵓ── WORKSPACE ᵓ── cmd

    │ ᵓ── BUILD.bazel │ ᵓ── apply.go │ └── ... ᵓ── main.go ᵓ── pkg │ └── spanner │ ᵓ── BUILD.bazel │ ᵓ── client.go │ └── ... └── ... Time consuming to write manually...
  15. WORKSPACE & BUILD 25 go_repository( name = "co_honnef_go_tools", importpath =

    "honnef.co/go/tools", sum = "h1:/hemPrYIhOhy8zYrNj+069zDB68us2sMGsfkFJO0iZs=", version = "v0.0.0-20190523083050-ea95bdfd59fc", ) go_repository( name = "com_github_burntsushi_toml", importpath = "github.com/BurntSushi/toml", sum = "h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=", version = "v0.3.1", ) go_repository( name = "com_github_burntsushi_xgb", importpath = "github.com/BurntSushi/xgb", sum = "h1:1BDTz0u9nC3//pOCMdNH+CiXJVYJh5UQNCOBG7jbELc=", version = "v0.0.0-20160522181843-27f122750802", ) ... Time consuming to write manually...
  16. WORKSPACE & BUILD 29 ᵓ── BUILD.bazel ᵓ── WORKSPACE ᵓ── cmd

    │ ᵓ── BUILD.bazel │ ᵓ── apply.go │ └── ... ᵓ── main.go ᵓ── pkg │ └── spanner │ ᵓ── BUILD.bazel │ ᵓ── client.go │ └── ... └── ... BUILD
  17. 32 go_repository( name = "co_honnef_go_tools", importpath = "honnef.co/go/tools", sum =

    "h1:/hemPrYIhOhy8zYrNj+069zDB68us2sMGsfkFJO0iZs=", version = "v0.0.0-20190523083050-ea95bdfd59fc", ) go_repository( name = "com_github_burntsushi_toml", importpath = "github.com/BurntSushi/toml", sum = "h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=", version = "v0.3.1", ) go_repository( name = "com_github_burntsushi_xgb", importpath = "github.com/BurntSushi/xgb", sum = "h1:1BDTz0u9nC3//pOCMdNH+CiXJVYJh5UQNCOBG7jbELc=", version = "v0.0.0-20160522181843-27f122750802", ) ...
  18. WORKSPACE & BUILD 33 ᵓ── BUILD.bazel ᵓ── WORKSPACE ᵓ── cmd

    │ ᵓ── BUILD.bazel │ ᵓ── apply.go │ └── ... ᵓ── main.go ᵓ── pkg │ └── spanner │ ᵓ── BUILD.bazel │ ᵓ── client.go │ └── ... └── ...
  19. 35 go_library( name = "go_default_library", srcs = ["main.go"], importpath =

    "github.com/mercari/wrench", visibility = ["//visibility:private"], deps = [ "//cmd:go_default_library", "//pkg/spanner:go_default_library", ], ) go_binary( name = "wrench", embed = [":go_default_library"], visibility = ["//visibility:public"], )
  20. Bake Docker Image 41 > ./bazel-bin/image help Loaded image ID:

    sha256:xxx Tagging xxx as bazel:image Usage: wrench [command] ...
  21. Bake Docker Image 43 Push to Registry load( "@io_bazel_rules_docker//container:container.bzl", "container_push",

    ) container_push( name = "registry", format = "Docker", image = ":image", registry = "index.docker.io", repository = "mercari/wrench", tag = version, )
  22. 48 go_repository( name = "co_honnef_go_tools", importpath = "honnef.co/go/tools", sum =

    "h1:/hemPrYIhOhy8zYrNj+069zDB68us2sMGsfkFJO0iZs=", version = "v0.0.0-20190523083050-ea95bdfd59fc", ) go_repository( name = "com_github_burntsushi_toml", importpath = "github.com/BurntSushi/toml", sum = "h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=", version = "v0.3.1", ) go_repository( name = "com_github_burntsushi_xgb", importpath = "github.com/BurntSushi/xgb", sum = "h1:1BDTz0u9nC3//pOCMdNH+CiXJVYJh5UQNCOBG7jbELc=", version = "v0.0.0-20160522181843-27f122750802", ) ... Extract Dependencies WORKSPACE
  23. 49 Extract Dependencies > bazel run //:gazelle -- \ update-repos

    \ -from_file go.mod \ -to_macro bazel/deps.bzl%wrench_deps
  24. 50 ᵓ── BUILD.bazel ᵓ── WORKSPACE ᵓ── bazel │ ᵓ── deps.bzl

    ᵓ── cmd │ ᵓ── BUILD.bazel │ ᵓ── apply.go │ └── ... ᵓ── main.go └── ... BUILD Extract Dependencies
  25. 51 load("@bazel_gazelle//:deps.bzl", "go_repository") def wrench_deps(): go_repository( name = "co_honnef_go_tools", importpath

    = "honnef.co/go/tools", sum = "h1:/hemPrYIhOhy8zYrNj+069zDB68us2sMGsfkFJO0iZs=", version = "v0.0.0-20190523083050-ea95bdfd59fc", ) go_repository( name = "com_github_burntsushi_toml", importpath = "github.com/BurntSushi/toml", sum = "h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=", version = "v0.3.1", ) Extract Dependencies
  26. nogo (experimental) 53 nogo is a tool that analyzes the

    source code of Go programs. It runs alongside the Go compiler in the Bazel Go rules and rejects programs that contain disallowed coding patterns. In addition, nogo may report compiler-like errors. https://github.com/bazelbuild/rules_go/blob/master/go/nogo.rst
  27. nogo (experimental) 54 nogo( name = "nogo", visibility = ["//visibility:public"],

    deps = [ "@org_golang_x_tools//go/analysis/passes/atomic:go_tool_library", "@org_golang_x_tools//go/analysis/passes/bools:go_tool_library", "@org_golang_x_tools//go/analysis/passes/buildtag:go_tool_library", "@org_golang_x_tools//go/analysis/passes/nilfunc:go_tool_library", "@org_golang_x_tools//go/analysis/passes/printf:go_tool_library", "@org_golang_x_tools//go/analysis/passes/structtag:go_tool_library", ], )
  28. Remote Caching 58 bazel-remote bazel-remote is a HTTP/1.1 and gRPC

    server that is intended to be used as a remote build cache for Bazel. The cache contents are stored in a directory on disk.
  29. Remote Caching 60 Google Cloud Storage resource "google_storage_bucket" "bazel-cache" {

    name = "my-bazel-remote-cache" location = "asia-northeast1" storage_class = "STANDARD" }
  30. Remote Caching 61 Google Cloud Storage resource "google_storage_bucket" "bazel-cache" {

    name = "my-bazel-remote-cache" location = "asia-northeast1" storage_class = "STANDARD" } ᶃ Create GCS Bucket
  31. Remote Caching 62 Google Cloud Storage ᶄ Specify Remote Bucket

    bazel build \ --google_credentials=<path to credentials.json> \ --remote_http_cache=https://storage.googleapis.com/<your bucket> \ //:image