Skip to content

Commit bc780b6

Browse files
committed
Deploy to cloud run
1 parent 30c8df1 commit bc780b6

File tree

8 files changed

+144
-13
lines changed

8 files changed

+144
-13
lines changed

.gcloudignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
!data/
2+
!data/**

.github/workflows/deploy.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Build & Deploy to Cloud Run
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
env:
9+
PROJECT_ID: sb-gcp-project-01
10+
REGION: europe-west1
11+
REPO: suntrace-repo
12+
SERVICE: suntrace
13+
GCS_BUCKET: suntrace # ← set this to the bucket where you uploaded your geojson
14+
15+
jobs:
16+
deploy:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v3
20+
21+
- name: Authenticate to GCP
22+
uses: google-github-actions/auth@v1
23+
with:
24+
credentials_json: ${{ secrets.GCP_SA_KEY }}
25+
26+
- name: Set up Cloud SDK
27+
uses: google-github-actions/setup-gcloud@v1
28+
with:
29+
project_id: ${{ env.PROJECT_ID }}
30+
install_components: ['gcloud', 'beta', 'alpha']
31+
32+
# ← NEW STEP: fetch your geojson folder from GCS into ./data
33+
- name: Download GeoJSON data from GCS
34+
run: |
35+
mkdir -p data
36+
gsutil -m cp -r gs://$GCS_BUCKET/geojson/* data/
37+
38+
- name: Build with Cloud Build
39+
run: |
40+
TAG="${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO }}/${{ env.SERVICE }}"
41+
# this will now include data/ in the build context
42+
gcloud builds submit --tag "$TAG"
43+
44+
- name: Deploy to Cloud Run
45+
run: |
46+
TAG="${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO }}/${{ env.SERVICE }}"
47+
gcloud run deploy ${{ env.SERVICE }} \
48+
--image "$TAG" \
49+
--region ${{ env.REGION }} \
50+
--platform managed \
51+
--allow-unauthenticated

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Ignore venv
2+
env/
23
venv/
34
.venv/
45
# Ignore Python cache files

Dockerfile

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,43 @@
11
# Use slim Python 3.12 image
22
FROM python:3.12-slim
33

4-
# Set environment variables
4+
# Don’t write .pyc files and force stdout/stderr to be unbuffered
55
ENV PYTHONDONTWRITEBYTECODE=1 \
6-
PYTHONUNBUFFERED=1
6+
PYTHONUNBUFFERED=1 \
7+
# Tell rasterio/geopandas where GDAL lives
8+
GDAL_CONFIG=/usr/bin/gdal-config \
9+
CPLUS_INCLUDE_PATH=/usr/include/gdal \
10+
C_INCLUDE_PATH=/usr/include/gdal
711

812
# Set working directory
913
WORKDIR /app
1014

11-
# Install system dependencies
12-
RUN apt-get update && apt-get install -y --no-install-recommends \
15+
# Install build tools + GDAL + GEOS + PROJ headers
16+
RUN apt-get update && \
17+
apt-get install -y --no-install-recommends \
1318
build-essential \
14-
gcc \
15-
libffi-dev \
16-
libssl-dev \
19+
python3-dev \
20+
gdal-bin \
21+
libgdal-dev \
22+
libgeos-dev \
23+
libproj-dev \
1724
&& rm -rf /var/lib/apt/lists/*
1825

1926
# Install Python dependencies
2027
COPY requirements.txt .
21-
RUN pip install --no-cache-dir -r requirements.txt
28+
RUN pip install --upgrade pip wheel && \
29+
pip install --no-cache-dir -r requirements.txt
2230

2331
# Copy the app code
2432
COPY . .
2533

34+
COPY ./start.sh /app/start.sh
35+
2636
# Expose the port FastAPI will run on
27-
EXPOSE 8000
37+
ENV PORT 8080
38+
# EXPOSE 8000
2839

2940
# Run the app using Uvicorn
30-
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
31-
41+
RUN chmod +x /app/start.sh
42+
# CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
43+
ENTRYPOINT ["/app/start.sh"]

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,30 @@ python main.py
5555
### 4. Run in Docker
5656

5757
```sh
58+
export OPENAI_API_KEY=your_openai_key
5859
docker build -t suntrace .
59-
docker run -p 8000:8000 suntrace
60+
docker run --rm -d \
61+
-p 8080:8080 \
62+
-e OPENAI_API_KEY="${OPENAI_API_KEY}" \
63+
--name suntrace \
64+
suntrace:latest
65+
```
66+
67+
See logs
68+
69+
```sh
70+
docker logs -f suntracte
71+
```
72+
73+
#### With docker compose
74+
75+
```sh
76+
docker-compose up -d --build
6077
```
6178

6279
### 5. Access Frontend
6380

64-
Open [http://localhost:8000](http://localhost:8000) in your browser.
81+
Open [http://localhost:8080](http://localhost:8080) in your browser.
6582

6683

6784
## Testing
@@ -78,6 +95,20 @@ Create a `.env` file for secrets (e.g., OpenAI API key):
7895
OPENAI_API_KEY=your_openai_key
7996
```
8097

98+
## Deployment
99+
100+
Make sure you have [**gcloud cli**](https://cloud.google.com/sdk/docs/install-sdk) installed and setup
101+
102+
The app is deployed using [**Google Cloud Run**](https://cloud.google.com/run?hl=en)
103+
104+
To deploy the application, run the commands below
105+
106+
```sh
107+
chmod +x bin/deploy
108+
chmod +x start.sh
109+
./bin/deploy
110+
```
111+
81112
## Data Requirements
82113

83114
Place required geospatial files in the `data/` directory. See [tests/TESTING.md](tests/TESTING.md) for details.

bin/deploy

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
export APP=suntrace
5+
export PROJECT_ID=sb-gcp-project-01
6+
export REGION=europe-west1
7+
export REPO=suntrace-repo
8+
export TAG=${REGION}-docker.pkg.dev/${PROJECT_ID}/${REPO}/${APP}
9+
10+
# 1. Build & push through Cloud Build
11+
gcloud builds submit --tag $TAG
12+
13+
14+
# # 2. Deploy to Cloud Run
15+
gcloud run deploy $APP \
16+
--image $TAG \
17+
--region $REGION \
18+
--platform managed \
19+
--allow-unauthenticated

docker-compose.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
services:
2+
suntrace:
3+
container_name: suntrace
4+
# image: suntrace:latest
5+
build:
6+
context: .
7+
dockerfile: Dockerfile
8+
ports:
9+
- "8080:8080"
10+
env_file:
11+
- .env

start.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
# Start the FastAPI application with Uvicorn
4+
uvicorn main:app --host 0.0.0.0 --port ${PORT} --workers 1

0 commit comments

Comments
 (0)