Skip to content

Instantly share code, notes, and snippets.

@DennisAlund
Last active January 24, 2023 22:00
Show Gist options
  • Select an option

  • Save DennisAlund/1c0a01496d161191733531652dd8b1fc to your computer and use it in GitHub Desktop.

Select an option

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:
FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
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
container:
image: evenbit/firebase:node14
steps:
- uses: actions/checkout@v2
- name: Get YARN cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: Initiate YARN cache
uses: actions/cache@v1
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install YARN packages
run: yarn install --immutable
- name: Linting files
run: yarn lint --no-fix
- name: Set development environment variables
if: env.IS_PR_DEV == 'true' || env.IS_PUSH_DEV == 'true'
run: |
echo "NODE_ENV=develop" >> $GITHUB_ENV
echo "PROJECT_ALIAS=${{secrets.DEVELOPMENT_PROJECT_ALIAS}}" >> $GITHUB_ENV
echo "HOSTING_ALIAS=${{secrets.DEVELOPMENT_HOSTING_ALIAS}}" >> $GITHUB_ENV
- name: Set staging environment variables
if: env.IS_PUSH_STAGING == 'true'
run: |
echo "NODE_ENV=staging" >> $GITHUB_ENV
echo "PROJECT_ALIAS=${{secrets.STAGING_PROJECT_ALIAS}}" >> $GITHUB_ENV
echo "HOSTING_ALIAS=${{secrets.STAGING_HOSTING_ALIAS}}" >> $GITHUB_ENV
- name: Set production environment variables
if: env.IS_PUSH_PROD == 'true'
run: |
echo "NODE_ENV=production" >> $GITHUB_ENV
echo "PROJECT_ALIAS=${{secrets.PRODUCTION_PROJECT_ALIAS}}" >> $GITHUB_ENV
echo "HOSTING_ALIAS=${{secrets.PRODUCTION_HOSTING_ALIAS}}" >> $GITHUB_ENV
- run: firebase use ${{ env.PROJECT_ALIAS }} --token ${{ env.FIREBASE_TOKEN }}
- run: yarn 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
--token ${{ env.FIREBASE_TOKEN }}
- name: Deploy to Firebase
if: github.event_name == 'push'
run: >-
firebase deploy
--only hosting:${{ env.HOSTING_ALIAS }}
-m "Github action run $GITHUB_RUN_ID"
--non-interactive
--force
--token ${{ env.FIREBASE_TOKEN }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment