Search code examples
bazel

How can I read environment variables inside the git_repository function?


I am trying to download a non-public git repository using git_repository, so I would like to use something like

def load_my-repo_repo():
    git_repository(
        name = "my-repo",
        remote = "https://user:${PASSWORD}@git-server.com/my-repo.git",
    )

I have tried with bazel test --test_env=PASSWORD=${TOKEN}, and with ctx.os.environ but I have not been able to use the environment variable that contains the password.

How can I read an environment variable inside the git_repository function?


Solution

  • You don't really want to approach this this way. .netrc files are the way to pass authentication credentials for private git repos.

    Create a file with the following format in your home directory with permissions 0600:

    machine git-server.com
    login user
    password SECRET