Skip to content

Commit b7a1332

Browse files
committed
add production workflows
1 parent 8786dfa commit b7a1332

4 files changed

Lines changed: 216 additions & 1 deletion

File tree

.github/workflows/prod.yml

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
---
2+
name: Prod
3+
4+
on:
5+
release:
6+
types:
7+
- released
8+
- prereleased
9+
- ch-add-releases-workflow
10+
11+
jobs:
12+
build:
13+
outputs:
14+
image: ${{ steps.export.outputs.image }}
15+
tag: ${{ steps.export.outputs.tag }}
16+
17+
runs-on: ubuntu-latest
18+
19+
steps:
20+
- name: Checkout
21+
uses: actions/checkout@v4
22+
with:
23+
ref: staging
24+
25+
- name: Install (Buildx)
26+
uses: docker/setup-buildx-action@v3
27+
28+
- name: Login (GCP)
29+
uses: google-github-actions/auth@v2
30+
with:
31+
credentials_json: ${{ secrets.CREDENTIALS_JSON }}
32+
33+
- name: Install (Gcloud)
34+
uses: google-github-actions/setup-gcloud@v1
35+
with:
36+
project_id: crane-cloud-274413
37+
install_components: "gke-gcloud-auth-plugin"
38+
39+
- name: Login (GCR)
40+
run: gcloud auth configure-docker
41+
42+
- id: meta
43+
name: Tag
44+
uses: docker/metadata-action@v3
45+
with:
46+
flavor: |
47+
latest=true
48+
images: gcr.io/crane-cloud-274413/database-api
49+
tags: |
50+
type=ref,event=branch
51+
type=ref,event=pr
52+
type=semver,pattern={{version}}
53+
type=semver,pattern={{major}}.{{minor}}
54+
type=sha
55+
56+
- name: Build
57+
uses: docker/build-push-action@v2
58+
with:
59+
cache-from: type=gha
60+
cache-to: type=gha,mode=max
61+
context: .
62+
file: docker/prod/Dockerfile
63+
labels: ${{ steps.meta.outputs.labels }}
64+
push: true
65+
tags: ${{ steps.meta.outputs.tags }}
66+
67+
- id: export
68+
name: Export
69+
uses: actions/github-script@v5
70+
with:
71+
script: |
72+
const metadata = JSON.parse(`${{ steps.meta.outputs.json }}`)
73+
const fullUrl = metadata.tags.find((t) => t.includes(':sha-'))
74+
if (fullUrl == null) {
75+
core.error('Unable to find sha tag of image')
76+
} else {
77+
const tag = fullUrl.split(':')[1]
78+
core.setOutput('image', fullUrl)
79+
core.setOutput('tag', tag)
80+
}
81+
82+
Production:
83+
name: Deploy (Production)
84+
85+
needs:
86+
- build
87+
88+
runs-on: ubuntu-latest
89+
90+
env:
91+
namespace: cranecloud-prod
92+
93+
steps:
94+
- name: Clone
95+
uses: actions/checkout@v2
96+
97+
- name: Login (GCP)
98+
uses: google-github-actions/auth@v2
99+
with:
100+
credentials_json: ${{ secrets.CREDENTIALS_JSON }}
101+
102+
- name: Install (Gcloud)
103+
uses: google-github-actions/setup-gcloud@v1
104+
with:
105+
project_id: crane-cloud-274413
106+
install_components: "gke-gcloud-auth-plugin"
107+
108+
- name: Login (Kubernetes Cluster)
109+
uses: google-github-actions/get-gke-credentials@v1
110+
with:
111+
cluster_name: staging-cluster
112+
location: us-central1-a
113+
project_id: crane-cloud-274413
114+
115+
- name: Add Repo (cranecloud)
116+
run: |
117+
helm repo add cranecloud https://crane-cloud.github.io/helm-charts/
118+
119+
- name: Helm Release
120+
run: |
121+
helm upgrade --install \
122+
database-api cranecloud/cranecloud \
123+
--values helm/values.prod.yaml \
124+
--namespace $namespace \
125+
--set image.tag="${{ needs.build.outputs.tag }}" \
126+
--set environment.DATABASE_URI="${{ secrets.PRODUCTION_DATABASE_URI }}" \
127+
--set environment.JWT_SALT="${{ secrets.PRODUCTION_JWT_SALT }}" \
128+
--set environment.ACTIVITY_LOGGER_URL="${{ secrets.PRODUCTION_ACTIVITY_LOGGER_URL }}" \
129+
--set environment.ADMIN_MYSQL_USER="${{ secrets.PRODUCTION_ADMIN_MYSQL_USER }}" \
130+
--set environment.ADMIN_MYSQL_PASSWORD="${{ secrets.PRODUCTION_ADMIN_MYSQL_PASSWORD }}" \
131+
--set environment.ADMIN_MYSQL_HOST="${{ secrets.PRODUCTION_ADMIN_MYSQL_HOST }}" \
132+
--set environment.ADMIN_MYSQL_PORT="${{ secrets.PRODUCTION_ADMIN_MYSQL_PORT }}" \
133+
--set environment.ADMIN_PSQL_USER="${{ secrets.PRODUCTION_ADMIN_PSQL_USER }}" \
134+
--set environment.ADMIN_PSQL_PASSWORD="${{ secrets.PRODUCTION_ADMIN_PSQL_PASSWORD }}" \
135+
--set environment.ADMIN_PSQL_HOST="${{ secrets.PRODUCTION_ADMIN_PSQL_HOST }}" \
136+
--set environment.ADMIN_PSQL_PORT="${{ secrets.PRODUCTION_ADMIN_PSQL_PORT }}" \
137+
--set environment.MAIL_PASSWORD="${{ secrets.PRODUCTION_MAIL_PASSWORD }}" \
138+
--timeout=300s
139+
140+
- name: Monitor Rollout
141+
run: |
142+
kubectl rollout status deployment/database-api --timeout=300s --namespace $namespace

