Skip to content

Commit

Permalink
.bazelrc: support .user-bazelrc for optional artifacts
Browse files Browse the repository at this point in the history
Signed-off-by: Øyvind Harboe <[email protected]>
  • Loading branch information
oharboe committed Sep 15, 2024
1 parent 01e36cb commit 3df7087
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build --incompatible_strict_action_env
try-import %workspace%/.user-bazelrc
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
bazel-*
copyright.txt
build/
.user-bazelrc
46 changes: 46 additions & 0 deletions cred_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python3

import subprocess
import json
import re
import sys


def get_gcloud_auth_token():
with open(".user-bazelrc") as f:
all = f.read()
# The username is in the .user-bazelrc file as "# user: <username>"
# fish it out using a regex
USER = re.search(r"# user: (.*)", all).group(1)

# Run gcloud command to get the authentication token
result = subprocess.run(
["gcloud", "auth", "print-access-token", USER],
capture_output=True, text=True, check=True)
token = result.stdout.strip()
return token


def generate_credentials():
# Get the Bearer token from gcloud
bearer_token = get_gcloud_auth_token()

# Create the JSON object with the required format
credentials = {
"headers": {
"Authorization": [f"Bearer {bearer_token}"]
}
}
return credentials


def main():
if len(sys.argv) != 2 or sys.argv[1] != "get":
sys.exit("Usage: python credential_helper.py get")

credentials = generate_credentials()
print(json.dumps(credentials, indent=2))


if __name__ == "__main__":
main()

0 comments on commit 3df7087

Please sign in to comment.