About this codelab
1. Overview
In this lab, you will set up the GitLab CICD pipeline and integrate it with Gemini to automate code review steps.
What you will learn
In this lab, you will learn how to do the following:
- How to add GenAI code review automation steps in GitLab
- How to run devai cli locally to automate code reviews
Prerequisites
- This lab assumes familiarity with the Cloud Console and Cloud Shell environments.
2. Setup and Requirements
Cloud Project setup
- Sign-in to the Google Cloud Console and create a new project or reuse an existing one. If you don't already have a Gmail or Google Workspace account, you must create one.
- The Project name is the display name for this project's participants. It is a character string not used by Google APIs. You can always update it.
- The Project ID is unique across all Google Cloud projects and is immutable (cannot be changed after it has been set). The Cloud Console auto-generates a unique string; usually you don't care what it is. In most codelabs, you'll need to reference your Project ID (typically identified as
PROJECT_ID
). If you don't like the generated ID, you might generate another random one. Alternatively, you can try your own, and see if it's available. It can't be changed after this step and remains for the duration of the project. - For your information, there is a third value, a Project Number, which some APIs use. Learn more about all three of these values in the documentation.
- Next, you'll need to enable billing in the Cloud Console to use Cloud resources/APIs. Running through this codelab won't cost much, if anything at all. To shut down resources to avoid incurring billing beyond this tutorial, you can delete the resources you created or delete the project. New Google Cloud users are eligible for the $300 USD Free Trial program.
Environment Setup
Open Gemini chat.
Or type "Ask Gemini" in the search bar.
Enable Cloud AI Companion API:
Click "Start chatting
" and follow one of the sample questions or type your own prompt to try it out.
Prompts to try:
- Explain Cloud Run in 5 key points.
- You are Google Cloud Run Product Manager, explain Cloud Run to a student in 5 short key points.
- You are Google Cloud Run Product Manager, explain Cloud Run to a Certified Kubernetes Developer in 5 short key points.
- You are Google Cloud Run Product Manager, explain when you would use Cloud Run versus GKE to a Senior Developer in 5 short key points.
Check out Prompt Guide to learn more about writing better prompts.
How Gemini for Google Cloud uses your data
Google's privacy commitment
Google was one of the first in the industry to publish an AI/ML privacy commitment, which outlines our belief that customers should have the highest level of security and control over their data that's stored in the cloud.
Data you submit and receive
The questions that you ask Gemini, including any input information or code that you submit to Gemini to analyze or complete, are called prompts. The answers or code completions that you receive from Gemini are called responses. Gemini doesn't use your prompts or its responses as data to train its models.
Encryption of prompts
When you submit prompts to Gemini, your data is encrypted in-transit as input to the underlying model in Gemini.
Program data generated from Gemini
Gemini is trained on first-party Google Cloud code as well as selected third-party code. You're responsible for the security, testing, and effectiveness of your code, including any code completion, generation, or analysis that Gemini offers you.
Learn more how Google handles your prompts.
3. Options to test prompts
If you would like to change/extend existing devai cli prompts, you have several options for that.
Vertex AI Studio is a part of Google Cloud's Vertex AI platform, specifically designed to simplify and accelerate the development and use of generative AI models.
Google AI Studio is a web-based tool for prototyping and experimenting with prompt engineering and the Gemini API.
- Gemini Web App (gemini.google.com)
The Google Gemini web app (gemini.google.com) is a web-based tool designed to help you explore and utilize the power of Google's Gemini AI models.
- Google Gemini mobile app for Android and Google app on iOS
4. Create Service Account
Activate Cloud Shell by clicking on the icon to the right of the search bar.
In the opened terminal, enable required services to use Vertex AI APIs and Gemini chat.
gcloud services enable \
aiplatform.googleapis.com \
cloudaicompanion.googleapis.com \
cloudresourcemanager.googleapis.com \
secretmanager.googleapis.com
If prompted to authorize, click "Authorize" to continue.
Run following commands to create a new service account and keys.
You will use this service account to make API calls to Vertex AI Gemini API from CICD pipelines.
PROJECT_ID=$(gcloud config get-value project)
SERVICE_ACCOUNT_NAME='vertex-client'
DISPLAY_NAME='Vertex Client'
KEY_FILE_NAME='vertex-client-key'
gcloud iam service-accounts create $SERVICE_ACCOUNT_NAME --display-name "$DISPLAY_NAME"
gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:$SERVICE_ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com" --role="roles/aiplatform.admin" --condition None
gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:$SERVICE_ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com" --role="roles/secretmanager.secretAccessor" --condition None
gcloud iam service-accounts keys create $KEY_FILE_NAME.json --iam-account=$SERVICE_ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com
5. Import GitHub Repo to GitLab Repo
Go to https://gitlab.com/projects/new and select "Import project
" / "Repository by URL
" option:
Git repository url:
https://github.com/GoogleCloudPlatform/genai-for-developers.git
Under Project URL - select your gitlab userid
Set Visibility to Public
.
Click - "Create Project
" to start the import process.
If you see an error about invalid GitHub Repository URL, create a new GitHub token(fine-grained) with Public repositories read-only access, and retry import again providing your GitHub userid and token.
6. Add GitLab CICD pipeline variables
Next you are going to enable the GitLab CICD pipeline to run code review when changes are pushed to the repository.
Open the GitLab repository in the browser and navigate to the "Settings / CICD"
section.
Expand the Variables
section and click "Add variable
".
Make sure to uncheck all the checkboxes when you add the new variables.
Example:
Add 3 variables:
PROJECT_ID
- your qwiklabs project idLOCATION
- us-central1GOOGLE_CLOUD_CREDENTIALS
For GOOGLE_CLOUD_CREDENTIALS
variable value, use the service account key created in section above. Run this command in the Google Cloud Shell and copy/paste the value.
cat ~/vertex-client-key.json
CI/CD Variables view:
8. Review GitLab pipeline output
Open "Build / Jobs
" in the GitLab UI and review the pipeline output.
9. Clone GitLab Repo
Return to Google Cloud Shell terminal and clone the repository.
cd ~
mkdir gitlab
cd gitlab
Replace with your GitLab userid and repository url that was just created.
git clone https://gitlab.com:YOUR_GITLAB_USERID/genai-for-developers.git
Change directory and open .gitlab-ci.yml
file. If you changed the repository name during import, update the folder name before running following commands.
cd genai-for-developers
cloudshell edit .gitlab-ci.yml
10. Enable Gemini Code Assist
Click on the "Gemini
" icon in the bottom right corner ,
click "Login to Google Cloud
" and "Select a Google Cloud Project
".
From the popup window, select your Qwiklabs project.
11. Explain code with Gemini Code Assist
Right click anywhere in the .gitlab-ci.yml
file and select Gemini Code Assist > Explain
this.
Review explanation:
12. Run DEVAI CLI locally
Go back to Cloud Shell terminal and run commands below to install devai
locally.
pip3 install devai-cli
The cli was installed but it's not in the PATH.
WARNING: The script devai is installed in '/home/student_00_478dfeb8df15/.local/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Run the command below to update the PATH environment variable. Replace with your user's home folder name. For example: student_00_478dfeb8df15
export PATH=$PATH:/home/YOUR-USER-HOME-FOLDER/.local/bin
Run devai cli command to perform code review locally. Review cli output.
export PROJECT_ID=$(gcloud config get-value project)
export LOCATION=us-central1
devai review code -c ~/gitlab/genai-for-developers/sample-app/src/main/java/anthos/samples/bankofanthos/balancereader
Open review script by running command below:
cloudshell edit ~/gitlab/genai-for-developers/devai-cli/src/devai/commands/review.py
Right click anywhere in the review.py
file and select Gemini Code Assist > Explain
this.
Review explanation.
13. DevAI CLI Development
In this section you will be making changes to devai cli.
To start, set up python virtualenv, install requirements and run the sample command.
cd ~/gitlab/genai-for-developers/devai-cli
python3 -m venv venv
. venv/bin/activate
pip3 install -r src/requirements.txt
pip3 install --editable ./src
devai echo
Re-run code review command to check that everything is working fine:
devai review code -c ~/gitlab/genai-for-developers/sample-app/src/main/java/anthos/samples/bankofanthos/balancereader
Review results using Markdown preview in Cloud Shell Editor.
Create a new file and paste the Gemini's response.
Then use Command Palette and select "Markdown: Open Preview
".
14. Explore devai cli commands
Code review command
devai review code -c ~/gitlab/genai-for-developers/sample-app/src/main/java
Performance review command
devai review performance -c ~/gitlab/genai-for-developers/sample-app/src/main/java
Security review command
devai review security -c ~/gitlab/genai-for-developers/sample-app/src/main/java
Test coverage review command
devai review testcoverage -c ~/gitlab/genai-for-developers/sample-app/src
Blockers review commands
devai review blockers -c ~/gitlab/genai-for-developers/sample-app/pom.xml
devai review blockers -c ~/gitlab/genai-for-developers/sample-app/setup.md
Image/Diagram review and summarization:
Input diagram[~/genai-for-developers/images/extension-diagram.png
]:
Review command:
devai review image \
-f ~/gitlab/genai-for-developers/images/extension-diagram.png \
-p "Review and summarize this diagram"
Output:
The diagram outlines a process for conducting local code reviews using a VS Code extension or CLI, leveraging Google Cloud's Vertex AI (Gemini Pro) for generating review prompts. **Process Flow:** 1. **Code Style Check:** Developers initiate the process by checking their code for adherence to pre-defined style guidelines. 2. **Prompt Generation:** The VS Code extension/CLI sends the code to Vertex AI (Gemini Pro) on Google Cloud. 3. **Vertex AI Review:** Vertex AI analyzes the code and generates relevant review prompts. 4. **Local Review:** The prompts are sent back to the developer's IDE for their consideration. 5. **Optional Actions:** Developers can optionally: - Create new JIRA issues directly from the IDE based on the review prompts. - Generate new issues in a GitLab repository. **Key Components:** * **VS Code Extension/CLI:** Tools facilitating the interaction with Vertex AI and potential integrations with JIRA and GitLab. * **Vertex AI (Gemini Pro):** Google Cloud's generative AI service responsible for understanding the code and generating meaningful review prompts. * **Google Cloud Secret Manager:** Securely stores API keys and access tokens required to authenticate and interact with Google Cloud services. * **JIRA/GitLab (Optional):** Issue tracking and project management tools that can be integrated for a streamlined workflow. **Benefits:** * **Automated Review Assistance:** Leveraging AI to generate review prompts saves time and improves the consistency and quality of code reviews. * **Local Development:** The process empowers developers to conduct reviews locally within their familiar IDE. * **Integration Options:** The flexibility to integrate with project management tools like JIRA and GitLab streamlines workflow and issue tracking.
Image diff analysis:
devai review imgdiff \
-c ~/gitlab/genai-for-developers/images/devai-api.png \
-t ~/gitlab/genai-for-developers/images/devai-api-slack.png
Output:
The following UI elements are missing in the "AFTER UPGRADE STATE" image compared to the "BEFORE UPGRADE STATE" image: 1. **Slack:** The entire Slack element, including the icon, "Team channel" label, and the arrow indicating interaction, is absent in the AFTER UPGRADE image. 2. **Storage Bucket:** The "Storage Bucket" element with its icon and "PDFs" label is missing in the AFTER UPGRADE image. 3. **"GenAI Agents" label in Vertex AI block:** The BEFORE UPGRADE image has "Vertex AI Agents" and "GenAI Agent" labels within the Vertex AI block, while the AFTER UPGRADE image only has "Vertex AI." 4. **"Open JIRA Issue" and "Team Project" labels:** In the BEFORE UPGRADE image, these labels are connected to the JIRA block with an arrow. These are missing in the AFTER UPGRADE image. **Decision Explanation:** The analysis is based on a direct visual comparison of the two provided images, noting the presence and absence of specific UI elements and their associated labels. The elements listed above are present in the BEFORE UPGRADE image but absent in the AFTER UPGRADE image.
Documentation generation command:
devai document readme -c ~/gitlab/genai-for-developers/sample-app/src/main/
Output:
# Bank of Anthos - Balance Reader Service ## Description The Balance Reader service is a component of the Bank of Anthos sample application. It provides a REST endpoint for retrieving the current balance of a user account. This service demonstrates key concepts for building microservices with Spring Boot and deploying them to a Kubernetes cluster. ## Features - Securely retrieves account balances using JWT authentication. - Leverages a local cache for fast balance retrieval. - Asynchronously processes transactions from a central ledger. - Provides health check endpoints for Kubernetes liveness and readiness probes. ...
Review available devai cli commands in the Cloud Shell Editor:
cloudshell edit ~/gitlab/genai-for-developers/devai-cli/README.md
Or review README.md in the GitHub repository.
15. Congratulations!
Congratulations, you finished the codelab!
What we've covered:
- Adding GenAI code review automation steps in GitLab
- Running devai cli locally
What's next:
- More hands-on sessions are coming!
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.
Deleting the project
The easiest way to eliminate billing is to delete the project that you created for the tutorial.
©2024 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.