helm/values.prod.yaml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
replicaCount: 1
2+
3+
image:
4+
repository: gcr.io/crane-cloud-274413/database-api
5+
pullPolicy: Always
6+
tag: ${{ DOCKER_IMAGE_TAG }}
7+
8+
nameOverride: "database-api"
9+
10+
service:
11+
type: NodePort
12+
port: 80
13+
14+
nginxConf:
15+
server {
16+
listen 80;
17+
18+
location / {
19+
proxy_pass http://localhost:8000/;
20+
}
21+
}
22+
23+
port: 8000
24+
25+
environment:
26+
FASTAPI_ENV: production
27+
DATABASE_URI: ${{ DATABASE_URI }}
28+
JWT_SALT: ${{ JWT_SALT }}
29+
ACTIVITY_LOGGER_URL: ${{ ACTIVITY_LOGGER_URL }}
30+
ADMIN_MYSQL_USER: ${{ ADMIN_MYSQL_USER }}
31+
ADMIN_MYSQL_PASSWORD: ${{ ADMIN_MYSQL_PASSWORD }}
32+
ADMIN_MYSQL_HOST: ${{ ADMIN_MYSQL_HOST }}
33+
ADMIN_MYSQL_PORT: ${{ ADMIN_MYSQL_PORT }}
34+
ADMIN_PSQL_USER: ${{ ADMIN_PSQL_USER }}
35+
ADMIN_PSQL_PASSWORD: ${{ ADMIN_PSQL_PASSWORD }}
36+
ADMIN_PSQL_HOST: ${{ ADMIN_PSQL_HOST }}
37+
ADMIN_PSQL_PORT: ${{ ADMIN_PSQL_PORT }}
38+
MAIL_PASSWORD: ${{ MAIL_PASSWORD }}
39+
MAIL_USERNAME: no-reply@cranecloud.io
40+
41+
42+
celery:
43+
create: true
44+
command:
45+
- "poetry"
46+
- "run"
47+
- "celery"
48+
- "-A"
49+
- "main.celery"
50+
- "worker"
51+
- "--loglevel=info"
52+
port: 5001

helm/values.staging.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ nginxConf:
5353
proxy_pass http://localhost:8000/;
5454
}
5555
}
56+
port: 8000
5657

5758
environment:
5859
FASTAPI_ENV: production
@@ -67,8 +68,19 @@ environment:
6768
ADMIN_PSQL_PASSWORD: ${{ ADMIN_PSQL_PASSWORD }}
6869
ADMIN_PSQL_HOST: ${{ ADMIN_PSQL_HOST }}
6970
ADMIN_PSQL_PORT: ${{ ADMIN_PSQL_PORT }}
71+
MAIL_PASSWORD: ${{ MAIL_PASSWORD }}
72+
MAIL_USERNAME: no-reply@cranecloud.io
7073

7174

7275

7376
celery:
74-
create: false
77+
create: true
78+
command:
79+
- "poetry"
80+
- "run"
81+
- "celery"
82+
- "-A"
83+
- "main.celery"
84+
- "worker"
85+
- "--loglevel=info"
86+
port: 5001

scripts/start-prod.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#! /bin/bash
2+
3+
4+
# apply migrations onto db
5+
poetry run alembic upgrade head
6+
7+
# start server
8+
poetry run uvicorn main:app --host 0.0.0.0 --port 8000 --reload
9+
# NEW_RELIC_CONFIG_FILE=newrelic.ini newrelic-admin run-program gunicorn --worker-tmp-dir /dev/shm --workers=4 --bind 0.0.0.0:8000 --timeout 240 main:app

0 commit comments

Comments
 (0)