How to load multiple dependencies in py3_image bazel rule

937 Views Asked by At

I recently started working with bazel so admittedly, have little knowledge of bazel intricacies. I'm using bazel to generate docker images but I want to use multiple deps inside the py3_image rule.

I have a BUILD.bazel which has python rule as follows:

load("@io_bazel_rules_docker//python3:image.bzl", "py3_image")
load("@io_bazel_rules_docker//container:container.bzl", "container_push")
load("@custom_deps//:requirements.bzl", "requirement")

exports_files(["component.yaml"])

py3_image(
    name = "custom",
    srcs = [
        "src/payload_populator/bq_populator.py",
        "src/payload_populator/cloudsql_fetcher.py",
        "src/payload_populator/config.py",
        "src/payload_populator/SingleListing.py",
        "src/payload_populator/sql.py",
        "src/custom/browse.py",
        "src/custom/closet.py",
        "src/custom/constants.py",
        "src/custom/listing.py",
        "src/custom/util.py",
        "src/session/session.py"
    ],
    base = "@python//image",
    main = "src/payload_populator/bq_populator.py",
    visibility = ["//visibility:public"],
    deps = [
        requirement("google-cloud-bigquery"),
        requirement("google-cloud-core"),
        "//common:common_lib",
    ],
)

# https://github.com/bazelbuild/rules_docker/tree/e15c9ebf203b7fa708e69ff5f1cdcf427d7edf6f#container_push
container_push(
    name = "push_custom",
    format = "Docker",
    image = ":custom",
    registry = "gcr.io",
    repository = "rental-ds/custom",
    tag = "$(BRANCH_NAME)",
)

I have 120+ dependencies that my code relies on inside

deps = [
        requirement("google-cloud-bigquery"),
        requirement("google-cloud-core"),
        "//common:common_lib",
    ],

I don't want to list out all of them independently to use them in the code. Is there a simple way to either import all of them in one go from requirement or a way to bypass my calling of requirement("library")?

I've tried to scour Bazel docs: https://docs.bazel.build/versions/main/be/python.html

and the github page for docker-rules: https://github.com/bazelbuild/rules_docker

If I'm missing some knowledge that's obvious, please link a reference for the read as well.

2

There are 2 best solutions below

0
On

It can be achieved with bazel.build/reference/be/functions#glob function. Have you tried. Seems old issue but wanted to see.

0
On

having BUILD.bazel file in each package makes for a convenient place to express unit-tests close to the code and then srcs becomes only local files and deps gets to be depending on your packages:

py_binary(
    name = "my_app",
    srcs = glob(["*.py", "src/my_app/*.py"]),
    deps = ["src/payload_populator", "src/custom"],
)

py_image(
    name = "app_image",
    deps = [":my_app"],
    cmd = ["./my-app"]
)