Skip to content

Commit a4eed61

Browse files
authored
Merge pull request #18 from ksiuwr/bartacc/ci_config
Add CI config for Google Cloud Run deployment
2 parents 7e25690 + 2a3240e commit a4eed61

File tree

6 files changed

+40
-35
lines changed

6 files changed

+40
-35
lines changed

.circleci/config.yml

+8-27
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version: 2.1
33
jobs:
44
run_tests:
55
docker:
6-
- image: node:18-bookworm
6+
- image: node:22-bookworm
77
- image: postgres
88
environment:
99
POSTGRES_DB: "zosia"
@@ -35,35 +35,17 @@ jobs:
3535

3636
deploy:
3737
docker:
38-
- image: node:18-bookworm
38+
- image: cimg/gcp:2024.08
3939
steps:
40-
- run:
41-
name: Install gcloud and python-pip
42-
command: |
43-
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y curl apt-transport-https ca-certificates gnupg
44-
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
45-
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -
46-
apt-get update
47-
48-
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y python3-pip python3-dev google-cloud-sdk
49-
echo ${GCLOUD_KEY} | base64 --decode | gcloud auth activate-service-account --key-file=-
50-
gcloud --quiet config set project ${GCLOUD_PROJECT}
51-
gcloud --quiet config set compute/zone europe_central2
5240
- checkout
41+
- setup_remote_docker
5342
- run:
54-
name: Build frontend
43+
name: Run deploy.sh script
5544
command: |
56-
(cd ./app && npm install)
57-
- run:
58-
name: Collect static files
59-
# we could prepare separate setting files for this without dependencies and with different STATIC_ROOT
60-
command: |
61-
python3 -m pip install --break-system-packages -r ./app/requirements.txt
62-
cd ./app/server && python3 ../manage.py collectstatic --no-input && cp -r /static/. ../static/ && python3 ../manage.py generate_client_assets
45+
echo ${GCP_PROJECT_KEY_BASE64_ENCODED} | base64 --decode --ignore-garbage > $HOME/gcloud-service-key.json
46+
gcloud auth activate-service-account --key-file=$HOME/gcloud-service-key.json
6347
64-
- run:
65-
name: Push image to App Engine
66-
command: (cd ./app && gcloud app deploy app.yaml)
48+
cd ./app && ./deploy.sh ${GCP_PROJECT_ID}
6749
# - run:
6850
# name: Purge cache
6951
# command: |
@@ -83,5 +65,4 @@ workflows:
8365
filters:
8466
branches:
8567
only:
86-
# - master
87-
- zosia_2025
68+
- master

app/Dockerfile

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM nikolaik/python-nodejs:python3.10-nodejs18-alpine AS base
1+
FROM nikolaik/python-nodejs:python3.12-nodejs22-alpine AS base
22

33
# Env variables
44
ENV PYTHONUNBUFFERED=1
@@ -58,3 +58,5 @@ COPY scripts ./scripts
5858
# Build frontend
5959
RUN python manage.py generate_client_assets
6060
RUN python manage.py build
61+
62+
ENTRYPOINT [ "./scripts/start_prod_server.sh" ]

app/deploy.sh

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
#!/bin/sh
22
set -eu
33

4-
PROJECT_ID="<put Google Cloud Project ID here>"
4+
if [ "$#" -ne 1 ]; then
5+
echo "Usage: $0 <GCP Project ID>"
6+
exit 1
7+
fi
8+
9+
PROJECT_ID="$1"
510
REPO_NAME=zosia-repo
6-
REGION=europe-central2
11+
REGION=europe-west4
712
REPO_HOSTNAME=$REGION-docker.pkg.dev
813
IMAGE_NAME=zosia_prod
914

1015
IMAGE_URL=$REPO_HOSTNAME/$PROJECT_ID/$REPO_NAME/$IMAGE_NAME:latest
1116

12-
# Configure gcloud and docker to be able to push to the Google Container Registry
17+
echo "[1] Configuring gcloud and docker to be able to push to the Google Container Registry"
1318
gcloud config set project $PROJECT_ID
19+
gcloud config set compute/zone $REGION
1420
gcloud auth configure-docker $REPO_HOSTNAME
1521

16-
# Build and push the image
22+
echo "[2] Building and pushing the image"
1723
docker build --target prod -t $IMAGE_URL .
1824
docker push $IMAGE_URL
1925

20-
# Run the migration job
26+
echo "[3] Running migrations"
27+
gcloud run jobs update migrate --region=$REGION --image $IMAGE_URL
2128
gcloud run jobs execute migrate --wait --region=$REGION
2229

23-
# Run the collectstatic job which will collect all the static files into GCS bucket
30+
echo "[4] Collecting static files into a GCS bucket"
31+
gcloud run jobs update collectstatic --region=$REGION --image $IMAGE_URL
2432
gcloud run jobs execute collectstatic --wait --region=$REGION
2533

26-
# Deploy new service revision with the new image
34+
echo "[5] Updating createsuperuser job to use the new image"
35+
gcloud run jobs update createsuperuser --region=$REGION --image $IMAGE_URL
36+
37+
echo "[6] Deploying the new image"
2738
gcloud run services update zosia --region $REGION --image $IMAGE_URL
2839

app/scripts/createsuperuser.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
set -eu
3+
4+
# This script creates a superuser based on the environment variables:
5+
# DJANGO_SUPERUSER_USERNAME
6+
# DJANGO_SUPERUSER_EMAIL
7+
# DJANGO_SUPERUSER_PASSWORD
8+
9+
python manage.py createsuperuser --noinput

app/server/settings/common.py

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def random_string(length=10):
6565
DEBUG = False
6666

6767
ALLOWED_HOSTS = os.environ.get("HOSTS", "staging.zosia.org").split(",")
68+
CSRF_TRUSTED_ORIGINS = [f"https://*.{host}" for host in ALLOWED_HOSTS]
6869

6970
AUTH_USER_MODEL = "users.User"
7071

app/server/settings/dev.py

+1
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@
5454
# Especially room.js makes heavy use of it
5555
INTERNAL_IPS = ['127.0.0.1']
5656
ALLOWED_HOSTS = ['127.0.0.1', '0.0.0.0', 'localhost']
57+
CSRF_TRUSTED_ORIGINS = [f"http://*.{host}" for host in ALLOWED_HOSTS]

0 commit comments

Comments
 (0)