-
-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathbuild_all.sh
More file actions
executable file
·437 lines (370 loc) · 14.8 KB
/
build_all.sh
File metadata and controls
executable file
·437 lines (370 loc) · 14.8 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/bin/zsh
# Exit on error
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_colored() {
local color=$1
local message=$2
echo -e "${color}${message}${NC}"
}
# Function to show usage
show_usage() {
echo "Usage: $(basename "$0") [OPTIONS]"
echo ""
echo "Options:"
echo " --android-only Build and deploy Android only"
echo " --ios-only Build and deploy iOS only"
echo " --help Show this help message"
echo ""
echo "Without any flags, builds both Android and iOS"
echo ""
exit 0
}
# Parse command line arguments
BUILD_ANDROID=true
BUILD_IOS=true
while [[ $# -gt 0 ]]; do
case $1 in
--android-only)
BUILD_ANDROID=true
BUILD_IOS=false
shift
;;
--ios-only)
BUILD_ANDROID=false
BUILD_IOS=true
shift
;;
--help)
show_usage
;;
*)
print_colored $RED "❌ Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac
done
print_colored $GREEN "🚀 Medito Build & Deploy Script"
print_colored $GREEN "==============================="
if [ "$BUILD_ANDROID" = true ] && [ "$BUILD_IOS" = true ]; then
print_colored $BLUE "This script will build both Android and iOS apps"
elif [ "$BUILD_ANDROID" = true ]; then
print_colored $BLUE "This script will build Android app only"
elif [ "$BUILD_IOS" = true ]; then
print_colored $BLUE "This script will build iOS app only"
fi
print_colored $BLUE "You'll choose the paywall environment below"
echo ""
# Get the current version from pubspec.yaml
CURRENT_VERSION=$(grep '^version:' pubspec.yaml | awk '{print $2}')
# Get the version and relative date from the last commit where pubspec.yaml was changed
LAST_COMMITTED_VERSION=$(git show HEAD:pubspec.yaml | grep '^version:' | awk '{print $2}')
LAST_COMMIT_DATE_AGO=$(git log -1 --pretty=format:%ar -- pubspec.yaml)
print_colored $BLUE "Current version: $CURRENT_VERSION"
print_colored $BLUE "Last committed version for pubspec.yaml: $LAST_COMMITTED_VERSION (committed $LAST_COMMIT_DATE_AGO)"
# Check if the version has been updated
if [ "$CURRENT_VERSION" = "$LAST_COMMITTED_VERSION" ]; then
print_colored $YELLOW "⚠️ The version in pubspec.yaml ($CURRENT_VERSION) has not been updated since the last commit ($LAST_COMMIT_DATE_AGO)."
echo -n "Do you want to continue building with this version? (y/N): "
read confirm
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
print_colored $RED "🛑 Build cancelled. Please update the version in pubspec.yaml and commit the change."
exit 1
fi
fi
# Paywall environment selection
print_colored $BLUE "🎯 Paywall Environment Selection"
print_colored $BLUE "================================"
echo "1) Live paywalls (for production users)"
echo "2) Dev paywalls (for testing)"
echo ""
echo -n "Select paywall environment (1-2, default: 1): "
read paywall_choice
case $paywall_choice in
2)
PAYWALL_ENV="dev"
print_colored $YELLOW "🧪 Selected: Dev paywalls"
print_colored $YELLOW "⚠️ WARNING: You are building with DEV paywalls!"
print_colored $YELLOW " Make sure this is for testing only, not production deployment."
echo ""
print_colored $YELLOW "Are you sure you want to continue with DEV paywalls? (y/N): "
read dev_confirm
if [[ ! "$dev_confirm" =~ ^[Yy]$ ]]; then
print_colored $RED "❌ Build cancelled."
exit 1
fi
;;
*)
PAYWALL_ENV="live"
print_colored $GREEN "✅ Selected: Live paywalls"
;;
esac
# Play Console upload selection (before building)
UPLOAD_TO_PLAY_STORE=false
PLAY_STORE_TRACK=""
if [ "$BUILD_ANDROID" = true ]; then
echo ""
print_colored $BLUE "📤 Play Console Upload Selection"
print_colored $BLUE "=================================="
echo -n "Do you want to upload the APK to Google Play Console after building? (Y/n): "
read upload_choice
if [[ ! "$upload_choice" =~ ^[Nn]$ ]]; then
UPLOAD_TO_PLAY_STORE=true
# Determine track based on paywall environment
if [ "$PAYWALL_ENV" = "dev" ]; then
PLAY_STORE_TRACK="internal"
print_colored $YELLOW "Will upload to 'internal' track (dev paywalls)"
else
echo ""
print_colored $BLUE "Select Play Console track:"
echo "1) internal (for internal testing)"
echo "2) beta (for beta testing)"
echo "3) production (for production release)"
echo ""
echo -n "Select track (1-3, default: 3): "
read track_choice
case $track_choice in
1)
PLAY_STORE_TRACK="internal"
;;
2)
PLAY_STORE_TRACK="beta"
;;
*)
PLAY_STORE_TRACK="production"
;;
esac
fi
fi
fi
# Collect iOS credentials upfront so they don't block the build pipeline
APPLE_ID=""
APP_PASSWORD=""
if [ "$BUILD_IOS" = true ]; then
echo ""
print_colored $BLUE "🍎 iOS Credentials"
print_colored $BLUE "==================="
if [ -f "ios_credentials.sh" ]; then
source ios_credentials.sh
else
print_colored $YELLOW "⚠️ 'ios_credentials.sh' not found. You may be prompted for credentials."
fi
if [ -z "$APPLE_ID_ENV" ] || [ -z "$APP_SPECIFIC_PASSWORD_ENV" ]; then
print_colored $YELLOW "Environment variables APPLE_ID_ENV or APP_SPECIFIC_PASSWORD_ENV not set. Falling back to manual input."
echo "Enter your Apple ID: "
read APPLE_ID
echo "Enter your app-specific password (https://appleid.apple.com/account/manage): "
read -s APP_PASSWORD
echo ""
else
print_colored $GREEN "Using credentials from environment variables."
APPLE_ID="$APPLE_ID_ENV"
APP_PASSWORD="$APP_SPECIFIC_PASSWORD_ENV"
fi
fi
echo ""
echo "🧪 Running Flutter tests..."
flutter test
# Generate common variables
DATED_APKS_DIR="dated_apks"
DATE_STAMP=$(date +"%d%b%Y")
ANDROID_LOG="/tmp/medito_android_postbuild_$$.log"
ANDROID_BG_PID=""
ANDROID_BG_FAILED=false
# --- Android post-build function (APK copy + Play Store upload) ---
android_post_build() {
mkdir -p "$DATED_APKS_DIR"
SOURCE_APK="build/app/outputs/apk/prod/release/app-prod-release.apk"
DEST_APK="$DATED_APKS_DIR/medito-$CURRENT_VERSION-$PAYWALL_ENV-paywall-$DATE_STAMP.apk"
cp "$SOURCE_APK" "$DEST_APK"
print_colored $GREEN "✅ APK copied to $DEST_APK"
if [ "$PAYWALL_ENV" = "dev" ]; then
print_colored $YELLOW "⚠️ This build has DEV paywalls - do NOT deploy to production!"
else
print_colored $GREEN "✅ This build has LIVE paywalls - safe for production deployment"
fi
if [ "$UPLOAD_TO_PLAY_STORE" = true ] && [ -n "$PLAY_STORE_TRACK" ]; then
echo ""
print_colored $BLUE "📤 Uploading APK to Play Console ($PLAY_STORE_TRACK track)..."
PLAY_API_KEY=""
if [ -f "../play-store-credentials.json" ]; then
PLAY_API_KEY="../play-store-credentials.json"
elif [ -f "android/fastlane/keys/google-play-api-key.json" ]; then
PLAY_API_KEY="android/fastlane/keys/google-play-api-key.json"
fi
if [ -z "$PLAY_API_KEY" ]; then
print_colored $RED "❌ Google Play API key not found"
print_colored $YELLOW "Checked locations:"
print_colored $YELLOW " - ../play-store-credentials.json"
print_colored $YELLOW " - android/fastlane/keys/google-play-api-key.json"
print_colored $YELLOW "Please ensure the API key file exists before uploading."
return 1
else
print_colored $GREEN "✅ Found Google Play credentials at: $PLAY_API_KEY"
ABS_APK_PATH="$(pwd)/$SOURCE_APK"
if [[ "$PLAY_API_KEY" == /* ]]; then
ABS_CREDENTIALS_PATH="$PLAY_API_KEY"
elif [[ "$PLAY_API_KEY" == ../* ]]; then
ABS_CREDENTIALS_PATH="$(cd .. && pwd)/$(basename "$PLAY_API_KEY")"
else
ABS_CREDENTIALS_PATH="$(pwd)/$PLAY_API_KEY"
fi
cd android
if command -v bundle &> /dev/null; then
if [ -f "Gemfile" ]; then
print_colored $BLUE "📦 Installing Fastlane dependencies..."
bundle install
fi
bundle exec fastlane android upload_apk apk_path:"$ABS_APK_PATH" track:"$PLAY_STORE_TRACK" json_key:"$ABS_CREDENTIALS_PATH"
else
print_colored $YELLOW "⚠️ bundle not found, trying fastlane directly..."
fastlane android upload_apk apk_path:"$ABS_APK_PATH" track:"$PLAY_STORE_TRACK" json_key:"$ABS_CREDENTIALS_PATH"
fi
UPLOAD_RESULT=$?
cd ..
if [ $UPLOAD_RESULT -eq 0 ]; then
print_colored $GREEN "✅ APK uploaded to Play Console successfully!"
print_colored $BLUE "Track: $PLAY_STORE_TRACK"
else
print_colored $RED "❌ Failed to upload APK to Play Console"
print_colored $YELLOW "APK is still available at: $DEST_APK"
return 1
fi
fi
fi
}
# --- Build pipeline ---
if [ "$BUILD_ANDROID" = true ]; then
print_colored $BLUE "🚀 Starting Android build..."
print_colored $BLUE "Paywall environment: $PAYWALL_ENV"
flutter build apk --flavor prod --release --dart-define-from-file=../.prod.json --dart-define=PAYWALL_ENV=$PAYWALL_ENV
if [ "$BUILD_IOS" = true ]; then
# Run Android post-build in background while iOS builds
print_colored $BLUE "📦 Android post-processing running in background while iOS builds..."
android_post_build > "$ANDROID_LOG" 2>&1 &
ANDROID_BG_PID=$!
else
android_post_build
echo ""
print_colored $BLUE "✅ Android build completed! Opening dated APKs directory..."
open "$DATED_APKS_DIR"
fi
else
print_colored $YELLOW "⏭️ Skipping Android build (--ios-only flag used)"
fi
if [ "$BUILD_IOS" = true ]; then
print_colored $BLUE "🚀 Starting iOS build and upload..."
print_colored $BLUE "Paywall environment: $PAYWALL_ENV"
flutter build ios --dart-define-from-file=../.prod.json --dart-define=ENABLE_DEVICE_PREVIEW=false --dart-define=PAYWALL_ENV=$PAYWALL_ENV --release
ARCHIVE_PATH="build/ios/archive/Runner.xcarchive"
EXPORT_PATH="build/ios/ipa"
EXPORT_OPTIONS_PLIST="ios/ExportOptions.plist"
rm -rf "$ARCHIVE_PATH" "$EXPORT_PATH"
xcodebuild -workspace ios/Runner.xcworkspace \
-scheme Runner \
-configuration Release \
-archivePath "$ARCHIVE_PATH" \
archive
if [ -f "ios/upload_dsyms.sh" ]; then
print_colored $BLUE "📤 Uploading dSYMs to Firebase Crashlytics..."
ios/upload_dsyms.sh "$ARCHIVE_PATH"
else
print_colored $YELLOW "⚠️ upload_dsyms.sh not found. Skipping dSYM upload."
fi
xcodebuild -exportArchive \
-archivePath "$ARCHIVE_PATH" \
-exportPath "$EXPORT_PATH" \
-exportOptionsPlist "$EXPORT_OPTIONS_PLIST"
IPA_PATH="$EXPORT_PATH/Runner.ipa"
if [ -f "$IPA_PATH" ]; then
NEW_IPA_PATH="$EXPORT_PATH/medito-${CURRENT_VERSION}-${PAYWALL_ENV}-paywall-${DATE_STAMP}.ipa"
cp "$IPA_PATH" "$NEW_IPA_PATH"
print_colored $GREEN "✅ iOS IPA built: $NEW_IPA_PATH"
if [ "$PAYWALL_ENV" = "dev" ]; then
print_colored $YELLOW "⚠️ This build has DEV paywalls - do NOT deploy to production!"
else
print_colored $GREEN "✅ This build has LIVE paywalls - safe for production deployment"
fi
else
print_colored $RED "❌ Failed to build iOS IPA"
exit 1
fi
print_colored $BLUE "📤 Uploading to TestFlight..."
xcrun altool --upload-app -f "$IPA_PATH" -t ios -u "$APPLE_ID" -p "$APP_PASSWORD"
if [ $? -eq 0 ]; then
print_colored $GREEN "✅ iOS app uploaded to TestFlight successfully"
else
print_colored $RED "❌ iOS upload failed"
exit 1
fi
if [ -n "$APPLE_ID_ENV" ]; then
unset APPLE_ID_ENV
fi
if [ -n "$APP_SPECIFIC_PASSWORD_ENV" ]; then
unset APP_SPECIFIC_PASSWORD_ENV
fi
else
print_colored $YELLOW "⏭️ Skipping iOS build (--android-only flag used)"
fi
# Wait for Android background job if it was started
if [ -n "$ANDROID_BG_PID" ]; then
echo ""
print_colored $BLUE "⏳ Waiting for Android post-processing to finish..."
set +e
wait $ANDROID_BG_PID
ANDROID_BG_RESULT=$?
set -e
echo ""
print_colored $BLUE "📋 Android post-processing output:"
print_colored $BLUE "──────────────────────────────────"
cat "$ANDROID_LOG"
rm -f "$ANDROID_LOG"
print_colored $BLUE "──────────────────────────────────"
if [ $ANDROID_BG_RESULT -ne 0 ]; then
ANDROID_BG_FAILED=true
print_colored $RED "❌ Android post-processing encountered errors (see above)"
else
print_colored $GREEN "✅ Android post-processing completed successfully"
fi
echo ""
print_colored $BLUE "✅ Android build completed! Opening dated APKs directory..."
open "$DATED_APKS_DIR"
fi
# Final summary
echo ""
print_colored $GREEN "🎉 Build and Deploy Complete!"
print_colored $GREEN "============================="
print_colored $BLUE "Version: $CURRENT_VERSION"
print_colored $BLUE "Paywall Environment: $PAYWALL_ENV"
if [ "$BUILD_ANDROID" = true ] && [ "$BUILD_IOS" = true ]; then
print_colored $BLUE "Platforms: Android & iOS"
elif [ "$BUILD_ANDROID" = true ]; then
print_colored $BLUE "Platforms: Android only"
elif [ "$BUILD_IOS" = true ]; then
print_colored $BLUE "Platforms: iOS only"
fi
echo ""
if [ "$PAYWALL_ENV" = "dev" ]; then
print_colored $YELLOW "⚠️ REMINDER: This build has DEV paywalls"
print_colored $YELLOW " Safe for: TestFlight, Play Store Internal Testing"
print_colored $YELLOW " NOT safe for: Production deployment"
else
print_colored $GREEN "✅ This build has LIVE paywalls"
print_colored $GREEN " Safe for: Production deployment"
fi
if [ "$ANDROID_BG_FAILED" = true ]; then
echo ""
print_colored $RED "⚠️ Android post-processing had errors — check output above"
fi
echo ""
if [ "$BUILD_ANDROID" = true ] || [ "$BUILD_IOS" = true ]; then
print_colored $BLUE "📤 Opening Telegram for easy sharing..."
open -a "Telegram"
fi