Skip to content

Commit 2120f33

Browse files
An action to set the Terraform Envrionment variables to access Azure
1 parent db3ce33 commit 2120f33

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

fetch_kv_secret.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,35 @@ def fetch_kv_secret():
5353
# Set the secret value as an environment variable
5454
os.environ[key] = get_secret.value
5555

56+
return secrets
57+
58+
def set_github_env_variables(secrets):
59+
"""
60+
Sets GitHub environment variables using workflow commands.
61+
62+
This function takes a dictionary of secrets, sanitizes the keys to make them
63+
compatible with GitHub environment variable naming conventions, and writes
64+
them to the `GITHUB_ENV` file to be used as environment variables in a GitHub
65+
Actions workflow.
66+
67+
Args:
68+
secrets (dict): A dictionary where keys are secret names and values are
69+
the corresponding secret values.
70+
71+
Raises:
72+
FileNotFoundError: If the `GITHUB_ENV` file is not found.
73+
"""
74+
logger = logging.getLogger(__name__)
75+
github_env_file = os.getenv('GITHUB_ENV')
76+
with open(github_env_file, 'a') as f:
77+
for key, value in secrets.items():
78+
if value is not None:
79+
# Sanitize the key name to be compatible with GitHub env vars
80+
env_key = key.replace('-', '_').upper()
81+
# Use workflow command to set environment variable
82+
f.write(f"{env_key}={value}\n")
83+
logger.warning(f"Set environment variable: {env_key}")
84+
5685

5786
def main():
5887
"""
@@ -62,7 +91,8 @@ def main():
6291
setup_logging()
6392
logger = logging.getLogger(__name__)
6493
logger.info("Starting to fetch secrets from Azure Key Vault")
65-
fetch_kv_secret()
94+
secrets = fetch_kv_secret()
95+
set_github_env_variables(secrets)
6696
logger.info("Finished fetching secrets from Azure Key Vault")
6797

6898
if __name__ == "__main__":

0 commit comments

Comments
 (0)