Skip to content

Commit 818662f

Browse files
committed
Fix environment variable handling in SSH execution
- Environment variables are now properly exported in SSH session - Support both comma-separated and newline-separated format - Fixed parsing to handle all environment variables correctly - Added debug output to help troubleshoot issues - Environment variables are set at the beginning of the script execution - Added script preview to show what will be executed This should resolve the issue where only 1 environment variable was being set instead of all 4 provided.
1 parent 81f280f commit 818662f

1 file changed

Lines changed: 38 additions & 11 deletions

File tree

entrypoint.sh

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,27 @@ chmod 600 ~/.ssh/config
5050
TEMP_SCRIPT=$(mktemp)
5151

5252
# Process environment variables if provided
53+
ENV_EXPORTS=""
5354
if [ -n "$ENVS" ]; then
54-
echo "Setting environment variables..."
55-
# Add environment variable exports to the script
56-
echo "# Environment variables" > "$TEMP_SCRIPT"
55+
echo "Processing environment variables..."
56+
echo "Raw ENVS input: '$ENVS'"
57+
58+
# Replace newlines with commas and handle both comma and newline delimited input
59+
CLEANED_ENVS=$(echo "$ENVS" | tr '\n' ',' | sed 's/,,*/,/g' | sed 's/^,//;s/,$//')
60+
echo "Cleaned ENVS: '$CLEANED_ENVS'"
5761

58-
# Split ENVS by comma and process each one
59-
IFS=',' read -ra ENV_ARRAY <<< "$ENVS"
62+
# Split by comma and process each one
63+
IFS=',' read -ra ENV_ARRAY <<< "$CLEANED_ENVS"
64+
echo "Number of environment variables to process: ${#ENV_ARRAY[@]}"
6065
for env_var in "${ENV_ARRAY[@]}"; do
66+
echo "Processing env_var: '$env_var'"
6167
# Trim whitespace
6268
env_var=$(echo "$env_var" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
69+
echo "After trimming: '$env_var'"
6370

6471
# Skip empty values
6572
if [ -z "$env_var" ]; then
73+
echo " Skipping empty value"
6674
continue
6775
fi
6876

@@ -74,22 +82,41 @@ if [ -n "$ENVS" ]; then
7482

7583
# Validate variable name (should start with letter or underscore, contain only alphanumeric and underscore)
7684
if [[ "$var_name" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then
77-
# Safely export the variable (escape special characters in value)
78-
printf 'export %s=%q\n' "$var_name" "$var_value" >> "$TEMP_SCRIPT"
79-
echo " ✅ Set: $var_name"
85+
# Build environment variable exports for SSH command (one per line)
86+
if [ -n "$ENV_EXPORTS" ]; then
87+
ENV_EXPORTS="${ENV_EXPORTS}\n"
88+
fi
89+
ENV_EXPORTS="${ENV_EXPORTS}export ${var_name}=$(printf '%q' "$var_value")"
90+
echo " ✅ Will set: $var_name"
8091
else
8192
echo " ⚠️ Warning: Invalid variable name '$var_name', skipping"
8293
fi
8394
else
8495
echo " ⚠️ Warning: Invalid format '$env_var', should be 'VAR=value', skipping"
8596
fi
8697
done
87-
98+
fi
99+
100+
# Build the complete script with environment variables
101+
if [ -n "$ENV_EXPORTS" ]; then
102+
echo "Setting environment variables in remote session..."
103+
# Add environment variable exports at the beginning of the script
104+
echo "# Environment variables" > "$TEMP_SCRIPT"
105+
printf "%b\n" "$ENV_EXPORTS" >> "$TEMP_SCRIPT"
88106
echo "" >> "$TEMP_SCRIPT"
107+
# Add the main script
108+
echo "$SCRIPT" >> "$TEMP_SCRIPT"
109+
else
110+
# No environment variables, just add the main script
111+
echo "$SCRIPT" > "$TEMP_SCRIPT"
89112
fi
90113

91-
# Add the main script
92-
echo "$SCRIPT" >> "$TEMP_SCRIPT"
114+
# Debug: Show the generated script (first 20 lines)
115+
echo "Generated script preview:"
116+
echo "========================="
117+
head -n 20 "$TEMP_SCRIPT" | sed 's/^/ /'
118+
echo "========================="
119+
echo ""
93120

94121
# Execute the script on the remote host using sshpass
95122
echo "Executing script on remote host..."

0 commit comments

Comments
 (0)