forked from kprakhar27/InboxAI
-
Notifications
You must be signed in to change notification settings - Fork 0
137 lines (120 loc) · 4.05 KB
/
Copy pathbackend-deploy.yml
File metadata and controls
137 lines (120 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
name: Deploy to Cloud Run
on:
push:
branches: ["main"]
paths: ["backend/**"]
workflow_dispatch:
env:
PROJECT_ID: inboxai-449918
REGION: us-central1
REPOSITORY: flask-repo
IMAGE_NAME: flask-app
SERVICE: flask-api
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Create dynamic credentials.json file
uses: jsdaniell/create-json@v1.2.3
with:
name: "credentials.json"
json: ${{ secrets.GOOGLE_CREDENTIALS }}
dir: "backend/"
- name: Create dynamic google_service_account.json file
uses: jsdaniell/create-json@v1.2.3
with:
name: "google_sa.json"
json: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
dir: "backend/"
- name: Create .env file
run: |
touch backend/.env
echo "${{ secrets.BACKEND_ENV }}" > backend/.env
cat backend/.env
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install dependencies for tests
run: |
python -m pip install --upgrade pip
pip install -r backend/requirements.txt
pip install pytest pytest-cov
- name: Run pre-deployment tests
run: |
cd backend
pytest
- name: Authenticate with GCP
uses: google-github-actions/auth@v2
with:
credentials_json: '${{ secrets.GCP_SA_KEY }}'
- name: Set up gcloud SDK
uses: google-github-actions/setup-gcloud@v1
with:
project_id: ${{ env.PROJECT_ID }}
- name: Authenticate Docker to Artifact Registry
run: |
gcloud auth configure-docker $REGION-docker.pkg.dev --quiet
- name: Build and Push Docker Image
run: |
IMAGE="$REGION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY/$IMAGE_NAME"
docker build -t $IMAGE backend/.
docker push $IMAGE
- name: Deploy to Cloud Run
run: |
IMAGE="$REGION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY/$IMAGE_NAME"
gcloud run deploy $SERVICE \
--image=$IMAGE \
--platform=managed \
--region=$REGION \
--allow-unauthenticated
- name: Post-deployment healthcheck
run: |
URL=$(gcloud run services describe $SERVICE \
--platform=managed \
--region=$REGION \
--format='value(status.url)')
echo "Checking $URL/auth..."
curl --fail --retry 3 --retry-delay 5 "$URL/auth" || exit 1
notify:
runs-on: ubuntu-latest
if: always()
needs: deploy
env:
SMTP__SMTP_HOST: smtp.gmail.com
SMTP__SMTP_PORT: 465
SMTP__SMTP_USER: pc612001@gmail.com
SMTP__SMTP_PASSWORD: ${{ secrets.SMTP_PASSWORD }}
SMTP__SMTP_MAIL_FROM: pc612001@gmail.com
ALERT_EMAIL: choudhari.pra@northeastern.edu
steps:
- name: Notify Success
if: success()
uses: dawidd6/action-send-mail@v4
with:
server_address: ${{ env.SMTP__SMTP_HOST }}
server_port: ${{ env.SMTP__SMTP_PORT }}
username: ${{ env.SMTP__SMTP_USER }}
password: ${{ env.SMTP__SMTP_PASSWORD }}
subject: "✅ Cloud Run Deployment Successful"
to: ${{ env.ALERT_EMAIL }}
from: ${{ env.SMTP__SMTP_MAIL_FROM }}
secure: true
body: "Flask backend has been successfully deployed via Cloud Run."
- name: Notify Failure
if: failure()
uses: dawidd6/action-send-mail@v4
with:
server_address: ${{ env.SMTP__SMTP_HOST }}
server_port: ${{ env.SMTP__SMTP_PORT }}
username: ${{ env.SMTP__SMTP_USER }}
password: ${{ env.SMTP__SMTP_PASSWORD }}
subject: "❌ Cloud Run Deployment Failed"
to: ${{ env.ALERT_EMAIL }}
from: ${{ env.SMTP__SMTP_MAIL_FROM }}
secure: true
body: "Deployment failed. Please check GitHub Actions logs for more details."