Skip to content

Instantly share code, notes, and snippets.

@boina-n
Created July 9, 2025 18:24
Show Gist options
  • Save boina-n/caa368693f8dd2f566a335f4ebf8819a to your computer and use it in GitHub Desktop.
Save boina-n/caa368693f8dd2f566a335f4ebf8819a to your computer and use it in GitHub Desktop.
docker_env.sh
#!/bin/bash
# ------------------------------------------------------------------------------
# docker_env.sh
#
# This script securely loads environment variables from Google Secret Manager,
# exports them to the local shell, and generates a .env file from a .env.dist
# template using envsubst.
#
# - Secrets are never printed to the console.
# - .env is created safely with only non-empty, non-commented variables.
# - File permissions on .env are restricted (chmod 600).
#
# Usage:
# ./docker_env.sh
#
# Requirements:
# - gcloud CLI authenticated with access to the secret
# - .env.dist file present in the current directory
# ------------------------------------------------------------------------------
set -e
export $(gcloud secrets versions access latest --secret=service-production |grep -v "^#\|^$" | xargs)
cat .env.dist | envsubst > .env
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment