Summary of the proposed change
Describe the process of deployment of the Metrics application to the Firebase Hosting and deploying the Cloud functions needed to create test data for the application.
Link to supporting documentation, GitHub tickets, etc.
- Getting started with Firestore
- Deploying to Firebase Hosting
- Getting started with Firebase Cloud Functions
What problem is this project solving?
The Metrics application instance accessible via a link.
Identify success metrics and measurable goals.
- Web application is deployed to Firebase Hosting and available for everyone via the link.
- The Cloud Functions required for creating test data are deployed.
Identify what's not in scope.
Deployment to other platforms is out of scope.
Explain and diagram the technical design
Metrics Web Source
-> Build
-> Deploy
-> Firebase Hosting
Metrics Web Cloud Functions
-> Deploy
-> Firebase Cloud Functions
Metrics Web Security Rules
-> Deploy
-> Firestore Security Rules
Metrics Web Indexes
-> Deploy
-> Firestore Indexes
Identify risks and edge cases
What will the proposed API look like?
Before you start, you should have the following installed:
- Go to Firebase Console, login if needed, and tap the
Add project
button. - Enter a project name and tap the
Continue
to proceed. - On the next page, select whether you want to use Firebase Analytics or not and click the
Continue
/Create project
to proceed. - If you choose to use Firebase Analytics in the previous step select a Firebase Analytics account and tap the
Create project
.
After a couple of seconds, your project will be ready, and you'll be redirected to the project console. Your next step will be to create a Firestore database:
- Go to the
Database
tab on the left bar and click theCreate database
button under theCloud Firestore
. - Select
Start in test mode
to create a database with no security rules for now and tapNext
. We will add security rules in the Configuring Firestore database section. - Select your database location and click
Done
.
After a while, your database will be created, and you will see the database console page.
When your Firestore database is up, you need to add a Web application to your Firebase project, to be able to connect your web application with the Firestore database:
- In the Firebase Console, open your project and tap on the setting gear icon near the
Project Overview
on top of the left panel and selectProject settings
. - Scroll down and find
There are no apps in your project
text, and tap on the</>
icon to add a new Web application to your Firebase project. - Enter the app nickname and make sure that
Also set up Firebase Hosting for this app
is checked. - Select from the dropdown menu, or create a new site for your application and tap the
Register app
to proceed. - Skip the
Configure Firebase SDK
step. We will return to Firebase SDK configuration a bit later in Firebase SDK configuration. - Tap on the
Next
button and follow instructions to install the Firebase CLI. - Skip the
Deploy to Firebase Hosting
and tap on theContinue to console
to finish configuring your Firebase Web application.
The deployment process described more detailed in the Building and deploying the application to the Firebase Hosting section.
Finally, your Firebase project configured and it's time to configure the Firebase SDK in your Flutter for the Web application.
By default, the Metrics Web Application is configured by using the auto-generated
Firebase configuration. It means that after you deploy the Metrics application to the Firebase, your app automatically pulls the Firebase configuration object from the Firebase project to which you've deployed.
If you are okay with the defaults, you can skip this configuration step.
If you want to configure a connection to the Firebase manually
, follow the next steps:
- Open the Firebase console, choose your project
and go to the project setting (tap on the setting gear icon near the
Project Overview
on top of the left panel and selectProject settings
). - Scroll down and find your Firebase Web Application.
- Go to
Firebase SDK snippet
of your application, selectConfig
and copy the generated code. - Go to the
web/index.html
file in the application directory and replace the following piece of code:
<script src="/__/firebase/init.js"></script>
with the:
// Your web app's Firebase configuration, copied one in the step 3
const firebaseConfig = {...};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
The full example of how the Firebase configuration in the index.html
looks like:
auto-generated
Firebase configuration
// Firebase SDKs
<script src="https://www.gstatic.com/firebasejs/7.5.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.5.0/firebase-analytics.js"></script>
...
<script src="/__/firebase/init.js"></script>
manual
configuration
<script src="https://www.gstatic.com/firebasejs/7.5.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.5.0/firebase-analytics.js"></script>
...
const firebaseConfig = {
apiKey: "AIzaSyCkM-7WEAb9GGCjKQNChi5MD2pqrcRanzo",
authDomain: "metrics-d9c67.firebaseapp.com",
databaseURL: "https://metrics-d9c67.firebaseio.com",
projectId: "metrics-d9c67",
storageBucket: "metrics-d9c67.appspot.com",
messagingSenderId: "650500796855",
appId: "1:650500796855:web:65a4615a28f3d88e8bb832",
measurementId: "G-3DB4JFLKHQ"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
Finally, you have a configured Flutter application that works with your Firebase instance. It's time to deploy your Flutter application to the Firebase Hosting!
The email and password sign-in option allows authorizing to the application using the email and password a user has been created with. About how to create a new user with email and password consider the Creating a new Firebase User section. Please note, that adding a new user requires the email and password sign-in option to be enabled!
To enable the email and password sign-in option, consider the following steps:
- Open the Firebase console and choose your project.
- Navigate to
Authentication
->Sign-in method
. - Press the
Email/Password
item in theSign-in providers
table. - Enable the
Email/Password
sign-in method using the toggle in the opened menu. - Press the
Save
button.
The email and password sign-in option is controlled by the remote Feature Config
stored in the Firestore. The isPasswordSignInOptionEnabled
configuration stands for the email and password auth form availability on the Login Page of the application. If the isPasswordSignInOptionEnabled
is false
then users are not allowed to log in using the email and password sign-in method and no appropriate authentication form appears on the UI. To know more about the Feature Config
consider the Feature Config design document.
To enable the email and password authentication form, consider the following steps:
- Open the Firebase console and choose your project.
- Navigate to
Cloud Firestore
->Data
. - Open the
feature_config
collection (or create one by pressing theStart collection
button). - Open the
feature_config
document (or create one by pressing theAdd document
button). - Find the
isPasswordSignInOptionEnabled
field (or create one by pressing theAdd field
button). - Ensure the
isPasswordSignInOptionEnabled
value istrue
. - Press the
Update
button to save the field value.
When both the email and password sign-in option and appropriate feature config is enabled, users can use their credentials to sign in to the application.
The Google sign-in option allows authorizing to the application using Google. To enable the Google sign-in option, consider the following steps:
- Open the Firebase console and choose your project.
- Navigate to
Authentication
->Sign-in method
. - Press the
Google
item in theSign-in providers
table. - Enable the
Email/Password
sign-in method using the toggle in the opened menu. - Expand
Web SDK Configuration
panel in the opened menu and copyWeb client ID
, and pressSave
to save configurations. - Open
metrics/web/web/metric_config.js
and replacethis.googleSignInClientId = "$GOOGLE_SIGN_IN_CLIENT_ID";
with your OAuth Client ID.
Once the Google sign-in option is enabled it should work out of the box on default hosting domain. If you would like to add additional domains you should populate the Authorized JavaScript origins
with URLs related to these domains. Consider the following steps:
- Open the Google Cloud Platform and select your project in the top left corner.
- Open the side menu and go to the
APIs & Services
section. - Go to the
Credentials
section, find theWeb client
inOAuth 2.0 Client IDs
section and open it. - Then you should find the
Authorized JavaScript origins
section. That is the place where you can add any URL origins that will have access to the Google sign-in.
Note: The Google sing-in option requires configuring the allowed email domains. Consider the Google Sign-in allowed domains configuration section to know more about allowed email domains.
The application validates users' email domains when they sign in with Google. These domains are stored within the Cloud Firestore
database. To configure allowed email domains, consider the following steps:
- Browse to the Firebase Console and select the project, created in previous steps.
- Open the
Cloud Firestore
section on the left panel. - Press the
Start collection
button, enter theallowed_email_domains
as a collection ID, and press theNext
button. - After you tapped the
Next
button, you'll be asked to add the first document to your collection. This is the point where we start adding the allowed user email domains for the application. For example, if we want to allow thegmail.com
domain, we should create an empty document with thegmail.com
document ID.
To add more allowed email domains you should add a new document for each email domain with the domain itself as a document ID.
Note: It is required for the allowed email domains collection to be not empty. Empty collection means that no email domains are allowed and the Google sign-in will fail with any domain.
To enable API key restrictions, follow the next steps:
First, let's restrict the browser key:
- Open the Google Cloud Platform and select your project in the top left corner.
- Open the side menu and go to the
APIs & Services
section. - Go to the
Credentials
section, find theBrowser Key
in theAPI Keys
section and open it. - Under the
Application restrictions
, clickHTTP referrers(web sites)
and thenAdd an item
to add URLs that can use this API key. - To specify the APIs available for this key, scroll down to the
API Restrictions
section, and click theRestrict key
button. - In the opened dropdown, enable the following APIs:
Google Identity Toolkit API
,Token Service API
,Firebase Installations API
, andFirebase Management API
. - Click the
Save
button.
To be able to synchronize the build data using the CI Integrations tool, we need to set up a new API key. The following steps are required to do so:
- Go back to the Credentials section.
- Click the
Create Credentials
button at the top of the page and selectAPI Key
. - In the opened dialog, click the
Restrict Key
button. - Set a new name of the created
API key
, e.g.CI Integrations Key
. - Scroll down to the
API Restrictions
section and click theRestrict key
button. - In the displayed dropdown, enable the
Google Identity Toolkit API
. - Click the
Save
button.
After that, update the CI Integration configurations to use the previously created key.
Before building and deploying Metrics application, make sure you have the correct Flutter version installed.
The required version of Flutter is v1.25.0-8.2.pre
.
The following command prints the current Flutter version on your machine:
flutter --version
If current version differs from the required one you can change it by running the following commands:
cd $(which flutter | xargs dirname) && git checkout 1.25.0-8.2.pre
Note: The above commands works correctly on Unix based operating systems (verified: macOS and Ubuntu)! Before using, consider rewriting it according to your operating system.
Also, you should enable Flutter Web support by running the command below:
flutter config --enable-web
Once you've installed Flutter you can build the application to deploy it then to the Firebase Hosting as described in the next section. To build Metrics Web Application consider the following steps:
- Open the terminal and navigate to the
metrics/web
project folder. - Run the
flutter build web --release
to build the release version of the application.
The built application is located under the build/web
folder. By default, Flutter builds a version that uses the HTML renderer that lacks performance on the desktop. Instead, one can use the Skia renderer that significantly improves performance on the desktop but breaks the application on mobile. The solution is to use auto renderer that automatically chooses which renderer to use. To enable auto renderer it is required to set the FLUTTER_WEB_AUTO_DETECT
flag to true
using the --dart-define
argument of the flutter build
command. Consider the following example:
flutter build web --release --dart-define=FLUTTER_WEB_AUTO_DETECT=true
The Metrics Web Application uses Sentry to report errors occurred during the app execution. Sentry requires additional configurations related to the application building. Thus, it requires binding DSN and release options Sentry uses to report errors. To know more about the Sentry options itself and how to bind them consider the Metrics Logger document and Sentry Integration section.
Also, Sentry uses source maps generated during the build to make errors more readable and clear. To ensure flutter build
command generates source maps it is required to pass the --source-maps
flag.
The following example demonstrates building the application with Sentry support:
flutter build web --release --source-maps --dart-define=FLUTTER_WEB_AUTO_DETECT=true --dart-define=SENTRY_DSN=<YOUR_SENTRY_DSN> --dart-define=SENTRY_RELEASE=<YOUR_SENTRY_RELEASE>
Then, using Sentry CLI one should update source maps as described in the Updating Source Maps section of the Metrics Logger document.
Once you've built the application, you can proceed to deploying to the Firebase Hosting.
You can deploy the built application to the Firebase Hosting using the firebase
CLI. Consider the following steps:
- Open the terminal and navigate to the
metrics/web
project folder. - Run
firebase login
command and follow the instructions to log in to the Firebase CLI with your Google account. Use the same account that you used to create your Firebase project (or the one that has access to it). - Run the
firebase use --add
command and select the ID of the project created in previous steps. - Run the
firebase target:apply hosting metrics <YOUR_HOSTING_NAME>
to specify the hosting deploy to. You can find the<YOUR_HOSTING_NAME>
within theHosting
section in the Firebase console. - Run the
firebase deploy --only hosting:metrics
command to deploy the application to the Firebase Hosting.
Note: If you've already had the configured hosting deployment target you should run the firebase target:clear hosting metrics
before the 4th step.
When the deployment process is finished, the application is accessible by the Hosting URL
printed to the console.
Once you've deployed the metrics application to Firebase Hosting, you should finish configuring your Firestore database.
The metrics/firebase
folder contains Firestore security rules, Firestore indexes, and a seedData
Cloud function needed to create a test data for our application. To deploy all of these components, follow the next steps:
- Activate the
seedData
function: go to themetrics/firebase/functions/index.js
file and change theconst inactive = true;
toconst inactive = false;
. - Install ESLint
npm install -g eslint
. - Open the terminal, navigate to
metrics/firebase
folder and ensure all dependencies are installed by running:npm install
. There are known issues regarding deploying Cloud Functions using Node 15.6.x. - Run the
firebase use --add
command and select current project ID. - Run the
firebase deploy
command to deploy all components. - Once command execution finished, you'll find the
seedData
function URL, that can be used to trigger function execution in the console. Save this URL somewhere - you will need it a bit later.
Now you can create test projects in your Firestore database:
- Go to the Firebase Console and select the project, created in previous steps.
- Go to the
Cloud Firestore
section on the left panel and tap on theStart collection
button. - Add collection with
projects
identifier and tapNext
. - In the document creation window tap on the
Auto-ID
button or enter the project identifier you want. - Add a field named
name
with theString
type and the name for your project as a value.
So, you've created the test projects in the database. It is time to generate test builds. In the previous steps you've got the seedData
function URL. Now you can trigger this function to create builds for the project, using this URL. This HTTP function has the following query parameters:
- buildsCount (required) - the number of builds to generate.
- projectId (required) - the project identifier for which builds will be generated.
- startDate (optional) - builds will be generated with 'startedAt'
property in the range from the
startDate
tostartDate - 7
days. Defaults to the current date. - delay (optional) - is the delay in milliseconds between adding builds to the project.
Once you've finished creating test data, you should deactivate the seedData
cloud function. To deactivate this function, follow the next steps:
- Go to the
metrics/firebase/functions/index.js
file and change theinactive
constant back totrue
. - Redeploy this function, using the
firebase deploy --only functions
command.
To validate user's email domain when they sign in with Google, the application uses the validateEmailDomain
cloud function deployed previously. If user signs in using the email and password method then no domain validation happens. To enable this validation, you should enable the validateEmailDomain
for all users. Consider the following steps:
- Open the Google Cloud Platform and select your project in the top left corner.
- Open the side menu and go to the
Cloud Functions
section. - Find the
validateEmailDomain
function and check its checkbox. - Tap the
SHOW INFO PANEL
button on the right top corner under your avatar if this panel is not opened. - In the info panel, select the
PERMISSIONS
tab and click anADD MEMBER
button. - In the opened menu, type
allUsers
in theNew members
field and select aCloud Functions
->Cloud Functions Invoker
role. - Save the changes.
Once you've finished deploying the Metrics application and created the test data, you probably want to open the application and ensure it works well, so you need to create a Firebase User to log-in to the application:
- Go to the Firebase Console and select the project, created in the previous steps.
- Go to the
Authentication
section on the left panel and tap on theAdd user
button. - Provide an email and a password to create a new user and type on the
Add user
button again. - You should see your email with additional data appear in the list of the users' table.
With that in place, you can use your credentials, that you've used to create the user, to fill an authentication form of the web application.
After logging in, you should see a dashboard page with a list of test projects and their metrics if you've created them in the previous steps or no data. The app should provide an ability to switch between light/dark themes.
What is the project blocked on?
No blockers.
What will be impacted by the project?
All the deployment process for a flutter web application(s) within the Metrics project will be impacted.
How will the project be tested?
This project will be tested manually by opening an application using the obtained link.
Summarize alternative designs (pros & cons)
No alternatives considered.
Document milestones and deadlines.
DONE:
- Deploy Metrics application to Firebase Hosting.
- Deploy the Cloud Function for creating test data.
- Deploy Firestore Security Rules & Indexes.
What was the outcome of the project?
The Metrics application and the Cloud Function for creating test data are deployed and available via the links.