DevOps Blog - Nicolas Paris

Deploy on Firebase Hosting in a Gitlab CICD with staging and production

DevOps

Here is a very quick setup.

I have two environment for staging and production corresponding to two application in a single project in firebase, called master and develop.

I use a temporary docker container that will contain my code. This could be controversial and can be done in a single stage.

My first approch was to use FIREBASE_TOKEN but I dont want to start with something that will be deprecated.

Authenticating with FIREBASE_TOKEN is deprecated and will be removed in a future major version of firebase-tools. Instead, use a service account key with GOOGLE_APPLICATION_CREDENTIALS

I had some Error: Failed to authenticate, have you run firebase login? but the --debug option does help.

variables:
IMAGE: "eu.gcr.io/my-project/${CI_PROJECT_NAME}"
TAG: "${CI_COMMIT_SHORT_SHA}"

stages:
- build
- delivery

build:
stage: build
tags:
- gcp-shell
except:
- tags
before_script:
- echo "$GOOGLE_APPLICATION_CREDENTIALS_JSON" > key.json
script:
- docker build -t ${IMAGE}:${TAG} .
- gcloud auth activate-service-account --key-file=/gitlab.json
- gcloud auth configure-docker
- docker push ${IMAGE}:${TAG}

delivery:
stage: delivery
tags:
- gcp-docker
image: ${IMAGE}:${TAG}
only:
- master
- develop
script:
- cd /app
- firebase use my-project-id
- firebase deploy --only hosting:${CI_COMMIT_BRANCH}

The dockerfile is simple, compilation of a node application with the installation of firebase-tools.

FROM node:18.12-alpine

WORKDIR /app
ENV NODE_ENV production
RUN npm install -g firebase-tools
COPY package*.json ./
RUN NODE_ENV=staging npm install
COPY . .
RUN npm run build

And firebase.json like

{
"hosting": [{
"target": "master",
"public": "dist",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
},{
"target": "develop",
"public": "dist",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
]
}

and the follogin in .firebaserc

{
"projects": {
"default": "myprojectid"
},
"targets": {
"myprojectid": {
"hosting": {
"develop": [
"my-app-staging"
],
"master": [
"my-app-production"
]
}
}
},
"etags": {}
}