Bitbucket Pipelines Documentation 📋

Welcome to the Bitbucket Pipelines documentation for our bitbucket-pipelines.yml. This file defines CI/CD workflows for testing and deploying our Firebase Cloud Functions across Development, Staging, and Production environments.


📑 Index

  1. Overview
  2. Pipeline Structure
  3. Default Pipeline
  4. Branch Pipelines
  5. Definitions
  6. Environment Variables & Secrets 🔑
  7. Artifacts & Caches
  8. Pipeline Flowchart
  9. Best Practices & Notes 📝

🔍 Overview

Our pipeline uses the official Node.js Docker image (node:latest) to:

  • Install dependencies
  • Run tests
  • Migrate Firestore schemas via Fireway
  • Deploy Cloud Functions to Firebase
  • Isolate credentials per environment using service-account JSON files

🏗 Pipeline Structure

image: node:latest

pipelines:
  default:
    - step: *Test

  branches:
    master:
      - step: *Deploy-Prod

    staging:
      - step: *Deploy-Staging

    develop:
      - step: *Deploy-Dev

definitions:
  caches:
    functions-npm: $BITBUCKET_CLONE_DIR/functions/node_modules

  steps:
    - step: &Test
        name: Test
        caches: [functions-npm]
        script:
          - echo "Test suite will run here"
        artifacts:
          - build/**

    - step: &Deploy-Prod
        name: Deploy to Firebase Prod
        caches: [functions-npm]
        script:
          - EXPORT & INSTALL & DEPLOY commands...

    - step: &Deploy-Staging
        name: Deploy to Firebase Prod via staging
        caches: [functions-npm]
        script:
          - EXPORT & INSTALL & DEPLOY commands...

    - step: &Deploy-Dev
        name: Deploy to Firebase Dev
        oidc: true
        caches: [functions-npm]
        script:
          - EXPORT & INSTALL & DEPLOY commands...

Note: We use YAML anchors (&) and aliases (*) to DRY up repeated steps.


⚙️ Default Pipeline

Runs on all branches except master, staging, and develop.
Primarily intended for CI checks (lint, unit tests, etc.).

Property Value
Step Test
Caches functions-npm
Script echo "Test suite..." 🔧
Artifacts build/** (for later download)
pipelines:
  default:
    - step:
        name: Test
        caches:
          - functions-npm
        script:
          - echo test will go here
        artifacts:
          - build/**

🌿 Branch Pipelines

master

Deploys Production Cloud Functions.

branches:
  master:
    - step:
        name: Deploy to Firebase Prod
        caches:
          - functions-npm
        script:
          - export PATH=$PATH:$BITBUCKET_CLONE_DIR/functions/node_modules/.bin
          - cd functions
          - npm install
          - mkdir src/JSONs
          - echo "$SERVICE_ACCOUNT_KEY_PROD" > src/JSONs/serviceAccountKey_prod.json
          - export GOOGLE_APPLICATION_CREDENTIALS='src/JSONs/serviceAccountKey_prod.json'
          - export FIREBASE_APP_STORAGE_BUCKET=$FIREBASE_APP_STORAGE_BUCKET_PROD
          - export FIREBASE_APP_PROJECT_ID=$FIREBASE_APP_PROJECT_ID_PROD
          - npm install firebase-tools
          - firebase use prod --token $FIREBASE_TOKEN_PROD
          - fireway migrate --require="ts-node/register"
          - firebase deploy --only functions --force --token $FIREBASE_TOKEN_PROD
  • Service Account: Inline JSON from $SERVICE_ACCOUNT_KEY_PROD
  • Migrations: fireway migrate
  • Deploy: firebase deploy --only functions

staging

Deploys Production via the staging branch with extra debug output.

branches:
  staging:
    - step:
        name: Deploy to Firebase Prod via staging
        caches:
          - functions-npm
        script:
          - export PATH=$PATH:$BITBUCKET_CLONE_DIR/functions/node_modules/.bin
          - cd functions
          - mkdir -p src/JSONs
          - echo "$SERVICE_ACCOUNT_KEY_PROD" > src/JSONs/serviceAccountKey_prod.json
          - export GOOGLE_APPLICATION_CREDENTIALS='src/JSONs/serviceAccountKey_prod.json'
          - export FIREBASE_APP_STORAGE_BUCKET=$FIREBASE_APP_STORAGE_BUCKET_PROD
          - export FIREBASE_APP_PROJECT_ID=$FIREBASE_APP_PROJECT_ID_PROD
          - echo "Dumping package.json for debug"
          - cat package.json
          - echo "Installing dependencies"
          - npm install --verbose
          - npm install firebase-tools
          - firebase use prod --token $FIREBASE_TOKEN_PROD || echo "Skipping firebase use..."
          - fireway migrate --require="ts-node/register"
          - firebase deploy --only functions --force --token $FIREBASE_TOKEN_PROD
  • 📦 Verbose install for troubleshooting
  • 🔄 Fallback on firebase use

develop

Deploys Development environment via OIDC (no tokens stored).

branches:
  develop:
    - step:
        name: Deploy to Firebase Dev
        oidc: true
        caches:
          - functions-npm
        script:
          - export PATH=$PATH:$BITBUCKET_CLONE_DIR/functions/node_modules/.bin
          - cd functions
          - mkdir -p src/JSONs
          - echo "$SERVICE_ACCOUNT_KEY_DEV" > src/JSONs/serviceAccountKey_dev.json
          - export GOOGLE_APPLICATION_CREDENTIALS="src/JSONs/serviceAccountKey_dev.json"
          - export FIREBASE_APP_STORAGE_BUCKET=$FIREBASE_APP_STORAGE_BUCKET_DEV
          - export FIREBASE_APP_PROJECT_ID=$FIREBASE_APP_PROJECT_ID_DEV
          - echo "Dumping package.json for debug"
          - cat package.json
          - echo "Installing dependencies"
          - npm install --verbose
          - npm install firebase-tools
          - firebase use dev --token $FIREBASE_TOKEN_DEV || echo "Skipping firebase use..."
          - fireway migrate --require="ts-node/register"
          - firebase deploy --only functions --force --token $FIREBASE_TOKEN_DEV
  • 🔐 OIDC-based auth for improved security
  • Mirrors Staging logic against Dev credentials

📦 Definitions

Key Value
functions-npm Cache path: $BITBUCKET_CLONE_DIR/functions/node_modules
definitions:
  caches:
    functions-npm: $BITBUCKET_CLONE_DIR/functions/node_modules

🔑 Environment Variables & Secrets

Variable Purpose
SERVICE_ACCOUNT_KEY_{DEV PROD}
FIREBASE_APP_STORAGE_BUCKET_{DEV PROD}
FIREBASE_APP_PROJECT_ID_{DEV PROD}
FIREBASE_TOKEN_{DEV PROD}
BITBUCKET_CLONE_DIR Bitbucket workspace root

Tip: Keep secrets in Bitbucket Pipelines Settings → Repository variables.


💾 Artifacts & Caches

  • Artifacts
    • build/** produced by Test step (downloadable).
  • Caches
    • functions-npm to speed up npm install.

🔄 Pipeline Flowchart

flowchart TD
  A[Push to Repo] --> B{Branch?}
  B --> |master| M[Test] --> MP[Deploy Prod]
  B --> |staging| S[Test] --> SP[Deploy Prod via staging]
  B --> |develop| D[Test] --> DP[Deploy Dev]
  B --> |others| O[Test Only]

  subgraph Default
    O
  end

  subgraph Branch Pipelines
    MP
    SP
    DP
  end

Legend:

  • Test: CI checks
  • Deploy Prod / Staging / Dev: Firebase Cloud Functions deployment

📝 Best Practices & Notes

  • Use YAML anchors to avoid duplication.
  • Echo debug info (e.g., package.json) when troubleshooting.
  • Encrypt service-account JSON via repository variables.
  • Migrate Firestore schema before deploying functions (fireway migrate).
  • OIDC in develop pipeline reduces secret exposure.
  • Force deploy (--force) to override potential deploy conflicts.

🚀 Ready to ship your Cloud Functions? This config ensures consistent, secure, and automated deployments across all environments!