@@ -53,6 +53,35 @@ def fetch_kv_secret():
53
53
# Set the secret value as an environment variable
54
54
os .environ [key ] = get_secret .value
55
55
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
+
56
85
57
86
def main ():
58
87
"""
@@ -62,7 +91,8 @@ def main():
62
91
setup_logging ()
63
92
logger = logging .getLogger (__name__ )
64
93
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 )
66
96
logger .info ("Finished fetching secrets from Azure Key Vault" )
67
97
68
98
if __name__ == "__main__" :
0 commit comments