Skip to content

Commit

Permalink
adding action
Browse files Browse the repository at this point in the history
  • Loading branch information
LynxLynxx committed Sep 30, 2024
1 parent 795c927 commit 54a4307
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 6 deletions.
62 changes: 62 additions & 0 deletions .github/workflows/firebase_hosting_action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Build and Deploy Flutter Web to Firebase Hosting

on:
push:
branches: [analytics_view]
# tags:
# - 'v*.*.*'
pull_request_target:
branches: [analytics_view]

jobs:
build-and-deploy:
runs-on: ubuntu-latest

steps:
# Checkout the code
- uses: actions/checkout@v2

# Set up Java (required by Flutter for Android builds)
- uses: actions/setup-java@v1
with:
java-version: "12.x"

# Set up Flutter using subosito/flutter-action
- uses: subosito/flutter-action@v2
with:
channel: stable

# Verify Flutter setup
- run: flutter --version

# Install Flutter dependencies
- run: flutter pub get

# Build Flutter assets
- run: flutter pub run build_runner build

# Step: Set up Firebase secrets in localStorage
- name: Set up Firebase secrets in localStorage
run: |
echo "window.localStorage.setItem('FIREBASE_API_KEY', '${{ secrets.APIKEY }}');" >> ./web/firebase_config.js
echo "window.localStorage.setItem('FIREBASE_AUTH_DOMAIN', '${{ secrets.AUTHDOMAIN }}');" >> ./web/firebase_config.js
echo "window.localStorage.setItem('FIREBASE_PROJECT_ID', '${{ secrets.PROJECTID }}');" >> ./web/firebase_config.js
echo "window.localStorage.setItem('FIREBASE_STORAGE_BUCKET', '${{ secrets.STORAGEBUCKET }}');" >> ./web/firebase_config.js
echo "window.localStorage.setItem('FIREBASE_MESSAGING_SENDER_ID', '${{ secrets.MESSAGINGSENDERID }}');" >> ./web/firebase_config.js
echo "window.localStorage.setItem('FIREBASE_APP_ID', '${{ secrets.APPID }}');" >> ./web/firebase_config.js
echo "window.localStorage.setItem('FIREBASE_MEASUREMENT_ID', '${{ secrets.MEASUREMENTID }}');" >> ./web/firebase_config.js
# Build Flutter Web
- run: flutter build web --release

# Firebase Hosting deploy
- name: Deploy to Firebase Hosting
uses: FirebaseExtended/action-hosting-deploy@v0
with:
repoToken: "${{ secrets.GITHUB_TOKEN }}"
firebaseServiceAccount: "${{ secrets.FIREBASE_SERVICE_ACCOUNT_CARLOG_WEBSITE }}"
projectId: "${{ secrets.PROJECTID }}"
channelId: live
preview: false
env:
FIREBASE_CLI_EXPERIMENTS: webframeworks
23 changes: 20 additions & 3 deletions lib/main_web.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:js' as js;
import 'dart:ui';

import 'package:carlog/core/addons/bloc_observer.dart';
Expand All @@ -24,9 +25,25 @@ import 'generated/l10n.dart';

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
final firebaseConfig = js.context['firebaseConfig'];
if (firebaseConfig == null) {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
} else {
await Firebase.initializeApp(
options: FirebaseOptions(
apiKey: firebaseConfig['FIREBASE_API_KEY'],
authDomain: firebaseConfig['FIREBASE_AUTH_DOMAIN'],
projectId: firebaseConfig['FIREBASE_PROJECT_ID'],
storageBucket: firebaseConfig['FIREBASE_STORAGE_BUCKET'],
messagingSenderId: firebaseConfig['FIREBASE_MESSAGING_SENDER_ID'],
appId: firebaseConfig['FIREBASE_APP_ID'],
measurementId: firebaseConfig['FIREBASE_MEASUREMENT_ID'],
),
);
}

HydratedBloc.storage = await HydratedStorage.build(
storageDirectory: kIsWeb
? HydratedStorage.webStorageDirectory
Expand Down
10 changes: 7 additions & 3 deletions web/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<!DOCTYPE html>
<html>

<head>
<!--
If you are serving your web app in a path other than the root, change the
Expand Down Expand Up @@ -27,12 +28,15 @@
<link rel="apple-touch-icon" href="icons/Icon-192.png">

<!-- Favicon -->
<link rel="icon" type="image/png" href="favicon.png"/>
<link rel="icon" type="image/png" href="favicon.png" />

<title>carlog</title>
<title>Carlog</title>
<link rel="manifest" href="manifest.json">
<script src="firebase_config.js"></script>
</head>

<body>
<script src="flutter_bootstrap.js" async></script>
</body>
</html>

</html>

0 comments on commit 54a4307

Please sign in to comment.