Skip to content

Commit 9caac15

Browse files
committed
Move backend from VM to Cloud Run and remove Load Balancer
Both to save lots of $$$
1 parent 0d5f1cd commit 9caac15

6 files changed

Lines changed: 143 additions & 342 deletions

File tree

.github/workflows/cd-api.yml

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: API Release
22
on:
33
push:
4-
branches: [main, master]
4+
branches: [ main, master ]
55
paths:
66
- 'backend/api/package.json'
77
- '.github/workflows/cd-api.yml'
@@ -70,17 +70,6 @@ jobs:
7070
- name: Configure Docker for Artifact Registry
7171
run: gcloud auth configure-docker us-west1-docker.pkg.dev --quiet
7272

73-
- name: Install Tofu (Terraform)
74-
run: |
75-
LATEST=https://github.com/opentofu/opentofu/releases/download/v1.10.5/tofu_1.10.5_linux_amd64.zip
76-
curl -LO "$LATEST"
77-
unzip -o tofu_*_linux_amd64.zip
78-
sudo mv tofu /usr/local/bin/
79-
rm tofu_*_linux_amd64.zip
80-
echo "OpenTofu version: $(tofu version)"
81-
cd backend/api
82-
tofu init
83-
8473
- name: Run deploy script
8574
run: |
8675
chmod +x backend/api/deploy-api.sh

backend/api/Dockerfile

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ FROM node:20-alpine
44
WORKDIR /usr/src/app
55

66
# Install PM2 globally
7-
RUN yarn global add pm2
7+
# RUN yarn global add pm2
88

99
# Fet dependencies in for efficient docker layering
1010
COPY dist/package.json dist/yarn.lock ./
@@ -21,13 +21,15 @@ RUN npm list || true
2121
COPY dist ./
2222

2323
# Copy the PM2 ecosystem configuration
24-
COPY ecosystem.config.js ./
24+
# COPY ecosystem.config.js ./
2525

26-
ENV PORT=80
27-
EXPOSE 80/tcp
26+
#ENV PORT=80
27+
#EXPOSE 80/tcp
2828
# EXPOSE 8090/tcp
2929
# EXPOSE 8091/tcp
3030
# EXPOSE 8092/tcp
3131

32-
# Use PM2 to run the application with the ecosystem config
33-
CMD ["pm2-runtime", "ecosystem.config.js"]
32+
# Use PM2 to run the application with the ecosystem config (was only for VM, not cloud run)
33+
#CMD ["pm2-runtime", "ecosystem.config.js"]
34+
35+
CMD ["node", "-r", "tsconfig-paths/register", "backend/api/lib/serve.js"]

backend/api/deploy-api.sh

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
11
#!/bin/bash
2-
3-
# steps to deploy new version to GCP:
4-
# 1. build new docker image & upload to Google
5-
# 2. create a new GCP instance template with the new docker image
6-
# 3. tell the GCP 'backend service' for the API to update to the new template
7-
# 4. a. GCP creates a new instance with the new template
8-
# b. wait for the new instance to be healthy (serving TCP connections)
9-
# c. route new connections to the new instance
10-
# d. delete the old instance
11-
122
set -e
133

144
cd "$(dirname "$0")"
@@ -45,28 +35,21 @@ TIMESTAMP=$(date +"%s")
4535
IMAGE_TAG="${TIMESTAMP}-${GIT_REVISION}"
4636
IMAGE_URL="${REGION}-docker.pkg.dev/${PROJECT}/builds/${SERVICE_NAME}:${IMAGE_TAG}"
4737

48-
echo "🚀 Deploying ${SERVICE_NAME} to ${ENV} ($(date "+%Y-%m-%d %I:%M:%S %p"))"
38+
echo "🚀 Building & Pushing Image..."
4939
yarn build
50-
51-
gcloud auth print-access-token | docker login -u oauth2accesstoken --password-stdin us-west1-docker.pkg.dev
52-
docker build . --tag ${IMAGE_URL} --platform linux/amd64 --progress=plain
53-
echo "docker push ${IMAGE_URL}"
40+
gcloud auth print-access-token | docker login -u oauth2accesstoken --password-stdin ${REGION}-docker.pkg.dev
41+
gcloud auth configure-docker ${REGION}-docker.pkg.dev --quiet
42+
docker build . --tag ${IMAGE_URL} --platform linux/amd64
5443
docker push ${IMAGE_URL}
5544

