Bazel: Use environment variables in .bazelrc via string interpolation

Created on 5 Mar 2020  路  4Comments  路  Source: bazelbuild/bazel

Description of the problem / feature request:

I want to use environment variables in my .bazelrc via string interpolation

Feature requests: what underlying problem are you trying to solve with this feature?

I'm running a remote Bazel cache secured with Basic Auth. I don't want to hardcode basic auth credentials into the .bazelrc checked into the repository.

Example .bazelrc:

build --remote_cache=https://username:[email protected]

I would rather do something like this:

build --remote_cache=https://${CACHE_USERNAME}:${CACHE_PASSWORD}@mybazelcache.example.com

What operating system are you running Bazel on?

macOS Catalina 10.15.3

What's the output of bazel info release?

INFO: Invocation ID: 067b442a-efd2-431e-b39d-9ea6e2467bf3
release 2.0.0

Have you found anything relevant by searching the web?

Related issue comment: https://github.com/bazelbuild/bazel/issues/4635#issuecomment-365998683

P2 team-XProduct feature request

Most helpful comment

The official docs provide a workaround for this use case via try-import in your .bazelrc: https://docs.bazel.build/versions/master/best-practices.html#bazelrc

All 4 comments

The official docs provide a workaround for this use case via try-import in your .bazelrc: https://docs.bazel.build/versions/master/best-practices.html#bazelrc

This workaround isn't _exactly_ covering the use-case (I have a similar need).

Using a try-import with a user.bazelrc requires that the user rcfile to contain the entirety of the option string. So in the situation where there is a small piece of user-specific information (i.e. username) that is combined with a big piece of information that should be source controlled (i.e. full url of remote cache), we are currently lacking a mechanism to combine those bits together.

(Unless I'm missing some mechanism to combine data from different lines of the rcfiles.)

@psigen Here was my workaround:

I needed this feature specifically for a CircleCI setup (where I set environment variables in "Project Settings"), so what worked for me was creating a script generate_bazelrc.sh:

#!/bin/bash

# Exit on error
set -e

echo "# Generated by generate_bazelrc.sh" > .bazelrc
echo "build --remote_cache=https://$BAZEL_CACHE_USER:[email protected]" >> .bazelrc

and then running ./generate_bazelrc.sh before the start of every build.

Thanks. My workaround is also running a script. But I'm using this for local development, so it's not as easy to make sure it's sourced when the environment is set up.

Was this page helpful?
0 / 5 - 0 ratings