This repo contains the expected end result of a series of prompts to Gemini Code Asssist to help build a simple NodeJS web server and deploy it to Cloud Run.
-
Create a server
"Help me write a web server based on NodeJS using the Express framework with one GET endpoint returning ‘Hello, World’"
Create an
app.js
according to the instructions. -
Add another endpoint
"Add a POST endpoint that adds two user-provided numbers and returns the result"
Use the "diff" functionality (button with the two opposing arrows) to merge the new code into the existing codebase
-
Prepare for testing
"Extract the functionality of the "/add" endpoint into a separate function"
Use the "diff" button again.
-
Prepare for testing
"How do I move the "addNumbers" function to a separate file called "util.js" and use it in "app.js"?"
Create a
util.js
according to the instructions. Also updateapp.js
accordingly for importing -
Writing tests
"Write a unit test for the addNumbers function"
Create a
test.js
according to the instructions. -
Create package.json
"Generate a package.json for this project including scripts for start and test"
Create a
package.json
according to the instructions. -
Run tests
Run
npm test
to verify that the code for
addNumbers()
is actually working as expected
-
Creating a Dockerfile
"Generate a Dockerfile for this project based on the latest NodeJS LTS version using port 8080"
Create a
Dockerfile
according to the instructions. -
Build using Cloud Build
"How do i deploy this Dockerfile to Cloud Run allowing unauthenticated access?"
Create a
cloudbuild.yaml
according to the instructions.Remember to:
- Replace all placeholders with their respective values
- Create the Artifact Registry repo before moving on to the next step
Fallback:
In case Gemini doesn't come up with a response regarding Cloud Build you may:
-
Use the
cloudbuild.yaml
from this repo-
Add the following permissions to the default Compute Engine Service Account:
logging.logWriter
(e.g. "Logs Writer" role)artifactregistry.repositories.uploadArtifacts
(e.g. "Artifact Registry Writer" role)
-
Trigger Cloud Build
gcloud builds submit --config cloudbuild.yaml
-
If you encounter an
AccessDeniedException: 403
error, add thestorage.objects.viewer
permission (e.g. "Storage Object Viewer" role) to the default Compute Engine Service Account
-
-
Don't use Cloud Build
docker build -t gcr.io/[YOUR_PROJECT_ID]/[YOUR_REPO_NAME] .
docker push gcr.io/[YOUR_PROJECT_ID]/[YOUR_REPO_NAME]
-
Deploying to Cloud Run
The explanations to the previous prompt should have also given instructions on how to use the
gcloud run deploy [...]
commandFallback:
In case Gemini didn't come up with the
gcloud run deploy [...]
command you may:-
Deploy from Artifact Registry
Use this if you deployed using
cloudbuild.yaml
gcloud run deploy my-service --image us-central1-docker.pkg.dev/[YOUR_PROJECT_ID]/[YOUR_REPO_NAME]/my-service:latest --region us-central1 --platform managed --allow-unauthenticated
-
Deploy from GCR
Use this if you deployed to GCR
gcloud run deploy my-service --image gcr.io/[YOUR_PROJECT_ID]/[YOUR_REPO_NAME] --region us-central1 --allow-unauthenticated
-