Skip to content

Instantly share code, notes, and snippets.

@ericblue
Created June 12, 2024 20:14
Show Gist options
  • Save ericblue/cb223b9a3d8fb36a0d0a55612954c426 to your computer and use it in GitHub Desktop.
Save ericblue/cb223b9a3d8fb36a0d0a55612954c426 to your computer and use it in GitHub Desktop.
Bash script to bulk update environment variables for a given Heroku app
#!/bin/bash
# This script is used to set environment variables for a Heroku application.
# It reads variables from a provided .env file and sets them for the specified Heroku application.
# The script ignores HOST and PORT variables.
# Usage: ./set_heroku_env.sh path_to_env_file heroku_app_name
# Check if the .env file and app name are provided as arguments
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage: $0 path_to_env_file heroku_app_name"
exit 1
fi
ENV_FILE=$1
HEROKU_APP=$2
# Check if the provided file exists
if [ ! -f "$ENV_FILE" ]; then
echo "Error: File '$ENV_FILE' not found!"
exit 1
fi
# Initialize a variable to store all config variables
CONFIG_VARS=""
# Read the .env file and collect variables except for HOST and PORT
while IFS= read -r line; do
if [[ $line != \#* && $line == *"="* ]]; then
var_name=$(echo $line | cut -d '=' -f 1)
var_value=$(echo $line | cut -d '=' -f 2-)
# Remove quotes from the value if present
var_value=$(echo $var_value | sed 's/^"\(.*\)"$/\1/')
if [[ "$var_name" != "HOST" && "$var_name" != "PORT" ]]; then
CONFIG_VARS+="$var_name=$var_value "
fi
fi
done < "$ENV_FILE"
# Set all the collected variables at once
heroku config:set $CONFIG_VARS --app "$HEROKU_APP"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment