Skip to content

Commit

Permalink
new post / docs/posts/2024/2024-09-11-generating-dotenv-file.md
Browse files Browse the repository at this point in the history
  • Loading branch information
copdips committed Sep 11, 2024
1 parent 390124b commit 1f3a841
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
51 changes: 51 additions & 0 deletions docs/posts/2024/2024-09-11-generating-dotenv-file.md
Original file line number Diff line number Diff line change
@@ -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
```
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ plugins:
- certificate
- cicd
- container
- database
- databricks
- datetime
- debug
Expand All @@ -111,6 +112,7 @@ plugins:
- network
- package
- pandas
- performance
- pip
- powershell
- proxy
Expand Down

0 comments on commit 1f3a841

Please sign in to comment.