environment-variablesbazelstarlark

Bazel - How to Read a String from An Environment File?


I have several k8s_object rules in my project and I supply each of them with the same cluster name like this:

k8s_object(
  name = "k8s_service",
  kind = "service",
  cluster = "gke_cents-ideas_europe-west3-b_cents-ideas",
  template = ":gateway.service.yaml",
)

So whenever I want to change the cluster name, I have to change it in many different places.

Goal

I would prefer to set the cluster name in a .env file like this:

KUBERNETES_CLUSTER=my-cluster-name

and let Bazel automatically pick up this value.


Solution

  • https://docs.bazel.build/versions/2.0.0/skylark/tutorial-sharing-variables.html

    Create a .bzl file in which you can declare a variable. Import this bzl file in all BUILD files referencing the variable.