Gitlab deploy to Firebase
Gitlab deployment script that will deploy to a Firebase app.
Gitlab Firebase Docker Build Image
Create a repo in Gitlab, that will be responsible for building a docker image. This image will be built into the gitlab registry, so it can be used to deploy any Gitlab repo to a Firebase hosting app.
Create a repo called firebase in your Gitlab project.
Create a Dockerfile that will install the latest node version and the command line tools for firebase.
FROM node
RUN npm i -g firebase-tools
Create a .gitlab-ci.yml to build this image into your projects Gitlab registry.
image: docker:git
services:
- docker:dind
stages:
- build
- release
variables:
GITLAB_PROJECT: {{ PROJECT_NAME }}
GITLAB_REPO: firebase
GITLAB_CONTAINER_TEST_IMAGE: registry.gitlab.com/$GITLAB_PROJECT/$GITLAB_REPO:$CI_COMMIT_SHA
GITLAB_CONTAINER_RELEASE_IMAGE: registry.gitlab.com/$GITLAB_PROJECT/$GITLAB_REPO:latest
before_script:
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
build:
stage: build
script:
- docker build -t $GITLAB_CONTAINER_TEST_IMAGE .
- docker push $GITLAB_CONTAINER_TEST_IMAGE
release:
stage: release
script:
- docker pull $GITLAB_CONTAINER_TEST_IMAGE
- docker tag $GITLAB_CONTAINER_TEST_IMAGE $GITLAB_CONTAINER_RELEASE_IMAGE
- docker push $GITLAB_CONTAINER_RELEASE_IMAGE
only:
- master
Change anywhere it says {{ PROJECT_NAME }} to whatever your group or project name is.
Push these files to the master branch of your firebase repo and it will create a build image we can use to deploy any firebase app in our project. Next we will use this newly create image to build and deploy for us.
Gitlab CI/CD Script
Generate the firebase token so Gitlab will know how to deploy our app. On the command line run this command. It will authenticate and generate a new token. Copy the token and add it to your Gitlab Environment variables as FIREBASE_TOKEN
firebase login:ci
Create a .gitlab-ci.yml file
image: registry.gitlab.com/{{ PROJECT_NAME }}/firebase
stages:
- test
- deploy
cache:
paths:
- node_modules/
key: "$CI_BUILD_REPO"
test:
stage: test
before_script:
- cd ./src
- npm i
script:
- npm run test
except:
- develop
- master
lint:
stage: test
before_script:
- cd ./src
- npm i
script:
- npm run lint
except:
- develop
- master
deploy-dev:
stage: deploy
before_script:
- cd ./src
- npm i
- npm run build:dev
script:
- firebase use dev --token $FIREBASE_TOKEN
- firebase deploy --only hosting -m "Pipe $CI_PIPELINE_ID Build $CI_BUILD_ID" --token $FIREBASE_TOKEN
only:
- develop
deploy-prod:
stage: deploy
before_script:
- cd ./src
- npm i
- npm run build
script:
- firebase use default --token $FIREBASE_TOKEN
- firebase deploy --only hosting -m "Pipe $CI_PIPELINE_ID Build $CI_BUILD_ID" --token $FIREBASE_TOKEN
when: manual
only:
- master
This script assumes you are running npm builds, but you can add any build steps you want.
To finally deploy the app to firebase the script runs these two lines
firebase use dev --token $FIREBASE_TOKEN// This will use the environment called "dev"firebase deploy --only hosting -m "Pipe $CI_PIPELINE_ID Build $CI_BUILD_ID" --token $FIREBASE_TOKEN// This will deploy the app to firebase using the $FIREBASE_TOKEN as authentication
Now anytime you push to develop or master branch it will deploy to your Firebase hosting application.
Happy coding!