56-
export TF_VAR_image_url=$IMAGE_URL
57-
export TF_VAR_env=$ENV
58-
tofu apply -auto-approve
59-
60-
#INSTANCE_NAME=$(gcloud compute instances list \
61-
# --filter="zone:(us-west1-c)" \
62-
# --sort-by="~creationTimestamp" \
63-
# --format="value(name)" \
64-
# --limit=1)
65-
#SERVICE_ACCOUNT_EMAIL=$(gcloud compute instances describe ${INSTANCE_NAME} \
66-
# --zone us-west1-c \
67-
# --format="value(serviceAccounts.email)")
68-
#gcloud projects add-iam-policy-binding ${PROJECT} \
69-
# --member="serviceAccount:$SERVICE_ACCOUNT_EMAIL" \
70-
# --role="roles/artifactregistry.reader"
71-
72-
echo "✅ Deployment complete! Image: ${IMAGE_URL}"
45+
# Update Cloud Run (The fast way)
46+
# This keeps all the Terraform-defined settings (env vars, memory, etc.)
47+
# but simply swaps the container image.
48+
gcloud run deploy ${SERVICE_NAME} \
49+
--image ${IMAGE_URL} \
50+
--region ${REGION} \
51+
--platform managed \
52+
--quiet
53+
54+
echo "Custom Domain: https://api.compassmeet.com"
55+
echo "✅ Code updated on Cloud Run!"

backend/api/deploy-init-api.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/bash
2+
set -e
3+
4+
cd "$(dirname "$0")"
5+
6+
source ../../.env
7+
8+
ENV=${1:-prod}
9+
10+
# Config
11+
REGION="us-west1"
12+
ZONE="us-west1-b"
13+
14+
PROJECT="compass-130ba"
15+
SERVICE_NAME="api"
16+
17+
GIT_REVISION=$(git rev-parse --short HEAD)
18+
GIT_COMMIT_DATE=$(git log -1 --format=%ci)
19+
GIT_COMMIT_AUTHOR=$(git log -1 --format='%an')
20+
GIT_COMMIT_MESSAGE=$(git log -1 --format='%s')
21+
echo "Git commit message: ${GIT_COMMIT_MESSAGE}"
22+
23+
cat > metadata.json << EOF
24+
{
25+
"git": {
26+
"revision": "${GIT_REVISION}",
27+
"commitDate": "${GIT_COMMIT_DATE}",
28+
"author": "${GIT_COMMIT_AUTHOR}",
29+
"message": "${GIT_COMMIT_MESSAGE}"
30+
}
31+
}
32+
EOF
33+
34+
TIMESTAMP=$(date +"%s")
35+
IMAGE_TAG="${TIMESTAMP}-${GIT_REVISION}"
36+
IMAGE_URL="${REGION}-docker.pkg.dev/${PROJECT}/builds/${SERVICE_NAME}:${IMAGE_TAG}"
37+
38+
echo "🚀 Building & Pushing Image..."
39+
yarn build
40+
gcloud auth print-access-token | docker login -u oauth2accesstoken --password-stdin ${REGION}-docker.pkg.dev
41+
docker build . --tag ${IMAGE_URL} --platform linux/amd64
42+
docker push ${IMAGE_URL}
43+
44+
echo "Infrastructure Update..."
45+
export TF_VAR_image_url=$IMAGE_URL
46+
export TF_VAR_env=$ENV
47+
tofu apply -auto-approve
48+
49+
# Get the new URL just in case
50+
SERVICE_URL=$(gcloud run services describe ${SERVICE_NAME} --platform managed --region ${REGION} --format 'value(status.url)')
51+
52+
echo "✅ Deployed to Cloud Run!"
53+
echo "Service URL: ${SERVICE_URL}"
54+
echo "Custom Domain: https://api.compassmeet.com"

0 commit comments

Comments
 (0)