Firebase Hosting: Preview and Deploy via GitHub Actions

Preview and Deploy your Angular or Scully app on Firebase Hosting automated via GitHub Actions

Authors
Marc Stammerjohann Marc Stammerjohann
Published at

You start building an Angular or a Scully application and at some point you want to invite colleagues, friends, family or customers to check it out. Firebase Hosting allows to host your static or dynamic web apps for FREE 💸. You are setting up a GitHub workflow deploying your Scully app (works with Angular and any other web framework) to preview and live channel.

Demo source code and hosted on Firebase 🔥.

Before you dive straight into this you will need a

Use your existing web application or create a new one by following Angular 10 with Tailwind CSS or Jamstack: Angular + Scully + Tailwind CSS to get started.

Getting started

Install Firebase CLI minimum in v8.12.0 for preview channels support.

# install firebase cli
npm install -g firebase-tools

# init firebase hosting
firebase init hosting
# hosting already setup, prepare GitHub workflow
firebase init hosting:github

Follow the CLI prompts to setup Firebase hosting and GitHub workflow.

Hosting Setup

Firebase Hosting Setup

Start by selecting an existing Firebase project, create one in Firebase console, or create a new project through the CLI.

Next enter the public directory containing all files of your web app including index.html which is uploaded to Firebase hosting

# Angular
dist/<project-name>

# Scully `outDir` specified in your scully.<project-name>.ts defaults to
dist/static

You can change the public directory anytime in firebase.json file.

Answer the next question "Configure as a single-page app (rewrite all urls to /index.html)?" with yes for Angular apps (and other single-page apps) and no for Scully apps (and other static-site apps).

Let Firebase CLI initialize your GitHub repository for automatic deploys. Several steps are taken care by the Firebase CLI for you

  • Creating a Firebase service account with deployment permissions to Firebase Hosting
  • Encrypt and add secret to GitHub repository
  • Creating GitHub workflow yaml files

Enter no for the next two questions to overwrite dist/static/404.html and dist/static/index.html, let those be generated by Scully.

Select a GitHub repository to setup your secret token for your workflow and enter a build script to build Angular and Scully like npm ci && npm run build:ci. For a Scully build add the following two scripts to your package.json:

"build:ci": "npm run build:prod && npm run scully:ci"
"scully:ci": "scully -- --host='0.0.0.0' --scanRoutes --serverTimeout=60000",

If you like to deploy to live channel on merged Pull Request answer with yes and enter your branch name for the live channel for example main.

GitHub Workflow

You should have now two workflows if you used the Firebase CLI. The workflows use the GitHub Action Deploy to Firebase Hosting, currently in alpha release.

Workflow to deploy to a preview channel on Pull Request .github/workflows/firebase-hosting-pull-request.yml

# This file was auto-generated by the Firebase CLI
# https://github.com/firebase/firebase-tools

name: Deploy to Firebase Hosting on PR
'on': pull_request
jobs:
  build_and_preview:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: 'npm ci && npm run build:ci'
      # Add additional build steps here
      # - run: ...
      - uses: FirebaseExtended/action-hosting-deploy@v0
        with:
          repoToken: '${{ secrets.GITHUB_TOKEN }}'
          firebaseServiceAccount: '${{ secrets.FIREBASE_SERVICE_ACCOUNT }}'
          projectId: your-firebase-project-id
          # default expire value 7 days
          # expires: 7d
        env:
          FIREBASE_CLI_PREVIEWS: hostingchannels

Workflow to deploy to your live channel on push to main branch .github/workflows/firebase-hosting-merge.yml

# This file was auto-generated by the Firebase CLI
# https://github.com/firebase/firebase-tools

name: Deploy to Firebase Hosting on merge
'on':
  push:
    branches:
      - main
jobs:
  build_and_deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: 'npm ci && npm run build:ci'
      # Add additional build steps here
      # - run: ...
      - uses: FirebaseExtended/action-hosting-deploy@v0
        with:
          repoToken: '${{ secrets.GITHUB_TOKEN }}'
          firebaseServiceAccount: '${{ secrets.FIREBASE_SERVICE_ACCOUNT }}'
          channelId: live
          projectId: your-firebase-project-id
        env:
          FIREBASE_CLI_PREVIEWS: hostingchannels

Deploying to the live channel requires channelId set to live. If left blank the action creates a new preview channel using the PR-branch name.

Additional option for the preview channel is expires which defaults to 7 days. Change the expiration of your preview channel to maximum 30 days. It supports the syntax h for hours, d for days and w for weeks, for example 19h, 30d, 3w.

Preview and Live Channel

Create a Pull Request with the above GitHub workflows and you should see the GitHub Action start building

GitHub Action run on Pull Request

After the workflow finished successfully, the Firebase action creates a comment with the preview URL for this PR.

Preview URL created by Firebase Action

View the preview of your web app, if you are not happy with your changes repeat it. Here is the Scully demo blog in the preview channel on Firebase Hosting.

Preview URL created by Firebase Action

Finally, merge your Pull Request to trigger the deployment to the live channel. Find the Scully demo blog on the live channel.

It was never easier to ship improvements to your web application to preview, ask colleagues or customers for a review 👌❓ and simply deploy your changes to the live channel 🚀 by merging your PR.

Sponsor us

Did you find this post useful? We at notiz.dev write about our experiences developing Apps, Websites and APIs and develop Open Source tools. Your support would mean a lot to us 🙏. Receive a reward by sponsoring us on Patreon or start with a one-time donation on GitHub Sponsors.

Table of Contents

Top of Page Comments Related Articles

Related Posts

Find more posts like this one.

Authors
Marc Stammerjohann
October 17, 2022

Codegen REST API types and requests for Angular

Automatic code generation from OpenAPI 3 for Angular
Angular NestJS Read More
Authors
Gary Großgarten
September 23, 2021

Media Queries with RxJS

Media Queries | Practical examples with RxJS
RxJS Angular Read More
Authors
Marc Stammerjohann
December 15, 2020

Tailwind CSS Purge: Optimize Angular for Production

Remove unused Tailwind CSS utilities from your Angular production build for best performance
Tailwind CSS Angular Scully Read More
Authors
Marc Stammerjohann
October 29, 2020

Jamstack: Angular + Scully + Tailwind CSS

Use Angular's static site generator Scully and style it with Tailwind CSS
Scully Angular Tailwind CSS Read More
Authors
Marc Stammerjohann
June 03, 2021

Angular with Tailwind CSS

Learn how to style Angular applications with Tailwind CSS
Angular Tailwind CSS CSS Read More
Authors
Gary Großgarten
March 25, 2020

Angular Elements: Create a Component Library for Angular and the Web

Publish Angular components and Custom Elements from a single project! Using the Angular CLI.
Angular Web Components Read More
Authors
Gary Großgarten
March 17, 2020

Build and publish your first GitHub Action

Build your first GitHub Action and deploy it to the Marketplace - Using TypeScript 💙
GitHub TypeScript Read More

Sign up for our newsletter

Sign up for our newsletter to stay up to date. Sent every other week.

We care about the protection of your data. Read our Privacy Policy.