From 9e9510206ad3eb929df36b7fafa50caa13b5c3e0 Mon Sep 17 00:00:00 2001 From: Brendan Date: Fri, 20 Dec 2024 11:35:37 +1100 Subject: [PATCH 1/4] deploy commit --- requirements.txt | 3 ++- startup.txt | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 startup.txt diff --git a/requirements.txt b/requirements.txt index 01307ad..82f7d72 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ Flask==3.0.0 -python-dateutil==2.8.2 \ No newline at end of file +python-dateutil==2.8.2 +gunicorn==21.2.0 \ No newline at end of file diff --git a/startup.txt b/startup.txt new file mode 100644 index 0000000..bf62770 --- /dev/null +++ b/startup.txt @@ -0,0 +1 @@ +gunicorn --bind=0.0.0.0 --timeout 600 app:app \ No newline at end of file From 3c20c4eefa2c76c2ca31b099cd8d72aea33fe09f Mon Sep 17 00:00:00 2001 From: Brendan Date: Fri, 20 Dec 2024 00:37:47 +0000 Subject: [PATCH 2/4] Add or update the Azure App Service build and deployment workflow config --- .github/workflows/deploy_emoji-caller.yml | 78 +++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 .github/workflows/deploy_emoji-caller.yml diff --git a/.github/workflows/deploy_emoji-caller.yml b/.github/workflows/deploy_emoji-caller.yml new file mode 100644 index 0000000..d6c7900 --- /dev/null +++ b/.github/workflows/deploy_emoji-caller.yml @@ -0,0 +1,78 @@ +# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy +# More GitHub Actions for Azure: https://github.com/Azure/actions +# More info on Python, GitHub Actions, and Azure App Service: https://aka.ms/python-webapps-actions + +name: Build and deploy Python app to Azure Web App - emoji-caller + +on: + push: + branches: + - deploy + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Set up Python version + uses: actions/setup-python@v5 + with: + python-version: '3.12' + + - name: Create and start virtual environment + run: | + python -m venv venv + source venv/bin/activate + + - name: Install dependencies + run: pip install -r requirements.txt + + # Optional: Add step to run tests here (PyTest, Django test suites, etc.) + + - name: Zip artifact for deployment + run: zip release.zip ./* -r + + - name: Upload artifact for deployment jobs + uses: actions/upload-artifact@v4 + with: + name: python-app + path: | + release.zip + !venv/ + + deploy: + runs-on: ubuntu-latest + needs: build + environment: + name: 'Production' + url: ${{ steps.deploy-to-webapp.outputs.webapp-url }} + permissions: + id-token: write #This is required for requesting the JWT + + steps: + - name: Download artifact from build job + uses: actions/download-artifact@v4 + with: + name: python-app + + - name: Unzip artifact for deployment + run: unzip release.zip + + + - name: Login to Azure + uses: azure/login@v2 + with: + client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_971F3F46F7FC456E8D4597FC800C658C }} + tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_0E984A4F42AF487BA0D671B798A6E912 }} + subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_CFF2F24C3CF64DC9B73A22E137C47DD7 }} + + - name: 'Deploy to Azure Web App' + uses: azure/webapps-deploy@v3 + id: deploy-to-webapp + with: + app-name: 'emoji-caller' + slot-name: 'Production' + \ No newline at end of file From 38126859628e1478cf3d418cef22fe6228fc3b22 Mon Sep 17 00:00:00 2001 From: Brendan Date: Fri, 20 Dec 2024 11:52:21 +1100 Subject: [PATCH 3/4] fixed db issue for az --- app.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index 1a5f9a2..1c22187 100644 --- a/app.py +++ b/app.py @@ -1,12 +1,14 @@ from flask import Flask, render_template, jsonify import sqlite3 from datetime import datetime +import os + app = Flask(__name__) -# Database initialization +DB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'emoji_stats.db')# Database initialization def init_db(): - conn = sqlite3.connect('emoji_stats.db') + conn = sqlite3.connect(DB_PATH) c = conn.cursor() # Create table for emoji usage counts c.execute(''' @@ -34,7 +36,7 @@ def init_db(): conn.close() def get_emoji_counts(): - conn = sqlite3.connect('emoji_stats.db') + conn = sqlite3.connect(DB_PATH) c = conn.cursor() c.execute('SELECT emoji_name, copy_count + api_count as total_count FROM emoji_counts') counts = {row[0]: row[1] for row in c.fetchall()} @@ -42,7 +44,7 @@ def get_emoji_counts(): return counts def increment_count(emoji_name, usage_type): - conn = sqlite3.connect('emoji_stats.db') + conn = sqlite3.connect(DB_PATH) c = conn.cursor() if usage_type == 'copy': c.execute('UPDATE emoji_counts SET copy_count = copy_count + 1, last_used = ? WHERE emoji_name = ?', @@ -239,7 +241,7 @@ def increment_emoji_count(emoji_name): @app.route('/api/stats') def get_stats(): - conn = sqlite3.connect('emoji_stats.db') + conn = sqlite3.connect(DB_PATH) c = conn.cursor() c.execute(''' SELECT From 4f17a31afca857be36a79d3243ba40df5a76ba24 Mon Sep 17 00:00:00 2001 From: Brendan Date: Fri, 20 Dec 2024 12:02:20 +1100 Subject: [PATCH 4/4] fixed data directory do deployment --- app.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 1c22187..0011b45 100644 --- a/app.py +++ b/app.py @@ -6,7 +6,16 @@ app = Flask(__name__) -DB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'emoji_stats.db')# Database initialization +# Use Azure's writable directory if available, otherwise use local path +if 'WEBSITE_SITE_NAME' in os.environ: + DB_PATH = os.path.join('/home/data', 'emoji_stats.db') +else: + DB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'emoji_stats.db') + +# Ensure the directory exists +os.makedirs(os.path.dirname(DB_PATH), exist_ok=True) + +# Database initialization def init_db(): conn = sqlite3.connect(DB_PATH) c = conn.cursor()