CI/CD Integration
Run MSR Firebase migrations in CI and deployment pipelines with credentials kept in your platform’s secret store.
Table of contents
GitHub Actions
Store the service account JSON and database URL as GitHub Actions secrets. The workflow writes the JSON secret to a temporary file, uses the Firebase URL from the console, and removes the file even after a failure.
name: Run Firebase migrations
on:
workflow_dispatch:
push:
branches: [main]
jobs:
migrate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '22'
cache: npm
- run: npm ci
- name: Write service account key
env:
FIREBASE_SERVICE_ACCOUNT_KEY: $
run: printf '%s' "$FIREBASE_SERVICE_ACCOUNT_KEY" > "$RUNNER_TEMP/firebase-key.json"
- name: Validate migrations
env:
DATABASE_URL: $
GOOGLE_APPLICATION_CREDENTIALS: $/firebase-key.json
run: npx msr-firebase validate
- name: Run migrations
env:
DATABASE_URL: $
GOOGLE_APPLICATION_CREDENTIALS: $/firebase-key.json
run: npx msr-firebase migrate --backup-mode full
- name: Remove service account key
if: always()
run: rm -f "$RUNNER_TEMP/firebase-key.json"
This example assumes the package is already in the application’s dependencies. The current adapter requires a service-account JSON key file; the CLI does not support keyless Application Default Credentials.
Live RTDB smoke test
The Live Firebase RTDB smoke test workflow runs when a pull request targeting master is opened, updated, or reopened. It also supports manual dispatch from master with a short label using lowercase letters, digits, underscores, or hyphens. For pull request events, it checks out and runs the trusted master branch code; it does not execute the pull request’s code. A UTC timestamp and the GitHub run ID and attempt create a separate shift for each run.
Before the first run, add these repository Actions secrets:
FIREBASE_SERVICE_ACCOUNT_KEY: the complete service-account JSON.FIREBASE_DATABASE_URL: the dedicated test Realtime Database URL.
Each run writes only beneath __msr_live_smoke__/<label>/<run-id>/<attempt>. Including the attempt keeps a workflow re-run isolated from any data left by an earlier attempt. The workflow refuses to proceed if that generated shift already contains data. It runs one marker migration with Core’s manual backup mode, checks the marker and schema-version record, then removes the run-specific shift. The service-account file is created with owner-only permissions and removed by an always() cleanup step. The workflow uses read-only repository permissions and does not upload credentials, database values, or artifacts.
Use this only with the dedicated test database. The normal unit and Emulator suites do not need these secrets and never connect to this live database.
GitLab CI
Set FIREBASE_DATABASE_URL and FIREBASE_SERVICE_ACCOUNT_KEY as protected, masked CI/CD variables. Keep the service account variable as the raw JSON content.
migrate:
stage: deploy
image: node:22
before_script:
- npm ci
- printf '%s' "$FIREBASE_SERVICE_ACCOUNT_KEY" > "$CI_PROJECT_DIR/firebase-key.json"
- export DATABASE_URL="$FIREBASE_DATABASE_URL"
- export GOOGLE_APPLICATION_CREDENTIALS="$CI_PROJECT_DIR/firebase-key.json"
script:
- npx msr-firebase validate
- >-
npx msr-firebase migrate
--backup-mode full
after_script:
- rm -f "$CI_PROJECT_DIR/firebase-key.json"
rules:
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
when: manual
CircleCI
Store DATABASE_URL and FIREBASE_SERVICE_ACCOUNT_KEY as CircleCI project or context environment variables. CircleCI provides DATABASE_URL directly to each step; the service-account JSON is written to a temporary file, and environment sets the path the adapter reads.
version: 2.1
jobs:
migrate:
docker:
- image: cimg/node:22.15
steps:
- checkout
- run:
name: Install dependencies
command: npm ci
- run:
name: Write service account key
command: printf '%s' "$FIREBASE_SERVICE_ACCOUNT_KEY" > /tmp/firebase-key.json
- run:
name: Validate migrations
command: npx msr-firebase validate
environment:
GOOGLE_APPLICATION_CREDENTIALS: /tmp/firebase-key.json
- run:
name: Run migrations
command: npx msr-firebase migrate --backup-mode full
environment:
GOOGLE_APPLICATION_CREDENTIALS: /tmp/firebase-key.json
- run:
name: Remove service account key
command: rm -f /tmp/firebase-key.json
when: always
workflows:
deploy:
jobs:
- migrate:
filters:
branches:
only: main
Docker
The container receives the database URL and a read-only mounted credential file at runtime. Do not bake service account keys into the image.
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY migrations ./migrations
CMD ["sh", "-c", "npx msr-firebase migrate --backup-mode full"]
docker build -t msr-firebase-migrations .
docker run --rm \
--env DATABASE_URL="$FIREBASE_DATABASE_URL" \
--env GOOGLE_APPLICATION_CREDENTIALS=/run/secrets/firebase.json \
--volume "$PWD/firebase-key.json:/run/secrets/firebase.json:ro" \
msr-firebase-migrations
Kubernetes
Create the service account key as a Kubernetes Secret and provide its mounted file path to the job. Set the database URL to the exact Realtime Database URL shown in Firebase Console; URL formats depend on the database’s region.
apiVersion: batch/v1
kind: Job
metadata:
name: firebase-migrations
spec:
backoffLimit: 1
template:
spec:
restartPolicy: Never
containers:
- name: migrations
image: your-registry/msr-firebase-migrations:latest
command: ["npx", "msr-firebase", "migrate", "--backup-mode", "full"]
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: firebase-secrets
key: database-url
- name: GOOGLE_APPLICATION_CREDENTIALS
value: /secrets/firebase-key.json
volumeMounts:
- name: firebase-credentials
mountPath: /secrets
readOnly: true
volumes:
- name: firebase-credentials
secret:
secretName: firebase-service-account
items:
- key: service-account.json
path: firebase-key.json
Deployment Notes
- Run
validatebefore applying migrations. - Prefer a single migration job per deployment and configure Core locking when concurrent deployments are possible.
- Keep development, staging, and production Firebase projects separate where possible.
- Use
shiftonly when intentionally sharing one database between isolated environments. - Read Backup & Restore before choosing backup modes or recovering data.
- Never print credentials or commit service account files.