Skip to content

Commit c541c53

Browse files
committed
A follow up changes to improve CloudRun deployment
1 parent 7c6253d commit c541c53

File tree

8 files changed

+96
-21
lines changed

8 files changed

+96
-21
lines changed

Readme.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,35 @@ Example: 1 hour session ≈ $0.50-1.00
227227

228228
The `cloudbuild.yaml` provides:
229229
1. Submodule initialization
230-
2. Docker image build
230+
2. Docker image build with caching
231231
3. Container Registry push
232232
4. Cloud Run deployment
233233
5. Traffic routing
234234

235-
Trigger builds via:
235+
### Build Commands
236+
236237
```bash
238+
# Normal build (with cache) - recommended for development
237239
gcloud builds submit --config cloudbuild.yaml
240+
241+
# Force rebuild without cache - use when dependencies change
242+
gcloud builds submit --config cloudbuild.yaml --substitutions=_NO_CACHE=true
243+
244+
# Automated deployment with Twilio TURN server setup
245+
./deploy.sh
238246
```
239247

248+
### Cache Control
249+
250+
The build system uses Docker layer caching by default to reduce build times and costs:
251+
- **With cache**: ~5-10 minutes, lower cost
252+
- **Without cache**: ~30+ minutes, higher cost (~$3-5 per build)
253+
254+
Use `_NO_CACHE=true` only when:
255+
- Dependencies have changed significantly
256+
- Base images need updating
257+
- Debugging build issues
258+
240259
## 📚 Additional Resources
241260

242261
- [kernel-images Documentation](https://github.com/onkernel/kernel-images)

cloudbuild.yaml

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Cloud Build configuration for kernel-browser
2+
# Usage: gcloud builds submit --substitutions=_NO_CACHE=true (to disable cache)
3+
substitutions:
4+
_NO_CACHE: 'false'
5+
26
steps:
37
# Step 1: Verify kernel-images directory exists
48
- name: 'gcr.io/cloud-builders/docker'
@@ -17,22 +21,36 @@ steps:
1721
args:
1822
- '-c'
1923
- |
20-
echo "Attempting to pull previous image for caching..."
21-
docker pull us-docker.pkg.dev/$PROJECT_ID/gcr.io/kernel-browser:latest || echo "No previous image found for caching"
24+
if [ "${_NO_CACHE}" = "true" ]; then
25+
echo "⚠️ Cache disabled by _NO_CACHE=true flag"
26+
else
27+
echo "Attempting to pull previous image for caching..."
28+
docker pull us-docker.pkg.dev/$PROJECT_ID/gcr.io/kernel-browser:latest || echo "No previous image found for caching"
29+
fi
2230
2331
# Step 3: Build the Docker image with caching (using cloudrun Dockerfile)
2432
- name: 'gcr.io/cloud-builders/docker'
33+
entrypoint: 'bash'
2534
args:
26-
- 'build'
27-
- '--file'
28-
- 'Dockerfile.cloudrun'
29-
- '--cache-from'
30-
- 'us-docker.pkg.dev/$PROJECT_ID/gcr.io/kernel-browser:latest'
31-
- '--build-arg'
32-
- 'CACHE_BUST=$BUILD_ID'
33-
- '--tag'
34-
- 'us-docker.pkg.dev/$PROJECT_ID/gcr.io/kernel-browser:latest'
35-
- '.'
35+
- '-c'
36+
- |
37+
if [ "${_NO_CACHE}" = "true" ]; then
38+
echo "🔨 Building without cache..."
39+
docker build \
40+
--file Dockerfile.cloudrun \
41+
--no-cache \
42+
--build-arg CACHE_BUST=$BUILD_ID \
43+
--tag us-docker.pkg.dev/$PROJECT_ID/gcr.io/kernel-browser:latest \
44+
.
45+
else
46+
echo "🚀 Building with cache from previous image..."
47+
docker build \
48+
--file Dockerfile.cloudrun \
49+
--cache-from us-docker.pkg.dev/$PROJECT_ID/gcr.io/kernel-browser:latest \
50+
--build-arg CACHE_BUST=$BUILD_ID \
51+
--tag us-docker.pkg.dev/$PROJECT_ID/gcr.io/kernel-browser:latest \
52+
.
53+
fi
3654
timeout: '3600s' # Allow 1 hour for build (it's a large image)
3755

3856
# Step 4: Push the image to Artifact Registry
@@ -48,12 +66,14 @@ steps:
4866
- '-c'
4967
- |
5068
# Check if Twilio secrets exist and choose appropriate service file
51-
if gcloud secrets describe twilio-account-sid --project=$PROJECT_ID >/dev/null 2>&1 && \
52-
gcloud secrets describe twilio-auth-token --project=$PROJECT_ID >/dev/null 2>&1; then
53-
echo "Using service-secrets.yaml with Secret Manager references"
69+
echo "Checking for Twilio secrets..."
70+
if gcloud secrets describe twilio-account-sid --project=$PROJECT_ID && \
71+
gcloud secrets describe twilio-auth-token --project=$PROJECT_ID; then
72+
echo "✅ Twilio secrets found! Using service-secrets.yaml with Secret Manager references"
5473
cp service-secrets.yaml temp-service.yaml
5574
else
56-
echo "Using standard service.yaml (secrets not configured)"
75+
echo "⚠️ Twilio secrets NOT found. Using standard service.yaml (secrets not configured)"
76+
echo "To use Twilio TURN servers, run: ./deploy.sh to set up secrets"
5777
cp service.yaml temp-service.yaml
5878
fi
5979

cloudrun-wrapper.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ export HEIGHT=768
1313
export WIDTH=1024
1414
export NEKO_BIND=:8081
1515

16+
# WebRTC Cloud Run configuration - force relay-only mode
17+
export NEKO_WEBRTC_ICE_LITE=true
18+
export NEKO_WEBRTC_ICE_POLICY=relay
19+
export NEKO_WEBRTC_MDNS=false
20+
export NEKO_WEBRTC_ICE_INTERFACES=""
21+
1622
# Get fresh Twilio TURN credentials if available
1723
if [ -f /twilio-credential-updater.sh ]; then
1824
echo "[cloudrun-wrapper] Getting fresh Twilio TURN credentials..."
@@ -51,6 +57,9 @@ http {
5157
include /etc/nginx/mime.types;
5258
default_type application/octet-stream;
5359
60+
# Configure log files to use /tmp for non-root execution
61+
access_log /tmp/cloudrun-nginx-access.log;
62+
5463
# Create temp directories for nginx (non-root execution)
5564
client_body_temp_path /tmp/nginx_client_temp;
5665
proxy_temp_path /tmp/nginx_proxy_temp;
@@ -120,6 +129,7 @@ http {
120129
# Chrome DevTools Protocol HTTP endpoints
121130
location /json {
122131
proxy_pass http://127.0.0.1:9223/json;
132+
proxy_http_version 1.1;
123133
proxy_set_header Host \$host;
124134
proxy_set_header X-Real-IP \$remote_addr;
125135
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
@@ -129,6 +139,7 @@ http {
129139
# Chrome DevTools Protocol HTTP endpoints (with trailing slash)
130140
location /json/ {
131141
proxy_pass http://127.0.0.1:9223/json/;
142+
proxy_http_version 1.1;
132143
proxy_set_header Host \$host;
133144
proxy_set_header X-Real-IP \$remote_addr;
134145
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;

deploy.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,29 @@ setup_secrets() {
149149
--project="$PROJECT_ID" \
150150
--quiet
151151

152+
# Grant Cloud Build service account permission to view secrets (needed for cloudbuild.yaml)
153+
local project_number=$(gcloud projects describe "$PROJECT_ID" --format="value(projectNumber)")
154+
local cb_sa_email="${project_number}@cloudbuild.gserviceaccount.com"
155+
156+
info "Granting Secret Manager viewer access to Cloud Build service account..."
157+
gcloud projects add-iam-policy-binding "$PROJECT_ID" \
158+
--member="serviceAccount:$cb_sa_email" \
159+
--role="roles/secretmanager.viewer" \
160+
--project="$PROJECT_ID" \
161+
--quiet
162+
163+
gcloud secrets add-iam-policy-binding twilio-account-sid \
164+
--member="serviceAccount:$cb_sa_email" \
165+
--role="roles/secretmanager.secretAccessor" \
166+
--project="$PROJECT_ID" \
167+
--quiet
168+
169+
gcloud secrets add-iam-policy-binding twilio-auth-token \
170+
--member="serviceAccount:$cb_sa_email" \
171+
--role="roles/secretmanager.secretAccessor" \
172+
--project="$PROJECT_ID" \
173+
--quiet
174+
152175
# Set flag to use secrets-enabled service.yaml
153176
export USE_SECRETS=true
154177

nginx.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ http {
7676
# Chrome DevTools Protocol HTTP endpoints
7777
location /json {
7878
proxy_pass http://127.0.0.1:9223;
79+
proxy_http_version 1.1;
7980
proxy_set_header Host $host;
8081
proxy_set_header X-Real-IP $remote_addr;
8182
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

service-secrets.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ spec:
3636
limits:
3737
# 2 CPU cores (within quota limits)
3838
cpu: "2"
39-
# 4GiB memory (within quota limits)
40-
memory: "4Gi"
39+
# 2GiB memory (reduced to prevent DevTools crashes)
40+
memory: "2Gi"
4141
requests:
4242
cpu: "1"
4343
memory: "2Gi"

supervisor/services-cloudrun/neko.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ priority=15
77
stdout_logfile=/var/log/supervisord/neko/neko.log
88
stdout_logfile_maxbytes=50MB
99
redirect_stderr=true
10-
environment=HOME="/home/kernel",USER="kernel",DISPLAY=":1",NEKO_WEBRTC_ICESERVERS_FRONTEND="",NEKO_WEBRTC_ICESERVERS_BACKEND=""
10+
environment=HOME="/home/kernel",USER="kernel",DISPLAY=":1"

twilio/update-twilio-credentials.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ spec:
9191
spec:
9292
containers:
9393
- name: kernel-browser
94+
image: us-docker.pkg.dev/${PROJECT_ID}/gcr.io/kernel-browser:latest
9495
env:
9596
- name: NEKO_ICESERVERS
9697
value: '${ice_servers}'

0 commit comments

Comments
 (0)