From 1f3a841f765894b8060a65e23b84f9e6d9d5ab89 Mon Sep 17 00:00:00 2001 From: Xiang ZHU Date: Wed, 11 Sep 2024 23:52:22 +0200 Subject: [PATCH] new post / docs/posts/2024/2024-09-11-generating-dotenv-file.md --- .../2024/2024-09-11-generating-dotenv-file.md | 51 +++++++++++++++++++ mkdocs.yml | 2 + 2 files changed, 53 insertions(+) create mode 100644 docs/posts/2024/2024-09-11-generating-dotenv-file.md diff --git a/docs/posts/2024/2024-09-11-generating-dotenv-file.md b/docs/posts/2024/2024-09-11-generating-dotenv-file.md new file mode 100644 index 00000000..5ee64b0e --- /dev/null +++ b/docs/posts/2024/2024-09-11-generating-dotenv-file.md @@ -0,0 +1,51 @@ +--- +authors: +- copdips +categories: +- python +comments: true +date: + created: 2024-09-11 +--- + +# Generating .env file + +During local testing, we often need to set environment variables. One way to do this is to create a `.env` file in the root directory of the project. +This file contains key-value pairs of environment variables. For example, a `.env` file might look like this: + +```plaintext +ENV=dev +SECRET=xxx +``` + +Hereunder a quick bash script to generate a `.env` file from a list of Azure KeyVault secrets, same logic can be applied to other secret managers. + +```bash +#!/bin/bash +set -e + +KEY_VAULT_NAME="azure_keyvault_name" + +secrets=( + "secret_name_1" + "secret_name_2" + "secret_name_3" +) + +az login + +# try to get the first secret to check if the user is authenticated before overwriting the .env file +first_serect=$(az keyvault secret show --vault-name "$KEY_VAULT_NAME" --name "${secrets[0]}") + +> .env +if ! grep -q "^\.env$" .gitignore; then + echo ".env" >> .gitignore + echo ".env added to .gitignore" +fi + +for secret in "${secrets[@]}"; do + value=$(az keyvault secret show --vault-name "$KEY_VAULT_NAME" --name "$secret" | jq .value -r) + secret_upper=$(echo "$secret" | tr '[:lower:]-' '[:upper:]_') + echo "${secret_upper}=${value}" >> .env +done +``` diff --git a/mkdocs.yml b/mkdocs.yml index 4347183a..f44f8ffb 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -87,6 +87,7 @@ plugins: - certificate - cicd - container + - database - databricks - datetime - debug @@ -111,6 +112,7 @@ plugins: - network - package - pandas + - performance - pip - powershell - proxy