-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
BUILD.bazel
106 lines (96 loc) · 2.92 KB
/
BUILD.bazel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup")
load("@aspect_rules_py//py:defs.bzl", "py_binary", "py_library")
load("@container_structure_test//:defs.bzl", "container_structure_test")
load("@pip//:requirements.bzl", "requirement")
load("@rules_oci//oci:defs.bzl", "oci_load")
load("//:py_layer.bzl", "py_oci_image")
py_library(
name = "hello_world_lib",
srcs = [
"__init__.py",
"app.py",
],
imports = [".."],
visibility = ["//:__subpackages__"],
deps = [
requirement("cowsay"),
],
)
py_binary(
name = "hello_world",
srcs = ["__main__.py"],
imports = [".."],
main = "__main__.py",
visibility = ["//:__subpackages__"],
deps = [":hello_world_lib"],
)
# Construct an image that has three layers.
# The result will look like this:
# bazel run //hello_world:tarball
#
#2f2353bd5bea: Loading layer [==================================================>] 47.13MB/47.13MB
#f02532d45017: Loading layer [==================================================>] 3.62MB/3.62MB
#9296e9071c11: Loading layer [==================================================>] 16.24kB/16.24kB
py_oci_image(
name = "image",
base = "@ubuntu",
binary = "hello_world",
entrypoint = ["/hello_world/hello_world"],
)
platform(
name = "aarch64_linux",
constraint_values = [
"@platforms//os:linux",
"@platforms//cpu:aarch64",
],
)
platform(
name = "x86_64_linux",
constraint_values = [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
)
platform_transition_filegroup(
name = "platform_image",
srcs = [":image"],
target_platform = select({
"@platforms//cpu:arm64": ":aarch64_linux",
"@platforms//cpu:x86_64": ":x86_64_linux",
}),
)
# To build the image and load it into it into a local runtime:
# $ bazel run //hello_world:image_load
# $ docker run --rm gcr.io/oci_python_hello_world:latest
oci_load(
name = "image_load",
image = ":platform_image",
repo_tags = ["gcr.io/oci_python_hello_world:latest"],
)
# Use this if you need a tarball:
# $ bazel build //hello_world:image_load.tar
filegroup(
name = "image_load.tar",
srcs = [":image_load"],
output_group = "tarball",
)
container_structure_test(
name = "test",
configs = ["test.yaml"],
image = ":platform_image",
tags = ["requires-docker"],
)
py_test(
name = "test_container",
srcs = ["app_test.py"],
# NB: this depends on the image rather than the tarball, to avoid the test needing to wait for
# an action that reads all bytes of the layers and writes all those bytes again.
# However, in order for the image to be loaded into the Docker daemon from files on disk,
# the test Setup has to do some sophisticated work to load each layer.
data = [":platform_image"],
main = "app_test.py",
tags = ["requires-docker"],
deps = [
requirement("testcontainers"),
],
)