- Google Cloud Project:
- A Google Cloud Platform project.
- Billing enabled for the project.
- Docker: Installed locally if you want to build/run the image locally.
- Python &
uv: Python 3.11+ anduv(orpip) for local development. - Gemini API Key:
- Obtain an API key for the Gemini API from Google AI Studio.
-
Clone the repository:
git clone <your-repo-url> cd <your-repo-name>
-
Set up a virtual environment (optional if you don't use
uv):python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Configure local Gemini API Key: Rename
.env.templateto.envand replaceYOUR_GEMINI_API_KEY_HEREwith your API key. -
Run the Streamlit app locally:
uv run streamlit run app.py # Or: streamlit run app.pyThe application will be available at
http://localhost:8501.
-
Ensure all files are committed and pushed to GitHub:
app.py(Streamlit application)requirements.txt(defining dependencies)Dockerfile(as configured in previous steps, usinguv sync)cloudbuild.yaml(as configured, with correct substitutions for_GCR_HOSTNAME).gitignore
-
(Optional but Recommended) Test Docker Build Locally: Open your terminal in the project root and run:
docker build -t gemini-chatbot-test .If this completes successfully, it's a good sign the
Dockerfileand dependency setup are correct. You can even try running it:docker run -p 8501:8501 -e PORT=8501 gemini-chatbot-test
Then open
http://localhost:8501in your browser. (You won't have the API key here unless you pass it, but the app should start).
-
Select or Create a GCP Project:
- Go to the Google Cloud Console.
- Ensure you have a project selected, or create a new one. Make sure billing is enabled for this project.
-
Enable Necessary APIs:
- In the Cloud Console, search for and enable the following APIs:
- Cloud Build API
- Cloud Run Admin API
- Secret Manager API
- In the Cloud Console, search for and enable the following APIs:
-
Store your Gemini API Key in Secret Manager:
- Navigate to Security > Secret Manager in the Cloud Console.
- Click "Create Secret".
- Name:
gemini-api-key-secret(this must match the name used in yourcloudbuild.yaml's--set-secretsflag). - Secret value: Paste your actual Gemini API key.
- Leave other settings as default and click "Create secret".
-
Create an Artifact Registry Docker Repository:
- Navigate to CI/CD > Artifact Registry in the Cloud Console.
- Click "Create Repository".
- Name: e.g.,
gemini-chatbot-repo(you can choose any name). - Format: Docker.
- Mode: Standard.
- Location Type: Region.
- Region: Select the same region you plan to deploy your Cloud Run service to (e.g.,
us-central1). This is important for performance and potential cost savings. - Click "Create".
- Note the path: After creation, the path will look like
REGION-docker.pkg.dev/YOUR_PROJECT_ID/YOUR_REPO_NAME.- For example:
us-central1-docker.pkg.dev/my-gcp-project-id/gemini-chatbot-repo - The
_GCR_HOSTNAMEin yourcloudbuild.yamlshould be the first part:us-central1-docker.pkg.dev. - The image name in
cloudbuild.yamlwill effectively become:us-central1-docker.pkg.dev/YOUR_PROJECT_ID/gemini-chatbot-repo:${COMMIT_SHA}if your_SERVICE_NAMEisgemini-chatbot-repoand your repository is part of the image path implicitly defined by Artifact Registry.
- For example:
-
Configure IAM Permissions for the Cloud Build Service Account: The Cloud Build service account needs permissions to perform actions on your behalf.
- Navigate to IAM & Admin > IAM.
- Find the principal (service account) named
[YOUR_PROJECT_NUMBER]@cloudbuild.gserviceaccount.com.- You can find
YOUR_PROJECT_NUMBERon the GCP Console Dashboard.
- You can find
- Click the pencil icon (Edit principal) next to it.
- Click "+ ADD ANOTHER ROLE" and add the following roles:
- Cloud Run Admin: (
roles/run.admin) - Allows Cloud Build to deploy and manage Cloud Run services. - Service Account User: (
roles/iam.serviceAccountUser) - Allows Cloud Build to deploy Cloud Run services that can act as (use the identity of) other service accounts (typically the Compute Engine default service account). - Artifact Registry Writer: (
roles/artifactregistry.writer) - Allows Cloud Build to push images to your Artifact Registry repository. (You can scope this to the specific repository if desired). - Secret Manager Secret Accessor: (
roles/secretmanager.secretAccessor) - Allows Cloud Build to access the Gemini API key from Secret Manager during deployment. (You can scope this to the specific secret).
- Cloud Run Admin: (
- Click "Save".
-
Connect Your GitHub Repository to Cloud Build:
- Navigate to CI/CD > Cloud Build > Triggers.
- If you haven't connected a source repository before, you might see a "Connect repository" wizard. Follow the prompts to connect your GitHub account and select the repository where your code resides.
- If you've connected before, click "Manage repositories" at the top or "Connect repository" if prompted.
- Select "GitHub" as the source.
- Authenticate with GitHub and authorize Google Cloud Build.
- Select your GitHub account/organization and then select the specific repository containing your Streamlit app code.
- Click "Connect". Then click "Done".
-
Create the Trigger:
- On the Cloud Build Triggers page, click "Create trigger".
- Name: e.g.,
deploy-gemini-chatbot-main - Description: (Optional) e.g.,
Deploys Gemini Streamlit app to Cloud Run on push to main - Event: "Push to a branch"
- Source > Repository: Select the GitHub repository you just connected.
- Source > Branch:
^main$(This is a regex that matches the "main" branch exactly. Use^master$if your default branch is "master"). - Configuration > Type: "Cloud Build configuration file (yaml or json)"
- Configuration > Location: "Repository"
- Configuration > Cloud Build configuration file location:
cloudbuild.yaml(If it's in the root of your repo. If it's in a subdirectory, specifysubdir/cloudbuild.yaml). - Advanced > Substitution variables: This is important to match your
cloudbuild.yaml.- Click "+ ADD VARIABLE" for each:
- Variable:
_SERVICE_NAMEValue:gemini-chatbot-repo(or whatever you used incloudbuild.yamland for your Artifact Registry repo name) - Variable:
_REGIONValue:us-central1(or your chosen region, ensure it matches Artifact Registry region andcloudbuild.yaml) - Variable:
_GCR_HOSTNAMEValue:us-central1-docker.pkg.dev(Adjust region if your Artifact Registry is elsewhere. This is the host for Artifact Registry, notgcr.iounless you're using the older Container Registry).
- Variable:
- Click "+ ADD VARIABLE" for each:
- Click "Create".
-
Make a small commit and push to your
mainbranch: For example, add a comment toapp.pyor update aREADME.md.git add . git commit -m "Trigger initial Cloud Build deployment" git push origin main
-
Monitor the Build in Cloud Build:
- Go to CI/CD > Cloud Build > History in the GCP Console.
- You should see a new build running for your repository and branch.
- Click on it to see the build logs and steps. It will:
- Clone your repository.
- Build the Docker image (using
docker build ...as defined incloudbuild.yaml). - Push the image to Artifact Registry.
- Deploy the image to Cloud Run (using
gcloud run deploy ...).
-
Check Cloud Run Service:
- Once the build completes successfully, navigate to Compute > Cloud Run.
- You should see your service (e.g.,
gemini-chatbot-repo) listed. - Click on the service name.
- The service URL will be displayed at the top. Click it to open your deployed Streamlit application!
- Check the "Logs" tab in Cloud Run for your service if you encounter any runtime issues.
- Test Your Deployed App: Interact with your chatbot to ensure it's working as expected and can call the Gemini API.
- Automatic Updates: Now, every time you push a commit to the
mainbranch of your connected GitHub repository, the Cloud Build trigger will automatically:- Rebuild your Docker image with the latest code.
- Push the new image to Artifact Registry.
- Deploy a new revision to your Cloud Run service with the updated image.
- Build Failures (Cloud Build): Check the build logs in Cloud Build carefully. Common issues:
- Incorrect
Dockerfilecommands. - Typos in
cloudbuild.yaml. - IAM permission errors (Cloud Build service account missing a role).
uvor dependency installation issues.
- Incorrect
- Deployment Failures (Cloud Run step in Cloud Build):
- IAM permission errors (Cloud Build SA missing Cloud Run Admin or Service Account User).
- Incorrect image path.
- Cloud Run service configuration issues.
- Application Not Starting/Errors on Cloud Run:
- Check the logs for your service in Cloud Run > [Your Service] > Logs.
- Ensure the
GEMINI_API_KEYenvironment variable is correctly injected (you can see environment variables in the Cloud Run service revision details). - Port issues: The
DockerfileEXPOSEs 8501, and theCMDuses$PORT. Cloud Run sets$PORTand expects the app to listen on it. Thecloudbuild.yamlalso specifies--port=8501for the service definition which is good. - Application code errors.
-
app.py:- The core Streamlit application.
- Manages session state for chat history and model selection.
- Makes requests to the Gemini API (
call_gemini). - Streams responses from the API (
response_generator). - Uses Streamlit's
st.chat_message,st.chat_input, andst.write_stream.
-
Dockerfile:- Uses
python:3.11-slimas the base image. - Installs
uvfor faster package installation. - Sets environment variables
PYTHONUNBUFFERED,PIP_NO_CACHE_DIR,PIP_DISABLE_PIP_VERSION_CHECK,UV_SYSTEM_PYTHON, andPORT. - Copies
requirements.txtand installs dependencies. - Copies the application code.
- Exposes port
8501(which Streamlit will use, dynamically set by Cloud Run's$PORTenv var). - The
CMDusesuv run streamlit run ...to start the application. Cloud Run's$PORTenvironment variable is used to set the server port.--server.enableCORS falseand--server.enableXsrfProtection false: These are often necessary for Streamlit apps behind proxies or load balancers like Cloud Run.--server.headless true: Recommended for running Streamlit in a headless environment.
- Uses
-
requirements.txt: Lists the Python dependencies:streamlit requests # Add any other direct dependencies here -
cloudbuild.yaml: Defines the CI/CD steps for Google Cloud Build:- Build Docker Image: Builds the Docker image using the
Dockerfile. - Push Docker Image: Pushes the built image to Artifact Registry (or GCR) tagged with the commit SHA.
- Deploy to Cloud Run: Deploys the image to Cloud Run.
--platform=managed: Specifies managed Cloud Run.--allow-unauthenticated: Makes the service publicly accessible. Remove or configure IAM for authentication if needed.--port=8501: Informs Cloud Run that the container expects traffic on port 8501.--set-secrets=GEMINI_API_KEY=gemini-api-key-secret:latest: Mounts the Gemini API key from Secret Manager.--command=uvand--args=run,streamlit,run,app.py,--server.port,${PORT},...: Overrides the DockerCMDto explicitly run Streamlit withuvand ensure it listens on the port provided by Cloud Run (${PORT}). This provides fine-grained control over the startup command.
- Build Docker Image: Builds the Docker image using the
This setup provides a robust foundation for developing and deploying a Gemini-powered Streamlit chatbot on Google Cloud.