-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare-deployment.sh.template
More file actions
328 lines (285 loc) · 11.5 KB
/
prepare-deployment.sh.template
File metadata and controls
328 lines (285 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/bin/bash
# =============================================================================
# Firebase Dynamic Deployment Preparation Script - TEMPLATE
# =============================================================================
#
# This template creates a clean deployment folder with only essential service files.
# Copy this file to your project and customize the configuration sections below.
#
# USAGE:
# 1. Copy this file to your firebase-scripts directory
# 2. Rename it to prepare-deployment.sh
# 3. Customize the configuration sections below
# 4. Make it executable: chmod +x prepare-deployment.sh
# 5. Run: ./prepare-deployment.sh prepare|deploy|clean
#
# =============================================================================
set -e
# =============================================================================
# CONFIGURATION SECTION - CUSTOMIZE THESE FOR YOUR PROJECT
# =============================================================================
# Project-specific paths
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
DEPLOY_DIR="/tmp/firebase-deployment-$$"
SERVICES_DIR="$PROJECT_ROOT/services" # Change this to your services directory
# Firebase project configuration
DEFAULT_PROJECT_ID="your-project-id" # Change this to your Firebase project ID
DEFAULT_REGION="europe-west1" # Change this to your preferred region
# =============================================================================
# SERVICES CONFIGURATION - CUSTOMIZE FOR YOUR PROJECT
# =============================================================================
# List your services in dependency order (services that other services depend on first)
SERVICES=(
# Example services - replace with your actual service names:
"shared-logging-service" # Shared services first
"shared-auth-service" # Shared services first
"user-service" # Your business services
"product-service" # Your business services
"order-service" # Your business services
# Add more services as needed...
)
# Files to include for each service
SERVICE_FILES=(
"index.js" # Main service file
"package.json" # Dependencies
"libs" # Service-specific libraries
"config" # Configuration files
# Add more file patterns as needed...
)
# =============================================================================
# EXCLUSION PATTERNS - CUSTOMIZE FOR YOUR PROJECT
# =============================================================================
# Files and directories to exclude globally
EXCLUDE_PATTERNS=(
"node_modules" # Dependencies (will be installed during deployment)
"package-lock.json" # Lock file (not needed in deployment)
"*.log" # Log files
"*.bak" # Backup files
"*.backup.*" # Backup files
"test" # Test directories
"tests" # Test directories
"scripts" # Build/deployment scripts
"obsolete" # Old/obsolete code
".vscode" # IDE files
".idea" # IDE files
"*.swp" # Vim swap files
"*.swo" # Vim swap files
"*~" # Backup files
".DS_Store" # macOS files
".DS_Store?" # macOS files
"._*" # macOS files
".Spotlight-V100" # macOS files
".Trashes" # macOS files
"ehthumbs.db" # Windows files
"Thumbs.db" # Windows files
".gcloudignore" # Google Cloud ignore
".firebaserc" # Firebase config
".env.local" # Local environment
".env.development" # Development environment
"*.local" # Local files
"add-*.js" # Utility scripts
"check-*.js" # Utility scripts
"debug-*.js" # Utility scripts
"trigger-*.js" # Utility scripts
"simple-*.js" # Utility scripts
"test-*.js" # Test scripts
"*.sh" # Shell scripts
# Add more exclusion patterns as needed...
)
# =============================================================================
# FIREBASE CONFIGURATION - CUSTOMIZE FOR YOUR PROJECT
# =============================================================================
# Firebase function configuration
FIREBASE_RUNTIME="nodejs18" # Change to your preferred runtime
FIREBASE_SOURCE="." # Source directory (usually ".")
# =============================================================================
# END CONFIGURATION SECTION
# =============================================================================
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Logging functions
log_info() { echo -e "${BLUE}ℹ️ $1${NC}"; }
log_success() { echo -e "${GREEN}✅ $1${NC}"; }
log_warning() { echo -e "${YELLOW}⚠️ $1${NC}"; }
log_error() { echo -e "${RED}❌ $1${NC}"; }
# =============================================================================
# CORE FUNCTIONS - USUALLY DON'T NEED TO CHANGE
# =============================================================================
cleanup() {
log_info "Cleaning up deployment directory..."
if [ -d "$DEPLOY_DIR" ]; then
rm -rf "$DEPLOY_DIR"
log_success "Cleaned up $DEPLOY_DIR"
fi
}
# Set up cleanup on script exit
trap cleanup EXIT
prepare_deployment() {
log_info "🚀 Preparing Firebase deployment directory..."
log_info "Source: $SERVICES_DIR"
log_info "Target: $DEPLOY_DIR"
# Create clean deployment directory
mkdir -p "$DEPLOY_DIR"
log_success "Created deployment directory: $DEPLOY_DIR"
# Copy shared libs first
if [ -d "$SERVICES_DIR/libs" ]; then
log_info "Copying shared libraries..."
cp -r "$SERVICES_DIR/libs" "$DEPLOY_DIR/"
log_success "Copied shared libraries"
fi
# Copy each service
local total_services=${#SERVICES[@]}
local current=0
for service in "${SERVICES[@]}"; do
((current++))
local service_path="$SERVICES_DIR/$service"
if [ -d "$service_path" ]; then
log_info "[$current/$total_services] Processing service: $service"
# Create service directory in deployment
mkdir -p "$DEPLOY_DIR/$service"
# Copy essential files
for file in "${SERVICE_FILES[@]}"; do
local source="$service_path/$file"
if [ -e "$source" ]; then
if [ -d "$source" ]; then
cp -r "$source" "$DEPLOY_DIR/$service/"
else
cp "$source" "$DEPLOY_DIR/$service/"
fi
fi
done
# Copy service-specific files (like index-pubsub.js if it exists)
for file in "$service_path"/*.js; do
if [ -f "$file" ]; then
cp "$file" "$DEPLOY_DIR/$service/"
fi
done
log_success "✅ Copied service: $service"
else
log_warning "⚠️ Service directory not found: $service"
fi
done
# Copy root package.json
if [ -f "$SERVICES_DIR/package.json" ]; then
cp "$SERVICES_DIR/package.json" "$DEPLOY_DIR/"
log_success "Copied root package.json"
fi
# Create clean firebase.json
cat > "$DEPLOY_DIR/firebase.json" << EOF
{
"functions": {
"source": "$FIREBASE_SOURCE",
"runtime": "$FIREBASE_RUNTIME"
}
}
EOF
log_success "Created clean firebase.json"
# Show deployment structure
log_info "📁 Deployment directory structure:"
tree "$DEPLOY_DIR" -I "node_modules" 2>/dev/null || find "$DEPLOY_DIR" -type f | head -20
# Show total size
local total_size=$(du -sh "$DEPLOY_DIR" | cut -f1)
log_success "📦 Total deployment size: $total_size"
# Show file count
local file_count=$(find "$DEPLOY_DIR" -type f | wc -l)
log_info "📄 Total files: $file_count"
}
deploy_to_firebase() {
local project_id="${1:-$DEFAULT_PROJECT_ID}"
local region="${2:-$DEFAULT_REGION}"
log_info "🚀 Deploying to Firebase project: $project_id"
log_info "Region: $region"
# Change to deployment directory
cd "$DEPLOY_DIR"
# Set Firebase project
firebase use "$project_id" --token "$(firebase login:ci --no-localhost)"
# Deploy functions
log_info "Deploying functions..."
if firebase deploy --only functions --project "$project_id"; then
log_success "🎉 Deployment successful!"
else
log_error "❌ Deployment failed!"
return 1
fi
}
show_usage() {
echo "Usage: $0 [OPTIONS] [COMMAND]"
echo ""
echo "Commands:"
echo " prepare Prepare deployment directory only"
echo " deploy Prepare and deploy to Firebase"
echo " clean Clean up deployment directory"
echo ""
echo "Options:"
echo " --project PROJECT_ID Firebase project ID (default: $DEFAULT_PROJECT_ID)"
echo " --region REGION Firebase region (default: $DEFAULT_REGION)"
echo " --help Show this help message"
echo ""
echo "Examples:"
echo " $0 prepare # Prepare deployment directory"
echo " $0 deploy # Deploy to $DEFAULT_PROJECT_ID"
echo " $0 deploy --project myproject # Deploy to custom project"
echo ""
echo "Configuration:"
echo " Edit this script to customize:"
echo " - Services list and order"
echo " - File inclusion/exclusion patterns"
echo " - Firebase project settings"
}
main() {
local command=""
local project_id="$DEFAULT_PROJECT_ID"
local region="$DEFAULT_REGION"
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
prepare|deploy|clean)
command="$1"
shift
;;
--project)
project_id="$2"
shift 2
;;
--region)
region="$2"
shift 2
;;
--help|-h)
show_usage
exit 0
;;
*)
log_error "Unknown option: $1"
show_usage
exit 1
;;
esac
done
case "$command" in
prepare)
prepare_deployment
log_success "Deployment directory ready at: $DEPLOY_DIR"
log_info "To deploy manually: cd $DEPLOY_DIR && firebase deploy --only functions"
;;
deploy)
prepare_deployment
deploy_to_firebase "$project_id" "$region"
;;
clean)
cleanup
;;
"")
log_error "No command specified"
show_usage
exit 1
;;
esac
}
# Run main function with all arguments
main "$@"