Skip to content

Commit 67e1695

Browse files
committed
Create load-env.sh
1 parent 6490d29 commit 67e1695

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

load-env.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env bash
2+
3+
# Load variables from the .env file into the current shell so they're available to kamal and other commands.
4+
# NOTE: This script must be *sourced*, not executed, e.g.:
5+
# source load-env.sh
6+
# or
7+
# . ./load-env.sh
8+
9+
# Add the necessary variables used by ./config/deploy.yml to .env file, e.g:
10+
# - GITHUB_REPOSITORY=org/repo # Repo name used to derive image/service name and folder paths
11+
# - KAMAL_DEPLOY_IP=100.100.100.100 # IP address of server to deploy to
12+
# - KAMAL_DEPLOY_HOST=www.example.org # domain name of website
13+
# - KAMAL_REGISTRY_USERNAME=user # Container registry credentials (for ghcr.io)
14+
# - GITHUB_PACKAGES_TOKEN=ghp_xxx
15+
# Login with:
16+
# echo $KAMAL_REGISTRY_PASSWORD | docker login ghcr.io -u mythz --password-stdin
17+
18+
# If this script is run directly (./load-env.sh), print a warning because
19+
# environment changes will not persist in the parent shell.
20+
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
21+
echo "[load-env] This script must be sourced to affect the current shell:" >&2
22+
echo " source load-env.sh" >&2
23+
exit 1
24+
fi
25+
26+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
27+
ENV_FILE="$SCRIPT_DIR/.env"
28+
29+
if [[ ! -f "$ENV_FILE" ]]; then
30+
echo "[load-env] No .env file found at: $ENV_FILE" >&2
31+
return 1
32+
fi
33+
34+
# Auto-export all variables defined while the script runs
35+
set -a
36+
source "$ENV_FILE"
37+
set +a
38+
39+
echo "[load-env] Loaded environment variables from $ENV_FILE"

0 commit comments

Comments
 (0)