Skip to content

Commit

Permalink
Deploy functions yml to be run from stg branch
Browse files Browse the repository at this point in the history
  • Loading branch information
yevkim committed Nov 27, 2024
1 parent 7731e94 commit b682dd0
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 46 deletions.
104 changes: 65 additions & 39 deletions .github/workflows/deploy_functions.yml
Original file line number Diff line number Diff line change
@@ -1,49 +1,75 @@
name: Deploy to Firebase Functions
name: Deploy to DEV

on:
push:
branches:
- stg
workflow_dispatch:

env:
FIREBASE_PROJECT_ID: "schemessg-v3-dev"
SERVICE_ACCOUNT_KEY: ${{ secrets.FIREBASE_SERVICE_ACCOUNT_SCHEMESSG_V3_DEV }}

jobs:
deploy:
name: Deploy Functions
build_and_deploy:
runs-on: ubuntu-latest

steps:
# Step 1: Checkout the code
- name: Checkout Repository
uses: actions/checkout@v3

# Step 2: Set up Node.js environment
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18

# Step 3: Install Dependencies
- name: Install Dependencies
run: |
npm install -g [email protected]
# Step 4: Authenticate Firebase CLI
- name: Authenticate Firebase CLI
env:
FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN_SCHEMES_V3_DEV }}
run: |
echo "$SERVICE_ACCOUNT_KEY" > ${HOME}/serviceAccountKey.json
firebase auth:login:ci --project $FIREBASE_PROJECT_ID --token $FIREBASE_TOKEN
firebase use --add $FIREBASE_PROJECT_ID --token $FIREBASE_TOKEN
# Step 5: Deploy All Functions
- name: Deploy Firebase Functions
run: |
firebase deploy --only functions --project $FIREBASE_PROJECT_ID
env:
FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
GOOGLE_APPLICATION_CREDENTIALS: ${HOME}/serviceAccountKey.json
# Step 1: Checkout the code
- uses: actions/checkout@v3

# Step 1.5: Environment variables
- name: create env file
working-directory: backend
run: |
cd functions
touch .env
echo "${{ secrets.FUNCTIONS_ENV_VARS }}" >> .env
# Step 2: Set up Python environment
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'

# Step 3: Set up Node.js environment
- name: Set up Node
uses: actions/setup-node@v3
with:
node-version: 19.7.0

# Step 3.5: Download the ZIP file from Google Drive
- name: Download Models ZIP
run: |
wget "${{ secrets.LINK_TO_MODEL_FILES }}" -O models.zip
- name: List Directory Contents
run: ls -lh models.zip
- name: Check File Type
run: file models.zip

# Step 3.5.2: Extract the ZIP file
- name: Extract Models ZIP
run: |
unzip models.zip -d backend/functions/ml_logic
# Step 3.5.3: Clean up
- name: Clean Up
run: rm models.zip

# Step 4: Create Service Account Key and Set Env Variables
- name: Create SA key and set env variables
run: |
echo '${{ secrets.FIREBASE_SERVICE_ACCOUNT_SCHEMESSG_V3_DEV }}' > ${{ runner.temp }}/gcloud.json
echo "GOOGLE_APPLICATION_CREDENTIALS=${{ runner.temp }}/gcloud.json" >> $GITHUB_ENV
# Step 5: Prepare the environment
- name: Prepare the environment
working-directory: backend/functions
run: |
python3.10 -m venv venv
. venv/bin/activate
npx firebase-tools --version
python3.10 -m pip install -r requirements.txt
# Step 6: deploy functions
- name: Deploy functions
working-directory: backend
env:
GOOGLE_APPLICATION_CREDENTIALS: ${{ runner.temp }}/gcloud.json
run: npx firebase-tools deploy --only functions --debug --token "${{ secrets.FIREBASE_TOKEN_SCHEMESSG_V3_DEV }}"
6 changes: 6 additions & 0 deletions backend/functions/fb_manager/firebaseManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ def _initialize_app(self):
if not firebase_admin._apps:
# Replace newlines in private key
private_key = os.getenv("FB_PRIVATE_KEY").replace("\\n", "\n")
# path_to_creds = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")

# if not path_to_creds or not os.path.exists(path_to_creds):
# raise ValueError(f"Invalid certificate path: {path_to_creds}. Make sure GOOGLE_APPLICATION_CREDENTIALS is set properly.")

# cred = credentials.Certificate(path_to_creds) # Adjust path as needed

cred = credentials.Certificate(
{
Expand Down
36 changes: 29 additions & 7 deletions backend/functions/feedback/feedback.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

from fb_manager.firebaseManager import FirebaseManager
from firebase_functions import https_fn
from datetime import timezone
import datetime
from datetime import datetime, timezone
import json

# Firestore client
firebase_manager = FirebaseManager()
Expand All @@ -22,9 +22,16 @@ def feedback(req: https_fn.Request) -> https_fn.Response:
Returns:
https_fn.Response: response sent to client
"""

headers = {
'Access-Control-Allow-Origin': 'http://localhost:3000'
}
if req.method != "POST":
return https_fn.Response("Only POST requests are allowed.", status=405)
return https_fn.Response(
response=json.dumps({"success": False, "message": "Only POST requests are allowed"}),
status=405,
mimetype="application/json",
headers=headers
)

try:
# Parse the request data
Expand All @@ -35,7 +42,12 @@ def feedback(req: https_fn.Request) -> https_fn.Response:
timestamp = datetime.datetime.now(timezone.utc)

if not feedback_text or not timestamp:
return {"success": False, "message": "Missing required fields."}, 400
return https_fn.Response(
response=json.dumps({"success": False, "message": "Missing required fields"}),
status=400,
mimetype="application/json",
headers=headers
)

# Prepare the data for Firestore
feedback_data = {
Expand All @@ -49,8 +61,18 @@ def feedback(req: https_fn.Request) -> https_fn.Response:
firebase_manager.firestore_client.collection("userFeedback").add(feedback_data)

# Return a success response
return {"success": True, "message": "Feedback successfully added."}, 200
return https_fn.Response(
response=json.dumps({"success": True, "message": "Feedback successfully added"}),
status=200,
mimetype="application/json",
headers=headers
)

except Exception as e:
print(f"Error: {e}")
return {"success": False, "message": "Failed to add feedback."}, 500
return https_fn.Response(
response=json.dumps({"success": False, "message": "Failed to add feedback"}),
status=500,
mimetype="application/json",
headers=headers
)

0 comments on commit b682dd0

Please sign in to comment.