使用 Bazel 将 Go Test 二进制文件添加到 container_image

Add Go Test binary to container_image using Bazel

我正在构建一个 go 测试包,我想将其包含在 Docker 图像中。可以使用 bazel build //testing/e2e:e2e_test 构建二进制文件。这会在 bazel_bin 文件夹中创建一个二进制文件。现在我想把这个二进制文件添加到 docker 图像中...

container_image(
    name = "image",
    base = "@alpine_linux_amd64//image",
    entrypoint = ["/e2e_test"],
    files = [":e2e_test"],
)

这给了我以下错误

ERROR: ...BUILD.bazel:27:16: in container_image_ rule //testing/e2e:image: non-test target '//testing/e2e:image' depends on testonly target '//testing/e2e:e2e_test' and doesn't have testonly attribute set
ERROR: Analysis of target '//testing/e2e:image' failed; build aborted: Analysis of target '//testing/e2e:image' failed

最终,我想要完成的是使用 Go Test 框架创建一个包含一套端到端测试的应用程序。然后,我可以将这些测试分发到 docker 容器中,以便在测试环境中成为 运行。

错误是说一个非测试目标依赖于一个测试目标,the docs for testonly 说这是不允许的:

If True, only testonly targets (such as tests) can depend on this target.

Equivalently, a rule that is not testonly is not allowed to depend on any rule that is testonly.

您可以像这样设置目标 testonly 来完成您正在寻找的事情:

container_image(
    name = "image",
    testonly = True,
    base = "@alpine_linux_amd64//image",
    entrypoint = ["/e2e_test"],
    files = [":e2e_test"],
)