iTranslated by AI
Deploying to Firebase Hosting from CircleCI
What I want to do
I want to deploy to a Firebase Hosting environment automatically when a pull request to the GitHub master branch is merged using CircleCI.
This explanation is intended for those who are new to Firebase.
Note that this article does not cover how to use CircleCI. If you would like to learn about CircleCI, please read this article.
Deploying to Firebase Hosting from the command line
Before deploying from CircleCI to Firebase Hosting, I will first explain how to deploy from the command line.
Creating a project in the cloud
First, create a Firebase project.
Click "Get started" on the Firebase website.
<img width="1261" alt="a.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/259125/24b65481-d052-f92b-6037-6d56a3322b77.png">
Click "Create project".
<img width="1253" alt="b.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/259125/a81d9544-8593-ca46-e99c-9086a728d059.png">
Give it an appropriate name and click "Continue".
<img width="1260" alt="c.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/259125/e427c248-dbe5-4c5d-10c3-b686430f58c3.png">
Click "Continue".
<img width="856" alt="d.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/259125/040929ed-d3b4-b8eb-97c9-77a252a8e4e7.png">
Check all the boxes and click "Create project".
<img width="799" alt="e.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/259125/7d0602f1-ca7f-fb76-b0cb-268685ad5e29.png">
Click "Continue".
<img width="369" alt="f.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/259125/ec973562-cc0b-c783-8cba-c2daf8fddee4.png">
Once you reach the dashboard, the project creation is complete.
<img width="1255" alt="g.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/259125/5fee35df-03cf-8410-7d4e-0e5106a413f3.png">
firebase login
To use the firebase command in your local environment, you need to install firebase-tools. Install it using npm install -g.
npm install -g firebase-tools
Next, run the login command to associate your Google account.
firebase login
When this appears in your browser, the login is complete.
<img width="457" alt="h.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/259125/c64f15d7-acc5-1750-8c65-3461530a9c3e.png">
firebase init
Navigate to the project you want to deploy via Firebase Hosting using the cd command and execute firebase init.
Think of it as running it in the same directory as your .gitignore file for a git project.
The repository where I actually ran firebase init this time is available here.
This project was created using vue-cli.
The article on how to create this project can be found here for your reference.
Move to the appropriate directory and execute firebase init.
firebase init
You will be asked for settings interactively.
Please configure it according to your environment. In this case, I set it up as follows:
-
? Which Firebase CLI features do you want to set up for this folder? Press Space to select features, then Enter to confirm your choices.-> Select Hosting. -
? Please select an option-> Select "Use an existing project". -
? Select a default Firebase project for this directory-> Select the project you just created. -
? What do you want to use as your public directory?-> Specify thedistdirectory. This is because the vue-cli project is configured to place the built source files in thedistdirectory via thenpm run buildcommand. Please set this according to your environment. -
? Configure as a single-page app (rewrite all urls to /index.html)?-> Select No as it is not a SPA.
<img width="691" alt="j.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/259125/adb5e66a-08ff-2062-6d88-b19d177828f3.png">
(↑ A console like this will be displayed)
firebase deploy
At this stage, you can deploy using commands.
The npm run build command will place the built source files in the dist directory (this is a setting in vue-cli).
Then, by running the firebase deploy command, the file group placed in the dist directory will be deployed to Firebase Hosting.
npm run build
firebase deploy
You can access the deployed page via the URL displayed in the console.
<img width="630" alt="x.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/259125/6612d8a3-e042-b73c-73b6-c0f3ee9fe062.png">
If you access https://vuesample-e2c8b.web.app displayed in the console, you will see your own web page.

You were able to easily deploy to the public without preparing a web server! This alone is amazing!
Deploying to Firebase Hosting from CircleCI
I have already explained how to deploy from the command line. Now, I will explain how to deploy to Firebase Hosting from CircleCI.
The basic flow is as follows:
- Installing
firebase-toolsto the project - Registering the Firebase token to CircleCI
- Configuring the
.circleci/config.ymlwith thefirebase deploycommand
1. Installing firebase-tools to the project
Since you need to install firebase-tools in the pipeline running on CircleCI, run the npm install --save-dev firebase-tools command in your local project. This ensures that firebase-tools is installed when npm install is executed on the CircleCI pipeline.
npm install --save-dev firebase-tools
2. Registering the Firebase token to CircleCI
To deploy from outside your own environment, you need a Firebase token, so you must generate one and register it with CircleCI.
firebase login:ci
A token will be generated as follows:

Now that the token is generated, let's go register it in CircleCI.
Click on "Project Settings" in the top right corner of the CircleCI website.

Click on "Environment Variables" -> "Add Environment Variables".
<img width="1792" alt="l.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/259125/45efdd5b-558b-ebf3-a863-98d4ecc77868.png">
Set the Name to FIREBASE_TOKEN and the Value to the token you generated earlier, then click "Add Variable".
<img width="581" alt="m.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/259125/fe7ca35e-7c81-05ad-0c1f-e47c5cb27ada.png">
Registration is now complete.
<img width="1045" alt="n.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/259125/917ada7b-0d39-a2fc-ecb4-19f076890026.png">
Now you can use the token by declaring $FIREBASE_TOKEN in the commands within .circleci/config.yml.
- If you have registered
.firebaserccontaining the Firebase ID in.gitignore, follow the same steps to register$FIREBASE_IDand specify$FIREBASE_IDin your deploy command. I have not registered.firebasercin.gitignorefor this project, so I will skip that part.
3. Configuring the .circleci/config.yml with the firebase deploy command
I have configured .circleci/config.yml as follows:
version: 2.1
executors:
default:
working_directory: ~/workspace
docker:
- image: node:12
commands:
restore_npm:
steps:
- restore_cache:
name: Restore npm dependencies
key: npm-{{ checksum "package.json" }}
save_npm:
steps:
- save_cache:
name: Cache npm dependencies
key: npm-{{ checksum "package.json" }}
paths:
- ~/workspace/node_modules
jobs:
build:
executor: default
steps:
- checkout
- restore_npm
- run:
name: Install dependencies
command: npm install
- run:
name: Lint
command: npm run lint
- run:
name: Build
command: npm run build
- run:
name: Test
command: npm run test:unit
- save_npm
deploy:
executor: default
steps:
- checkout
- restore_npm
- run:
name: Build
command: npm run build
- run:
name: Check dist
command: ls -la dist
- run:
name: Deploy to Firebase Hosting
command: ./node_modules/.bin/firebase deploy --token=$FIREBASE_TOKEN
workflows:
push-build:
jobs:
- build
- deploy:
requires:
- build
filters:
branches:
only: master
The key point is that I set it to deploy to Firebase Hosting only when pushed to the master branch. (This is the filters section.)
In other words, as shown below, when pushing from a development branch for a pull request, only the build job runs. When a pull request is merged into the master branch, or when pushing directly to the master branch, both the build and deploy jobs will run.
Of course, this depends on your project operations, but I have set it up this way as a demonstration.
As you can see in the diagram below, only build runs for the feature/firebase branch, but both build and deploy jobs run for the master branch.
<img width="1085" alt="aas.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/259125/4a41cec7-b958-eefe-1ee7-ebe38830dab6.png">
That concludes the process of deploying to Firebase from CircleCI.
Closing
Next, I would like to create a mechanism to notify the results of this deployment on Slack.
Next article:
Notify CircleCI Job results to Slack
Discussion