Back to homepage

Firebase Hosting Targets

Published on 27/02/2021
Github Workflow doesn't support Firebase hosting target?

Add a target

Target is a shortcut or alias of your sites. Assume you have installed Firebase CLI already.

To add a new target, run this command

# firebase target:apply hosting [targetName] [siteName]
firebase target:apply hosting new2021 ianc-dev-2021

Site name is basically the leading part of your [siteName].web.app .

Don't use number as target name. Otherwise you'll see strange stuff in .firebaserc and the target won't be found by CLI - even it was created by itself!

.firebaserc

Targets reflect in .firebaserc file. Now you'll see the new target there.

{
  "projects": {
    "default": "ianc-dev-website-lorem-epsom"
  },
  "targets": {
    "ianc-dev-website-lorem-epsom": {
      "hosting": {
        "new2021": [
          "ianc-dev-2021"
        ]
      }
    }
  }
}

Remove a target

To remove a target just run.

firebase target:remove hosting [targetName]

Deploy your site using target - after configuring firebase.json

So finally you can use target to deploy code to the target site - no matter if you have single or multiple sites.

To make it possible, you'll need to update firebase.json first.

Initially, you can find "hosting": {} in the file. Since we'd like to manage multiple sites, the value of "hosting" becomes an array.

"hosting": [    
  {
    "public": "dist",
    "target": "new2021",
    "rewrites": [
      {          
        "source": "**",          
        "destination": "/index.html"
      }
    ]
  }
]

Basically you just wrap the existing one into an array then add "target" with the target name you've just created.

Now you can deploy your site using the target.

firebase deploy --only target:new2021

Another way to deploy - use hosting

You can also deploy to a target site using "hosting" in command instead of "target"

firebase deploy --only hosting:ianc-dev-website-lorem-epsom

So what's the point creating targets rather than using hosting directly?

Well… Just my Github Workflow CD doesn't seem to support hosting so far so I have to use target.

jobs:
  build_and_preview:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: npm install
      - run: npm run build
      - uses: FirebaseExtended/action-hosting-deploy@v0
        with:
          repoToken: '${{ secrets.GITHUB_TOKEN }}'
          firebaseServiceAccount: '${{ secrets.FIREBASE_SERVICE_ACCOUNT_IANC_DEV_WEBSITE }}'
          projectId: ianc-dev-website-lorem-epsom
          **target**: new2021

Be the first
to get my latest blog
Comments