Skip to content

Instantly share code, notes, and snippets.

@adilsoncarvalho
Last active August 22, 2022 12:00
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adilsoncarvalho/db8e72c494c82b15a50e981cdbf50727 to your computer and use it in GitHub Desktop.
Save adilsoncarvalho/db8e72c494c82b15a50e981cdbf50727 to your computer and use it in GitHub Desktop.
Deploying a firebase function from Bitbucket pipelines

Deploying functions from Bitbucket Pipelines

It's quite easy and the same process applies to all Firebase features with minor changes.

Acquire a deployment token

In order to deploy you'll need a deployment token (I named it FIREBASE_TOKEN) and it can be acquired by using the following command

firebase login:ci

Add it to your pipeline's environment variables or to your team's environment variables if you want to use it among multiple repositories.

When you have many functions on the same repository

You can define the function to get deployed using --only functions:addMessage as defined on the Firebase documentation.

The boring part is that you should find out which function got changed to deploy just it, what you can do using git.

The Firebase CI/CD documentation

https://github.com/firebase/firebase-tools#using-with-ci-systems

The supported node versions

https://firebase.google.com/docs/functions/manage-functions#set_nodejs_version

# using the available node version at Google Cloud Function
image: node:12.16.1
pipelines:
default:
- step:
name: Test function
caches:
- functions-npm
script:
- export PATH=$PATH:$BITBUCKET_CLONE_DIR/functions/node_modules/.bin
- cd functions
- npm install
# - npm test
branches:
master:
- step:
name: Build function
caches:
- functions-npm
script:
- export PATH=$PATH:$BITBUCKET_CLONE_DIR/functions/node_modules/.bin
- cd functions
- npm install
- npm install firebase-tools
- firebase deploy
--only functions
--non-interactive
--token=$FIREBASE_TOKEN
-m "Deploying $BITBUCKET_COMMIT (CI/CD)"
definitions:
caches:
functions-npm: $BITBUCKET_CLONE_DIR/functions/node_modules
@Donkijote
Copy link

Trying to execute this pipeline throws the following error

Error: HTTP Error: 401, Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential.

image

can you give me a hand with this?

@alexander0205
Copy link

alexander0205 commented Jan 26, 2021

How to define other project to deploy?

@adilsoncarvalho
Copy link
Author

Hello @Donkijote,

I'd guess that's probably because of the node version. It won't allow the installation of the lastest firebase-tools, and probably the one available to node@6.11 doesn't connect to the firebase API anymore.

Yeap, as for June 8th 2020 the support for node 8.x was dropped (imagine for 6.x 😅)

I updated the script to use the latest node version supported by firebase. As of today, the latest stable is v12. You can use v14, but it is still in beta.

https://firebase.google.com/docs/functions/manage-functions#set_nodejs_version

@adilsoncarvalho
Copy link
Author

Hello @alexander0205,

You might want to have a look at the firebase use command. It sets the default project to be used. You'll need to add a line invoking it before the firebase deploy on at line 25.

https://github.com/firebase/firebase-tools#configuration-commands

@alexander0205
Copy link

Hello @alexander0205,

You might want to have a look at the firebase use command. It sets the default project to be used. You'll need to add a line invoking it before the firebase deploy on at line 25.

https://github.com/firebase/firebase-tools#configuration-commands

Thanks, work good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment