Created
October 23, 2025 01:35
-
-
Save webowodev/a87ef75ccb493bf09262751b33dc6b38 to your computer and use it in GitHub Desktop.
Generate .env file from Vault API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if [ -z "$TOKEN" ]; then | |
| echo "Token not provided!" | |
| exit 1 | |
| else | |
| if [ -z "$API_URL" ]; then | |
| echo "Missing the API url, I don't know where the env is :/" | |
| exit 2 | |
| else | |
| echo "Getting environment variable from $API_URL" | |
| if [ -z "$OUTPUT" ]; then | |
| # set default output name | |
| OUTPUT=.env | |
| fi | |
| # Execute curl and jq commands and store the result in a temporary file | |
| if ! curl --fail -X 'GET' \ | |
| "$API_URL" \ | |
| -H 'accept: */*' \ | |
| -H "X-Vault-Token: $TOKEN" > temp.json; then | |
| echo "Failed to fetch environment variables" | |
| rm -f temp.json | |
| exit 1 | |
| fi | |
| if ! jq -r '.data.data | to_entries[] | if .value | type == "string" and contains("\n") then "\(.key)=\"\(.value|tostring|gsub("\\n"; "\\n"))\"" else "\(.key)=\(.value)" end' temp.json > "$OUTPUT"; then | |
| echo "Failed to process environment variables" | |
| rm -f temp.json | |
| exit 1 | |
| fi | |
| rm -f temp.json | |
| fi | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prerequisite
Usage
API_URL=https://yourvaultapiurl.com/v1/{KV_PATH}/data/{YOUR_SECRET_PATH} TOKEN=hvs..... OUTPUT=.env sh generate-env.sh.env