acqio / rules_microsoft_azure

This repository contains rules for interacting with Microsoft Azure.
Apache License 2.0
6 stars 3 forks source link

Include vendored `az` binary in rules #28

Open codersasha opened 1 year ago

codersasha commented 1 year ago

I figured out a way to include the az binary in Bazel, as a python target. If I get time I'll write up a PR to add it to this repo -- otherwise, here's all the Bazel code you need to run bazel run azur-cli :)

WORKSPACE:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "rules_python",
    sha256 = "8c8fe44ef0a9afc256d1e75ad5f448bb59b81aba149b8958f02f7b3a98f5d9b4",
    strip_prefix = "rules_python-0.13.0",
    url = "https://github.com/bazelbuild/rules_python/archive/refs/tags/0.13.0.tar.gz",
)

load("@rules_python//python:repositories.bzl", "python_register_toolchains")

python_register_toolchains(
    name = "python3_9",
    python_version = "3.9",
)

load("@python3_9//:defs.bzl", "interpreter")
load("@rules_python//python:pip.bzl", "pip_parse")

pip_parse(
    name = "pip",
    python_interpreter_target = interpreter,
    requirements_lock = "//:requirements_lock.txt",
)

load("@pip//:requirements.bzl", "install_deps")

install_deps()

BUILD.bazel:

load("@rules_python//python:defs.bzl", "py_binary")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")

compile_pip_requirements(
    name = "requirements",
    extra_args = ["--allow-unsafe"],
    requirements_in = "requirements.in",
    requirements_txt = "requirements_lock.txt",
)

py_binary(
    name = "azure-cli",
    srcs = ["@pip_azure_cli//:site-packages/azure/cli/__main__.py"],
    main = "@pip_azure_cli//:site-packages/azure/cli/__main__.py",
    deps = ["@pip_azure_cli//:pkg"],
)

requirements.in:

azure-cli==2.40.0

requirements_lock.txt:

<starts out blank, run bazel run requirements.update to generate>

You could also probably tweak it to move requirements.in to a write_file command, and then generate requirements.txt from a genrule.

payparain commented 1 year ago

This is amazing! Now we don't need to have the Azure CLI pre-installed to use these rules. Incredible!!