generate unique env file for each deployment to prevent conflicts

This commit is contained in:
evanpelle
2025-05-29 17:14:49 -07:00
parent c2063cd73b
commit 42f94659f6
2 changed files with 30 additions and 8 deletions
+7 -3
View File
@@ -171,8 +171,12 @@ if [ $? -ne 0 ]; then
exit 1
fi
# Generate a random filename for the environment file to prevent conflicts
# when multiple deployments are happening at the same time.
ENV_FILE="${REMOTE_UPDATE_PATH}/${SUBDOMAIN}-${RANDOM}.env"
ssh -i $SSH_KEY $REMOTE_USER@$SERVER_HOST "chmod +x $REMOTE_UPDATE_SCRIPT && \
cat > $REMOTE_UPDATE_PATH/.env << 'EOL'
cat > $ENV_FILE << 'EOL'
GAME_ENV=$ENV
ENV=$ENV
HOST=$HOST
@@ -192,8 +196,8 @@ OTEL_ENDPOINT=$OTEL_ENDPOINT
BASIC_AUTH_USER=$BASIC_AUTH_USER
BASIC_AUTH_PASS=$BASIC_AUTH_PASS
EOL
chmod 600 $REMOTE_UPDATE_PATH/.env && \
$REMOTE_UPDATE_SCRIPT"
chmod 600 $ENV_FILE && \
$REMOTE_UPDATE_SCRIPT $ENV_FILE"
if [ $? -ne 0 ]; then
echo "❌ Failed to execute update script on server."
+23 -5
View File
@@ -2,12 +2,25 @@
# update.sh - Script to update Docker container on Hetzner server
# Called by deploy.sh after uploading Docker image to Docker Hub
# Load environment variables if .env exists
if [ -f /home/openfront/.env ]; then
echo "Loading environment variables from .env file..."
export $(grep -v '^#' /home/openfront/.env | xargs)
# Check if environment file is provided
if [ $# -ne 1 ]; then
echo "Error: Environment file path is required"
echo "Usage: $0 <env_file_path>"
exit 1
fi
ENV_FILE="$1"
# Check if environment file exists
if [ ! -f "$ENV_FILE" ]; then
echo "Error: Environment file '$ENV_FILE' not found"
exit 1
fi
# Load environment variables from the provided file
echo "Loading environment variables from $ENV_FILE..."
export $(grep -v '^#' "$ENV_FILE" | xargs)
echo "======================================================"
echo "🔄 UPDATING SERVER: ${HOST} ENVIRONMENT"
echo "======================================================"
@@ -47,7 +60,7 @@ fi
echo "Starting new container for ${HOST} environment..."
docker run -d \
--restart="${RESTART}" \
--env-file /home/openfront/.env \
--env-file "$ENV_FILE" \
--name "${CONTAINER_NAME}" \
"${DOCKER_IMAGE}"
@@ -60,6 +73,11 @@ if [ $? -eq 0 ]; then
docker image prune -a -f
docker container prune -f
echo "Cleanup complete."
# Remove the environment file
echo "Removing environment file ${ENV_FILE}..."
rm -f "$ENV_FILE"
echo "Environment file removed."
else
echo "Failed to start container"
exit 1