-
Notifications
You must be signed in to change notification settings - Fork 1
164 lines (146 loc) · 5.87 KB
/
Copy pathapi.yml
File metadata and controls
164 lines (146 loc) · 5.87 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
name: CI/CD Pipeline for API Service
env:
SERVICE_NAME: api-v2
REPO_NAME: v2
REGION: us-west1
PROJECT_ID: ${{ secrets.GCP_PROJECT }}
on:
push:
branches:
- main
- dev
paths:
- 'services/api/**'
- 'services/lib/**'
- '.github/workflows/api.yml'
jobs:
determine-environment:
name: Determine Deployment Environment
runs-on: ubuntu-latest
outputs:
deploy_env: ${{ steps.set-env.outputs.deploy_env }}
steps:
- name: Set deployment environment based on branch
id: set-env
run: |
if [[ $GITHUB_REF == 'refs/heads/main' ]]; then
echo "deploy_env=prod" >> $GITHUB_OUTPUT
else
echo "deploy_env=dev" >> $GITHUB_OUTPUT
fi
build:
name: Build Docker Image and Push to Artifact Registry
runs-on: ubuntu-latest
needs: determine-environment
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Google Artifact Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGION }}-docker.pkg.dev
username: _json_key
password: ${{ secrets.GCP_CREDENTIALS }}
- name: Build and Push Image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: |
${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/${{ env.SERVICE_NAME }}:latest
${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/${{ env.SERVICE_NAME }}:${{ github.sha }}
${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/${{ env.SERVICE_NAME }}:${{ needs.determine-environment.outputs.deploy_env }}
file: services/api/Dockerfile
cache-from: type=gha
cache-to: type=gha,mode=max
deploy:
name: Deploy to ${{ needs.determine-environment.outputs.deploy_env }}
runs-on: ubuntu-latest
needs: [ determine-environment, build ]
outputs:
service_url: ${{ steps.get-url.outputs.service_url }}
steps:
- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v3
with:
credentials_json: ${{ secrets.GCP_CREDENTIALS }}
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v3
- name: Deploy to Cloud Run
run: |
CLOUD_RUN_NAME="${{ env.SERVICE_NAME }}-${{ needs.determine-environment.outputs.deploy_env }}"
gcloud run deploy "$CLOUD_RUN_NAME" \
--image ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/${{ env.SERVICE_NAME }}:${{ needs.determine-environment.outputs.deploy_env }} \
--region ${{ env.REGION }} \
--update-env-vars="\
DEPLOYMENT_ENV=${{ needs.determine-environment.outputs.deploy_env }},\
INFRA_ENV=${{ needs.determine-environment.outputs.deploy_env }},\
GCP_PROJECT=${{ secrets.GCP_PROJECT }},\
GCP_REGION=${{ env.REGION }},\
GRIDS_BUCKET=${{ secrets.GRIDS_BUCKET }},\
EXPORTS_BUCKET=${{ secrets.EXPORTS_BUCKET }},\
RASTERS_BUCKET=${{ secrets.RASTERS_BUCKET }},\
INVENTORIES_BUCKET=${{ secrets.INVENTORIES_BUCKET }},\
UPLOADS_BUCKET=${{ secrets.UPLOADS_BUCKET }},\
FEATURES_BUCKET=${{ secrets.FEATURES_BUCKET }},\
POINT_CLOUDS_BUCKET=${{ secrets.POINT_CLOUDS_BUCKET }}" \
--quiet
- name: Get service URL
id: get-url
run: |
CLOUD_RUN_NAME="${{ env.SERVICE_NAME }}-${{ needs.determine-environment.outputs.deploy_env }}"
URL=$(gcloud run services describe "$CLOUD_RUN_NAME" \
--region ${{ env.REGION }} \
--format "value(status.url)")
echo "service_url=$URL" >> $GITHUB_OUTPUT
- name: Wait for new revision to be reachable
run: |
# URL="${{ steps.get-url.outputs.service_url }}"
# deadline=$((SECONDS + 120))
# until curl -fsS --max-time 10 "$URL/" >/dev/null 2>&1; do
# if [ $SECONDS -ge $deadline ]; then
# echo "Service did not respond within 120s"; exit 1
# fi
# echo "Waiting for $URL ..."
# sleep 5
# done
# echo "Service is reachable. Sleeping 15s to let the revision settle."
sleep 60
test:
name: Run Tests
runs-on: ubuntu-latest
needs: deploy
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.13'
- name: Install uv
uses: astral-sh/setup-uv@v4
- name: Install dependencies
working-directory: services/api
run: uv sync --frozen --group dev
- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v3
with:
credentials_json: ${{ secrets.GCP_CREDENTIALS }}
- name: Run tests
working-directory: services/api
env:
GCP_PROJECT: ${{ secrets.GCP_PROJECT }}
GCP_REGION: ${{ env.REGION }}
GRIDS_BUCKET: ${{ secrets.GRIDS_BUCKET }}
EXPORTS_BUCKET: ${{ secrets.EXPORTS_BUCKET }}
RASTERS_BUCKET: ${{ secrets.RASTERS_BUCKET }}
INVENTORIES_BUCKET: ${{ secrets.INVENTORIES_BUCKET }}
UPLOADS_BUCKET: ${{ secrets.UPLOADS_BUCKET }}
FEATURES_BUCKET: ${{ secrets.FEATURES_BUCKET }}
POINT_CLOUDS_BUCKET: ${{ secrets.POINT_CLOUDS_BUCKET }}
TEST_BUCKET: ${{ secrets.TEST_BUCKET }}
TEST_API_URL: ${{ needs.deploy.outputs.service_url }}
TEST_API_KEY: ${{ secrets.TEST_API_KEY }}
run: uv run pytest tests/ -v