Skip to content

Instantly share code, notes, and snippets.

@DennisAlund
Last active January 24, 2023 22:00
Show Gist options
  • Save DennisAlund/1c0a01496d161191733531652dd8b1fc to your computer and use it in GitHub Desktop.
Save DennisAlund/1c0a01496d161191733531652dd8b1fc to your computer and use it in GitHub Desktop.
Firebase hosting with preview channels for pull requests
name: Hosting
on:
pull_request:
branches:
- development
- staging
- main
push:
branches:
- development
- staging
- main
env:
IS_PR_DEV: ${{github.event_name == 'pull_request' && github.base_ref == 'development'}}
IS_PUSH_DEV: ${{github.event_name == 'push' && github.ref == 'refs/heads/development'}}
IS_PR_STAGING: ${{github.event_name == 'pull_request' && github.base_ref == 'staging'}}
IS_PUSH_STAGING: ${{github.event_name == 'push' && github.ref == 'refs/heads/staging'}}
IS_PR_PROD: ${{github.event_name == 'pull_request' && github.base_ref == 'main'}}
IS_PUSH_PROD: ${{github.event_name == 'push' && github.ref == 'refs/heads/main'}}
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
cache: 'yarn'
cache-dependency-path: |
web-app/yarn.lock
- name: Install YARN packages
run: yarn --cwd web-app install
- name: Linting files
run: yarn --cwd web-app lint --no-fix
- name: Set development environment variables
if: env.IS_PR_DEV == 'true' || env.IS_PUSH_DEV == 'true'
run: |
echo "PROJECT_ALIAS=${{secrets.DEVELOPMENT_PROJECT_ALIAS}}" >> $GITHUB_ENV
echo "SA_FIREBASE=${{secrets.DEVELOPMENT_SA}}" >> $GITHUB_ENV
- name: Set staging environment variables
if: env.IS_PUSH_STAGING == 'true'
run: |
echo "PROJECT_ALIAS=${{secrets.STAGING_PROJECT_ALIAS}}" >> $GITHUB_ENV
echo "SA_FIREBASE=${{secrets.STAGING_SA}}" >> $GITHUB_ENV
- name: Set production environment variables
if: env.IS_PUSH_PROD == 'true'
run: |
echo "PROJECT_ALIAS=${{secrets.PRODUCTION_PROJECT_ALIAS}}" >> $GITHUB_ENV
echo "SA_FIREBASE=${{secrets.PRODUCTION_SA}}" >> $GITHUB_ENV
# The variable `SA_FIREBASE` must be a base64 encoded string
# of the service account JSON to avoid problems with quote escaping and
# conversion of '\n' characters generating invalid JSON
- name: Set service account for Firebase CLI
run: |
echo "GOOGLE_APPLICATION_CREDENTIALS=$(pwd)/service_account.json" >> $GITHUB_ENV
echo ${{ env.SA_FIREBASE }} | base64 -d - > service_account.json
- run: firebase use ${{ env.PROJECT_ALIAS }}
- run: yarn --cwd web-app build
- name: Deploy preview channel to Firebase
if: github.event_name == 'pull_request'
# Deploying to preview channel using the branch name as channel name
# String split '/' and use the last part of the branch name
run: >-
firebase
hosting:channel:deploy
$(echo ${{ github.head_ref }} | grep -o '[^/]*$')
--expires 5d
- name: Deploy to Firebase
if: github.event_name == 'push'
run: >-
firebase deploy
--only hosting
-m "Github action run $GITHUB_RUN_ID"
--non-interactive
--force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment