Skip to content

Commit 9528090

Browse files
committed
Update cloud build configs to deploy playground sandboxes
Fix kaniko cache invalidation issue
1 parent ff984bd commit 9528090

File tree

3 files changed

+120
-15
lines changed

3 files changed

+120
-15
lines changed

Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ RUN npx lerna run build --scope lit-dev-api --stream && \
4444
# Site content
4545
COPY packages/lit-dev-content/ ./packages/lit-dev-content/
4646
ARG PLAYGROUND_SANDBOX
47+
# Kaniko doesn't include ARG values in the layer cache key (see
48+
# https://github.com/GoogleContainerTools/kaniko/pull/1085). This is different
49+
# to normal Docker behavior, which would invalidate anything after the ARG
50+
# declaration if the value changes. So, we need to write it to the file system
51+
# to force a cache invalidation. Otherwise, we might re-use the most recent
52+
# Eleventy build output, even when the playground sandbox URL has changed.
53+
RUN echo "$PLAYGROUND_SANDBOX" > playground-sandbox
4754
RUN npx lerna run build --scope lit-dev-content --stream
4855

4956
# Run the web service on container startup.

cloudbuild-main.yaml

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,31 @@ steps:
88
# https://docs.docker.com/engine/reference/commandline/build/
99
# https://github.com/GoogleCloudPlatform/cloud-builders/tree/master/docker
1010
# https://cloud.google.com/build/docs/kaniko-cache
11-
- id: Build
11+
- id: build
1212
name: gcr.io/kaniko-project/executor
1313
args:
1414
- --dockerfile=./Dockerfile
15-
- --destination=$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA
15+
- --destination=$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/lit-dev:$COMMIT_SHA
1616
- --cache=true
1717
- --cache-ttl=168h # 1 week
18+
# Bake in the playground sandbox url
19+
- --build-arg=PLAYGROUND_SANDBOX=https://lit-dev-playground-5ftespv5na-uc.a.run.app/
1820

19-
# Create a new Cloud Run revision.
21+
# Create a new Cloud Run revision for the main site.
2022
#
2123
# https://cloud.google.com/sdk/gcloud/reference/beta/run/deploy
2224
# https://github.com/GoogleCloudPlatform/cloud-sdk-docker
23-
- id: Deploy
25+
- id: deploy-main
2426
name: gcr.io/google.com/cloudsdktool/cloud-sdk
2527
entrypoint: gcloud
2628
args:
2729
- beta
2830
- run
2931
- deploy
30-
- $_SERVICE_NAME
32+
- lit-dev # Service name
3133
- '--region=$_DEPLOY_REGION'
3234
- '--platform=managed'
33-
- '--image=$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
35+
- '--image=$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/lit-dev:$COMMIT_SHA'
3436
- '--quiet'
3537
- '--no-traffic'
3638
- '--tag=master-$SHORT_SHA'
@@ -42,20 +44,80 @@ steps:
4244
- '--concurrency=40'
4345
- '--min-instances=1'
4446
- '--max-instances=1000'
47+
# Serve the main site
48+
- '--update-env-vars=MODE=main'
4549

46-
# Route traffic to new revision.
50+
# Create a new Cloud Run revision for the Playground sandbox.
51+
#
52+
# https://cloud.google.com/sdk/gcloud/reference/beta/run/deploy
53+
# https://github.com/GoogleCloudPlatform/cloud-sdk-docker
54+
- id: deploy-playground
55+
name: gcr.io/google.com/cloudsdktool/cloud-sdk
56+
entrypoint: gcloud
57+
waitFor:
58+
# Don't wait for main site deploy
59+
- build
60+
args:
61+
- beta
62+
- run
63+
- deploy
64+
- lit-dev-playground # Service name
65+
- '--region=$_DEPLOY_REGION'
66+
- '--platform=managed'
67+
- '--image=$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/lit-dev:$COMMIT_SHA'
68+
- '--quiet'
69+
- '--no-traffic'
70+
- '--tag=master-$SHORT_SHA'
71+
# IMPORTANT: If you change --memory, be sure to also change
72+
# --max-old-space-size in ./Dockerfile, and this same flag in
73+
# ./cloudbuild-pr.yaml
74+
- '--memory=1Gi'
75+
- '--cpu=1'
76+
- '--concurrency=40'
77+
- '--min-instances=1'
78+
- '--max-instances=1000'
79+
# Serve the playground files
80+
- '--update-env-vars=MODE=playground'
81+
82+
# Route traffic to new revision for the playground.
4783
#
4884
# https://cloud.google.com/sdk/gcloud/reference/beta/run/services
4985
# https://github.com/GoogleCloudPlatform/cloud-sdk-docker
50-
- id: Route Traffic
86+
- id: route-traffic-main
5187
name: gcr.io/google.com/cloudsdktool/cloud-sdk
5288
entrypoint: gcloud
89+
waitFor:
90+
# Route traffic simultaneously once both deploys are done
91+
- deploy-main
92+
- deploy-playground
5393
args:
5494
- beta
5595
- run
5696
- services
5797
- update-traffic
58-
- $_SERVICE_NAME
98+
- lit-dev # Service name
99+
- '--region=$_DEPLOY_REGION'
100+
- '--platform=managed'
101+
- '--quiet'
102+
- '--to-tags=master-$SHORT_SHA=100'
103+
104+
# Route traffic to new revision for the main site.
105+
#
106+
# https://cloud.google.com/sdk/gcloud/reference/beta/run/services
107+
# https://github.com/GoogleCloudPlatform/cloud-sdk-docker
108+
- id: route-traffic-playground
109+
name: gcr.io/google.com/cloudsdktool/cloud-sdk
110+
entrypoint: gcloud
111+
waitFor:
112+
# Route traffic simultaneously once both deploys are done
113+
- deploy-main
114+
- deploy-playground
115+
args:
116+
- beta
117+
- run
118+
- services
119+
- update-traffic # Service name
120+
- lit-dev-playground
59121
- '--region=$_DEPLOY_REGION'
60122
- '--platform=managed'
61123
- '--quiet'

cloudbuild-pr.yaml

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,31 @@ steps:
1212
# https://docs.docker.com/engine/reference/commandline/build/
1313
# https://github.com/GoogleCloudPlatform/cloud-builders/tree/master/docker
1414
# https://cloud.google.com/build/docs/kaniko-cache
15-
- id: Build
15+
- id: build
1616
name: gcr.io/kaniko-project/executor
1717
args:
1818
- --dockerfile=./Dockerfile
19-
- --destination=$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA
19+
- --destination=$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/lit-dev:$COMMIT_SHA
2020
- --cache=true
2121
- --cache-ttl=168h # 1 week
22+
# Bake in this revision's corresponding playground sandbox url
23+
- --build-arg=PLAYGROUND_SANDBOX=https://pr$_PR_NUMBER-$SHORT_SHA---lit-dev-playground-5ftespv5na-uc.a.run.app/
2224

23-
# Create a new Cloud Run revision.
25+
# Create a new Cloud Run revision for the main site.
2426
#
2527
# https://cloud.google.com/sdk/gcloud/reference/beta/run/deploy
2628
# https://github.com/GoogleCloudPlatform/cloud-sdk-docker
27-
- id: Deploy
29+
- id: deploy-main
2830
name: gcr.io/google.com/cloudsdktool/cloud-sdk
2931
entrypoint: gcloud
3032
args:
3133
- beta
3234
- run
3335
- deploy
34-
- $_SERVICE_NAME
36+
- lit-dev # Service name
3537
- '--region=$_DEPLOY_REGION'
3638
- '--platform=managed'
37-
- '--image=$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
39+
- '--image=$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/lit-dev:$COMMIT_SHA'
3840
- '--quiet'
3941
- '--no-traffic'
4042
- '--tag=pr$_PR_NUMBER-$SHORT_SHA'
@@ -46,6 +48,40 @@ steps:
4648
- '--concurrency=default' # unlimited
4749
- '--min-instances=0'
4850
- '--max-instances=1'
51+
# Serve the main site
52+
- '--update-env-vars=MODE=main'
53+
54+
# Create a new Cloud Run revision for the playground sandbox.
55+
#
56+
# https://cloud.google.com/sdk/gcloud/reference/beta/run/deploy
57+
# https://github.com/GoogleCloudPlatform/cloud-sdk-docker
58+
- id: deploy-playground
59+
name: gcr.io/google.com/cloudsdktool/cloud-sdk
60+
entrypoint: gcloud
61+
waitFor:
62+
# Don't wait for main site to deploy
63+
- build
64+
args:
65+
- beta
66+
- run
67+
- deploy
68+
- lit-dev-playground # Service name
69+
- '--region=$_DEPLOY_REGION'
70+
- '--platform=managed'
71+
- '--image=$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/lit-dev:$COMMIT_SHA'
72+
- '--quiet'
73+
- '--no-traffic'
74+
- '--tag=pr$_PR_NUMBER-$SHORT_SHA'
75+
# IMPORTANT: If you change --memory, be sure to also change
76+
# --max-old-space-size in ./Dockerfile, and this same flag in
77+
# ./cloudbuild-main.yaml
78+
- '--memory=1Gi'
79+
- '--cpu=1'
80+
- '--concurrency=default' # unlimited
81+
- '--min-instances=0'
82+
- '--max-instances=1'
83+
# Serve the playground
84+
- '--update-env-vars=MODE=playground'
4985

5086
tags:
5187
- lit-dev

0 commit comments

Comments
 (0)